Where to implement methods when a derived class differs only in "context" and return type

I got the base class "BaseClass" and n derived class DerivedCLass1, DerivedClass2 ... (I don’t know if the term "derived" is correct, but my DerivedClassX is a BaseClass with just a few methods added. Like a car versus a car / motorcycle / truck )

All derived classes share the storePDF function, which generates / saves a PDF file. This method cannot be called from an instance of BaseClass, as it does not matter. But I do not want the code to be repeated across all derived classes. How do I organize classes? (or interfaces)

public class BaseClass
{
    public static string select = "
    select Derivedclass1DT.* from Derivedclass1DT union
    select Derivedclass2DT.* from Derivedclass2DT where type='foo' union
    select * from Derivedclass2DT where type='foo'
    ...    
    "

    public static string objecttype= "";    

    public List<Baseclass> getInstance(string id)
    {
        /* create instance from db using the select ...  */
    }

    public PDFObject storePDF()
    {
        /* 
            generate a pdf shouldn't be called directly from an BaseClass instance
        */
    }        
}

public class DerivedCLass1:BaseClass
{
    public static string select = "select Derivedclass1DT.* from Derivedclass1DT";
    public static string objecttype= "some text specific to this class";

    public List<DerivedCLass1> getInstance(string id)
    {
        /* same code as is the base class just using a different select and return type */    
    }

    /*
        Don't want to store storePDF() implementation here. It is the same for each derived Class
    */
}
public class DerivedCLass2:BaseClass
{
    public static string select = "select Derivedclass2DT.* from Derivedclass2DT where type='foo'";
    public static string objecttype= "some other text specific to this class";

    public List<DerivedCLass1> getInstance(string id)
    {
        /* same code as is the base class just using a different select and return type */    
    }

    /*
        Don't want to store storePDF() implementation here. It is the same for each derived Class
    */
}
+3
source share
5 answers

storePdf , , , , ,

0

, , , , . , , base.DoSomething() from derive.DoSomething(). .

0

storePDF() , , , . - storePDF() .

, , protected storePDFHelper:

public abstract class BaseClass
{
    //(...)

    protected PDFObject storePDFHelper() { /* Do stuff here */ }
    public abstract PDFObject storePDF();
}
public class ChildClass : BaseClass, IPDFGenerator
{
    public override PDFObject storePDF() { return base.storePDFHelper(); }
}

, , , : 1) storePDF BaseClass, , 2) storePDF BaseClass

, :) , !

0

List<DerivedCLass1> getInstance(string id) - ?

, , , . abstract - :

public abstract class BaseClass      //note the abstract modifier!
{

    public abstract List<Baseclass> getInstance(string id);

    public PDFObject storePDF()
    {
        /* 
            generate a pdf shouldn't be called directly from an BaseClass instance
        */
    }        
}

public class DerivedCLass1:BaseClass
{
    public override List<BaseClass> getInstance(string id)
    {
        List<BaseClass> myList = new List<BaseClass>();
        myList.Add(new DerivedClass1() { ... } );       // note the type of the class beng added!    
    }

    /* there is no need to override storePDF(), it is already visible via this class */
}

Using the method, storePDF()leave its implementation in the base class - any user will have to call it through the derived class, because the base class is abstract and cannot be created directly.

0
source

StorePdf should probably be pulled into its class. Whether this should be introduced into your derived classes, or whether StorePdf should receive them as arguments, depends on the implementation details. First, you delete it to remove the SRP violation.

0
source

All Articles