Abstract Methods in "Product" - Factory Method C#
Posted
by Regina Foo
on Stack Overflow
See other posts from Stack Overflow
or by Regina Foo
Published on 2010-03-15T07:44:35Z
Indexed on
2010/03/15
7:49 UTC
Read the original article
Hit count: 411
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?
© Stack Overflow or respective owner