Search code examples
c#abstract-classcom+factory-method

Abstract Methods in "Product" - Factory Method C#


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 in WS, the Compare() method cannot be invoked. It's because the client will make use of WS type object, not CompareWS object.

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?


Solution

  • Regina, abstract classes can define both abstract and virtual methods. Virtual methods will contain some default implementation in the base class, but can be overriden by derived classes as needed:

        public abstract class WS
    {
    
        // All derived classes will have to implement this
        public abstract double Calculate(double a, double b);
    
        // This can be overriden by derived classes (since it's a virtual method)
        public virtual string Compare(double a, double b)
        {
            // Some default implementation.
            // Any derived classes can override this implementation if they want
        }
    }