Abstract Methods in "Product" - Factory Method C#
- by Regina Foo
I have a simple class library (COM+ service) written in C# to consume 5 web services: Add, Minus, Divide, Multiply and Compare.
I've created the abstract product and abstract factory classes. The abstract product named WS's code:
public abstract class WS
{
public abstract double Calculate(double a, double b);
public abstract string Compare(double a, double b);
}
As you see, when one of the subclasses inherits WS, both methods must be overridden which might not be useful in some subclasses. E.g. Compare doesn't need Calculate() method.
To instantiate a new CompareWS object, the client class will call the CreateWS() method which returns a WS object type.
public class CompareWSFactory : WSFactory
{
public override WS CreateWS()
{
return new CompareWS();
}
}
But if Compare() is not defined as abstract in WS, the Compare() method cannot be invoked.
This is only an example with two methods, but what if there are more methods? Is it stupid to define all the methods as abstract in the WS class?
My question is: I want to define abstract methods that are common to all subclasses of WS whereas when the factory creates a WS object type, all the methods of the subclasses can be invoked (overridden methods of WS and also the methods in subclasses). How should I do this?