Unknown
Unknown

Reputation: 306

Polymorphism using interface

A class BasePrintHandler as been derived from the interface IPrintHandler and implements required functionality. This is provided as a library

Application has to provide a functionality(SendToPrint) which is different from the library. Now For this I have derived a class from BaseClass as well as from the interface.

For this I have created an example

interface IPrintHandler
{
    void SetPage();
    void SendToPrint();
}

class BasePrintHandler : IPrintHandler
{
    void IPrintHandler.SendToPrint()
    {
        Console.WriteLine("in the base print handler");
    }

    void IPrintHandler.SetPage()
    {
        Console.WriteLine("in the setpage");
    }
}

class ChildPrintHandler : BasePrintHandler,IPrintHandler
{
    private BasePrintHandler m_Printhandler;

    public ChildPrintHandler()
    {
        m_Printhandler = new BasePrintHandler();
    }

    void IPrintHandler.SendToPrint()
    {
        Console.WriteLine("in the derive class");
        IPrintHandler aPRinthandler = m_Printhandler as IPrintHandler;
        aPRinthandler.SendToPrint();
    }
}


class Program
{
    static void Main(string[] args)
    {
        IPrintHandler aLayouthandler = new ChildPrintHandler();
        aLayouthandler.SendToPrint();

        aLayouthandler.SetPage();

        Console.Read();
    }
}
  1. How good is this design , the class derived from the baseclass and as well from the interface and implement only the required functionality - SendToPrint.

Upvotes: 1

Views: 1260

Answers (1)

Moo-Juice
Moo-Juice

Reputation: 38825

It's not too bad, but in this kind of model I prefer to do it slightly differently. Your base class may implement some default behaviour (that can be completely replaced), or it might implement some mostly required behaviour, where the subclass must remember to call the base-class. When I write interfaces and provide a base-class to provide a boiler-plate implementation, I go a step further with protected overrides:

public interface IPrinter
{
    void SetPage();
    void SendToPrint();
} // eo interface IPrinter


public class BasePrinter : IPrinter /* Could well be abstract */
{
     protected virtual void SetPageImpl() { }      /* Also could be abstract */
     protected virtual void SendToPrintImpl() { }  /* ........ditto .....    */


     // IPrinter implementation
     public void SetPage()
     {
         SetPageImpl();
         // can do other stuff here.  Will always be called!
     } // eo SetPage


     public void SendToPrint()
     {
         SendToPrintImpl();
         // ditto
     }
}  // eo class BasePrinter


public class ChildPrinter : BasePrinter
{
     // we only do something different when setting a page
     protected override void SetPageImpl()
     {
        // ChildPrinter-specifics
     } // eo SetPageImpl
} // eo class ChildPrinter

This way, we keep the contract of the interface, provide boiler-plate code that will always be called, and allow other classes to change the behaviour. I tend to implement the above with abstract protected functions in most cases. I also don't have to remember to call the base-class implementation, or in which order (start or end of my override?)

Upvotes: 1

Related Questions