How to implement WebPage pattern and add Interface in save class test - c#

Please see this structure (from here):
public abstract class AbstractPage<T> where T : AbstractPageEmenetsMap, new()
{
protected readonly string url;
protected VendorInfo vendorInfo;
public AbstractPage(VendorInfo vendorInfo)
{
this.vendorInfo = vendorInfo;
this.url = this.vendorInfo.Url;
}
public void Navigate()
{
WebDriver.Driver.Navigate().GoToUrl(this.url);
}
protected T Map
{
get { return new T(); }
}
}
public abstract class AbstractPage<M, V> : AbstractPage<M>, ITest
where M : AbstractPageEmenetsMap, new()
where V : AbstractPageValidator<M>, new()
{
public AbstractPage(VendorInfo vendorInfo)
: base(vendorInfo) { }
public V Validate()
{
return new V();
}
public void Login();
{
throw new System.NotImplementedException();
}
public void Logout();
{
throw new System.NotImplementedException();
}
}
And i want to add interface with some operations
public interface ITest
{
void Login();
void Logout();
}
Now this is Son class:
public class GmailPage : AbstractPage<GmailPageElementsMap, GmailPageValidator>, ITest
{
public GmailPage() : base("http:...") { }
}
Class that holds all the elements:
public IWebElement EmailAddressTextBox
{
get
{
return WebDriver.WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.Id("identifierId")));
}
}
And validator:
public class GmailPageValidator : AbstractPageValidator<GmailPageElementsMap>
{
}
As you can see i implement ITest from my Gmail class but i don't received any compile error although i do not add this 2 interface methods (Login and Logout).

This is because those methods are implemented in the parent AbstractPage. If you want to force GmailPage (and all other derived classes) to implement Login() and Logout() declare them as abstract in the parent AbstractPage class
public abstract class AbstractPage<M, V> : AbstractPage<M>, ITest
where M : AbstractPageEmenetsMap, new()
where V : AbstractPageValidator<M>, new()
{
public AbstractPage(VendorInfo vendorInfo) : base(vendorInfo) { }
public V Validate()
{
return new V();
}
public abstract void Login();
public abstract void Logout();
}
And override in GmailPage
public class GmailPage : AbstractPage<GmailPageElementsMap, GmailPageValidator>
{
public GmailPage() : base("http:...") { }
public override void Login()
{
throw new System.NotImplementedException();
}
public override void Logout()
{
throw new System.NotImplementedException();
}
}

Related

When do we need to create the concrete factory method?

I am trying to learn the factory design pattern. I have created a code for creating two animals (for now only dogs and a cats).
I have got an Animal (as a product)
public abstract class Animal
{
public abstract void CreateBody();
public abstract void CreateLeg();
}
Dog and Cat Implements Animal class:
public class Dog: Animal
{
public override void CreateBody()
{
Console.WriteLine("Dog body created");
}
public override void CreateLeg()
{
Console.WriteLine("Dog body created");
}
}
Cat Implements Animal:
public class Cat: Animal
{
public override void CreateBody()
{
Console.WriteLine("Cat body created");
}
public override void CreateLeg()
{
Console.WriteLine("Cat Leg created");
}
}
Created a factory class that makes different animal according to the input given in the parameter:
public class AnimalFactory
{
public static Animal CreateAnimal(string animal)
{
Animal animalDetails = null;
if (animal == "cat")
{
animalDetails = new Cat();
}
else if (animal == "dog")
{
animalDetails = new Dog();
}
animalDetails.CreateBody();
animalDetails.CreateLeg();
return animalDetails;
}
}
In the main it is used as:
static void Main(string[] args)
{
Animal animalDetails = AnimalFactory.CreateAnimal("cat");
if (animalDetails != null)
{
}
else
{
Console.Write("Invalid Card Type");
}
Console.ReadLine();
}
Here in this code, I know exactly what methods I need to call to create an animal, so I called those methods in the factory class.
As I read in the various tutorials and books, most of the implementation of the factory method uses another concrete factory method to create the concrete product?
In what case it is appropriate to create the concrete factory method? Is that concrete factory relevant in this demo too?
My implementation :
Animal interface :
public interface IAnimal
{
void CreateBody();
void CreateLeg();
}
Cat & Dog classes:
public class Dog : IAnimal
{
public void CreateBody()
{
Console.WriteLine("Dog body created");
}
public void CreateLeg()
{
Console.WriteLine("Dog leg created");
}
}
public class Cat : IAnimal
{
public void CreateBody()
{
Console.WriteLine("Cat body created");
}
public void CreateLeg()
{
Console.WriteLine("Cat leg created");
}
}
Animal factory
public static class AnimalFactory
{
public static IAnimal? GetInstance(Type type)
{
if(type.GetInterfaces().Contains(typeof(IAnimal))) // check if type implement IAnimal
{
return Activator.CreateInstance(type) as IAnimal;
}
return null;
}
}
Use case
IAnimal? cat = AnimalFactory.GetInstance(typeof(Cat));
IAnimal? dog = AnimalFactory.GetInstance(typeof(Dog));
cat?.CreateBody();
cat?.CreateLeg();
dog?.CreateBody();
dog?.CreateLeg();
this structure can be expanded into an abstractory factory provider. it currently demonstrate a simple factory provider model. The provider accepts the derived class of animal which is either cat or dog and sets a private label called type
public abstract class Animal
{
public string _Type;
public abstract void CreateBody();
public abstract void CreateLeg();
public abstract void SetType(string type);
}
public class Dog : Animal
{
public Dog() {
SetType("Dog");
}
public override void SetType(string type)
{
_Type = type;
}
public override void CreateBody()
{
Console.WriteLine("Dog body created");
}
public override void CreateLeg()
{
Console.WriteLine("Dog body created");
}
}
public class Cat : Animal
{
public Cat()
{
SetType("Cat");
}
public override void SetType(string type)
{
_Type = type;
}
public override void CreateBody()
{
Console.WriteLine("Cat body created");
}
public override void CreateLeg()
{
Console.WriteLine("Cat Leg created");
}
}
public class AnimalProviderFactory {
public AnimalProvider GetAnimalProvider()
{
return new AnimalProvider();
}
}
public class AnimalProvider{
public string GenerateShippingLabelFor(Animal catOrDog)
{
return catOrDog._Type;
}
}
public class AnimalType
{
private readonly Animal catOrDog;
private readonly AnimalProviderFactory animalProviderFactory;
public AnimalType(Animal catOrDog, AnimalProviderFactory animalProviderFactory)
{
this.catOrDog = catOrDog;
this.animalProviderFactory = animalProviderFactory;
}
public string Finalize()
{
var provider = animalProviderFactory.GetAnimalProvider();
return provider.GenerateShippingLabelFor(catOrDog);
}
}
public void TestAbstractFactory()
{
Cat cat = new Cat();
AnimalType animalType= new AnimalType(cat, new AnimalProviderFactory());
var type = animalType.Finalize();
_output.WriteLine(type);
Assert.True(true);
}

Call derived class constructor before base class

I need to execute derived class constructor before base class constructor.
I am attaching code which is using virtual object in base class which need to be initialised in derived class. We decide type of virtual object in derived class and then assign values to that object once we have type of that object.
How could I call derived class constructor before base class constructor in this scenario.
public class BaseClass : UserControl, INotifyPropertyChanged
{
public Path ConnIn;
public Path ConnOut;
public virtual ObjectBase BaseObject { get; set; }
public void BaseClass(XmlElementConfig config)
{
this.BaseObject.Title = config.Title;
this.BaseObject.GroupID = config.GroupID;
}
}
public partial class DerivedClass : CanvasBase
{
private Audio_MonitorAction audio_objectAction;
public override ObjectBase BaseObject
{
get { return audio_objectAction; }
set
{
audio_objectAction = (Audio_MonitorAction)value;
NotifyPropertyChanged();
}
}
public DerivedClass(XmlElementConfig config) : base(config)
{
InitializeComponent();
audio_objectAction = new Audio_MonitorAction(createGuid);
}
}
"execute derived class constructor before base class constructor" is impossible
if possible, move initialization into BaseObject property (why should it be virtual?)
public class BaseClass : UserControl, INotifyPropertyChanged
{
public Path ConnIn;
public Path ConnOut;
private ObjectBase baseObject;
public ObjectBase BaseObject
{
get { return baseObject; }
set
{
baseObject = value;
if (baseObject != null)
{
baseObject.Title = config.Title;
baseObject.GroupID = config.GroupID;
}
NotifyPropertyChanged();
}
}
XmlElementConfig config;
public void BaseClass(XmlElementConfig config)
{
this.config = config;
}
}
public partial class DerivedClass : CanvasBase
{
private Audio_MonitorAction audio_objectAction;
public DerivedClass(XmlElementConfig config) : base(config)
{
InitializeComponent();
BaseObject = audio_objectAction = new Audio_MonitorAction(createGuid);
}
}
public class BaseClass : UserControl, INotifyPropertyChanged
{
public Path ConnIn;
public Path ConnOut;
public virtual ObjectBase BaseObject { get; set; }
public void BaseClass(XmlElementConfig config)
{
InitBase(config);
}
protected void InitBase(XmlElementConfig config)
{
if (BaseObject != null)
{
BaseObject.Title = config.Title;
BaseObject.GroupID = config.GroupID;
}
}
}
public DerivedClass(XmlElementConfig config) : base(config)
{
InitializeComponent();
audio_objectAction = new Audio_MonitorAction(createGuid);
InitBase(config);
}
Alternatively you can do in BaseClass call to virtual function and override it in derived class.
public class BaseClass : UserControl, INotifyPropertyChanged
{
public Path ConnIn;
public Path ConnOut;
public virtual ObjectBase BaseObject { get; set; }
public void BaseClass(XmlElementConfig config)
{
InitDerivedClass();
if (BaseObject != null)
{
BaseObject.Title = config.Title;
BaseObject.GroupID = config.GroupID;
}
}
protected virtual void InitDerivedClass() {}
}
public partial class DerivedClass : CanvasBase
{
private Audio_MonitorAction audio_objectAction;
public override ObjectBase BaseObject
{
get { return audio_objectAction; }
set
{
audio_objectAction = (Audio_MonitorAction)value;
NotifyPropertyChanged();
}
}
protected override ObjectBase InitDerivedClass
{
audio_objectAction = new Audio_MonitorAction(createGuid);
}
public DerivedClass(XmlElementConfig config) : base(config)
{
InitializeComponent();
}
}

Abstract factory implementation within C# application

I'd like to implement abstract factory design pattern. I add this snippet :
public class Class1
{
static Ete _ete;
static Hiver _hiver;
public static void Main(Clothes cl)
{
_ete = cl.CreateEteClothes();
_hiver = cl.CreateHiverClothes();
Console.WriteLine(_ete.GetMarque());
Console.ReadKey();
Console.WriteLine(_hiver.GetMarque());
Console.ReadKey();
}
}
public abstract class Clothes
{
public abstract Ete CreateEteClothes();
public abstract Hiver CreateHiverClothes();
}
public abstract class ItalianFactory: Clothes
{
public override Ete CreateEteClothes()
{
return new TShirtJuve();
}
public override Hiver CreateHiverClothes()
{
return new PullJuve();
}
}
public abstract class FrenchFactory : Clothes
{
public override Ete CreateEteClothes()
{
return new TShirtPsg();
}
public override Hiver CreateHiverClothes()
{
return new PullPsg();
}
}
public abstract class TunisianFactory : Clothes
{
public override Ete CreateEteClothes()
{
return new TShirtCa();
}
public override Hiver CreateHiverClothes()
{
return new PullCa();
}
}
public abstract class Ete
{
public abstract string GetMarque();
}
public abstract class Hiver
{
public abstract string GetMarque();
}
public class TShirtJuve: Ete
{
public override string GetMarque()
{
return "Juventus T shirt";
}
}
public class TShirtPsg : Ete
{
public override string GetMarque()
{
return "PSG T shirt";
}
}
public class TShirtCa : Ete
{
public override string GetMarque()
{
return "club africain T shirt";
}
}
public class PullJuve : Hiver
{
public override string GetMarque()
{
return "Juventus Pull";
}
}
public class PullPsg : Hiver
{
public override string GetMarque()
{
return "PSg Pull";
}
}
public class PullCa : Hiver
{
public override string GetMarque()
{
return "Club africain Pull";
}
}
I'd like to test this implementation, but I get an exception indicates that the signature of main method is not acceptable.
So How can I fix my code to test this design pattern implementation?
You have public static void Main(Clothes cl)
This should be static void Main(string[] args) as this is the entry point for the application and there can be only one entry point. See the .NET documentation for more info.
A method's signature usually consists of the methods name, return type, and parameters. Your application is expecting the correct signature for the Main method, hence it's giving you this exception.

c# Cannot call abstract generic method from static base class method [duplicate]

I'm trying to create an abstract generic class which inherits from another abstract generic class.
Here's what I have so far
public abstract class BaseClass {
public long Id { get; private set; }
public BaseClass(long id) {
this.Id = id;
}
}
public abstract class BaseClass<T> : BaseClass where T : BaseClass {
protected BaseClass(long id)
: base(id) {
}
public static T Get(long id) {
T item;
return TryGet(id, out item) ? item : default(T);
}
public static bool TryGet(long id, out T item) {
item = null; // This is where I call the cache but for this example I've removed so it will compile
if (item != null) { return true; }
else {
// Call TryGetFallback method
return false;
}
}
protected abstract T TryGetFallback(long id);
}
public abstract class DerivedClass : BaseClass<DerivedClass> {
public String Name { get; private set; }
public DerivedClass(long id, String name)
: base(id) {
this.Name = name;
}
}
public class DerivedDerivedClass : DerivedClass {
protected override DerivedDerivedClass TryGetFallback(long id) {
// Handle the try get fallback
}
}
The TryGetFallback method on the DerivedDerivedClass causes a compiler error.
First you need to fix your BaseClass<T> implementation to not have a recursive type constraint.
public abstract class BaseClass<T> : BaseClass where T : new() {
//snip
}
Then you can use it in your derived class, for example I will make it use int for the generic type parameter:
public abstract class DerivedClass : BaseClass<int> {
//snip
}
And now if you compile it will warn you that 'DerivedDerivedClass' does not implement inherited abstract member 'BaseClass<int>.TryGetFallback(long)'
Thanks for the tips #DavidG it's helped me to solve the problem with the following code
public abstract class BaseClass {
public long Id { get; private set; }
public BaseClass(long id) {
this.Id = id;
}
}
public abstract class BaseClass<T> : BaseClass where T : BaseClass<T>, new() {
protected BaseClass(long id) : base(id) { }
public static T Get(long id) {
T item;
return TryGet(id, out item) ? item : default(T);
}
public static bool TryGet(long id, out T item) {
item = null; // Try to get item from cache here
if (item != null) { return true; }
else {
T obj = new T();
item = obj.TryGetFallback(id);
return item != null;
}
}
protected abstract T TryGetFallback(long id);
}
public abstract class DerivedClass<T> : BaseClass<T> where T : DerivedClass<T>, new() {
public String Name { get; private set; }
public DerivedClass() : base(0) { }
public DerivedClass(long id, String name)
: base(id) {
this.Name = name;
}
protected abstract override T TryGetFallback(long id);
}
public class DerivedDerivedClass : DerivedClass<DerivedDerivedClass> {
public DerivedDerivedClass() {
}
protected override DerivedDerivedClass TryGetFallback(long id) {
throw new NotImplementedException();
}
}

Cannot convert from 'SpecificComponent' to 'IComponent<IComponentGuid>'

I've this class structure:
namespace ClassLibrary1
{
public interface IComponentGuid { }
class ComponentGuid : IComponentGuid{}
internal interface IComponent<T> where T : IComponentGuid {
List<T> List();
}
class SpecificComponent : IComponent<ComponentGuid> {
public List<ComponentGuid> List()
{
throw new System.NotImplementedException();
}
}
class P
{
public P(IComponent<IComponentGuid> pComponent) { }
}
class Caller
{
public Caller()
{
var specific = new SpecificComponent();
var p = new P(specific);
}
}
}
The problem arise instantiating P: var p = new P(specific);
I get a
cannot convert from 'ClassLibrary1.SpecificComponent' to 'ClassLibrary1.IComponent<ClassLibrary1.IComponentGuid>'
What am I doing wrong?
Thank you.
You can make it work if you forgo List<T> on your interface and replace it with a co-variant interface of IEnumerable<T> and then make your type parameter co-variant as well:
namespace ClassLibrary1
{
public interface IComponentGuid { }
class ComponentGuid : IComponentGuid{}
internal interface IComponent<out T> where T : IComponentGuid {
IEnumerable<T> List();
}
class SpecificComponent : IComponent<ComponentGuid> {
public IEnumerable<ComponentGuid> List()
{
throw new System.NotImplementedException();
}
}
class P
{
public P(IComponent<IComponentGuid> pComponent) { }
}
class Caller
{
public Caller()
{
var specific = new SpecificComponent();
var p = new P(specific);
}
}
}
I'm trying this solution splitting the IComponent interface into two, one covariant and one invariant.
namespace ClassLibrary1
{
public interface IComponentGuid { }
public class ComponentGuid : IComponentGuid { }
public interface IComponentBase<out T> where T : IComponentGuid
{
IEnumerable<T> List();
}
interface IComponent<T>
{
void AddToList(T item );
}
public class SpecificComponent : IComponentBase<ComponentGuid>, IComponent<ComponentGuid>
{
public IEnumerable<ComponentGuid> List()
{
throw new System.NotImplementedException();
}
public void AddToList(ComponentGuid item)
{
throw new System.NotImplementedException();
}
}
public class P
{
public P(IComponentBase<IComponentGuid> pComponentBase) { }
}
class Caller
{
public Caller()
{
var specific = new SpecificComponent();
var p = new P(specific);
}
}
}

Categories

Resources