mardi 6 août 2019

design pattern for the case of abstract class with inheriting classes with empty implementation methods

Can you please recommend of a good practice pattern for the following scenario: I have a base abstract class that defines the common behavior and two inheriting classes that each defines set of methods that differ the one implementation from the other. Then in the creation code (let's say by configuration) I choose which implementation I desire for each case of the configuration setting and call "doWork", my issue is how to avoid the "empty" implementations in the inheriting classes in case the inheriting class chooses not to implement the method.

abstract class CommonParentClass{
    public void doWork()
    {
      commonA();
      commonB();
      uniqueA();
      uniqueB();
    }
    internal  void commonA()
    {
        //do work which is same for inheriting classes
    }
    internal void commonB()
    {
       //do work which is same for inheriting classes
    }   
    abstract void uniqueA(); 
    abstract void uniqueB();
    abstract void uniqueC();    
}

class FirstChildClass:CommonParentClass
{
        protected void uniqueA()
        {
            //implementation specific to first child
        }
        protected void uniqueB()
        {
            //EMPTY
        }   
        protected void uniqueC()
        {
            //implementation specific to first child
        }       
}

class SecondChildClass:CommonParentClass
{
        protected void uniqueA()
        {
            EMPTY
        }
        protected void uniqueB()
        {
            //implementation specific to second child
        }
        protected void uniqueC()
        {
            //implementation specific to second child
        }       
}

I have about 5/6 methods which are empty in one of the inheriting classes. What Design Pattern should I use in order to overcome this and avoid code repetition?

Aucun commentaire:

Enregistrer un commentaire