Design Template Required

I need a programming template, but I could not figure out what it would be. I'm not even sure if what I want is possible or not. Suppose I have a class Class Aand 10 inherited from A. I want the same method to Method Print()implement differently in each inherited class, but they all have to repeat the same method Class A.

Class A
{
    public virtual void Print()
    { 
    }
    protected void StartingProcedure()
    {
        /// something 
    }
    protected void EndingProcedure()
    {
        /// something 
    }
}

Class A_1 : A
{
    public override void Print()
    {
        StartingProcedure();
        /// class specific print operation
        EndingProcedure();
    }
}

Class A_2 : A
{
    public override void Print()
    {
        StartingProcedure();
        /// class specific print operation
        EndingProcedure();
    }
}

As you can see, I do not want to write StartingProcedure();for each overridden method Print. I want to automate this procedure when I inherit a new class from A. I don’t want to object to the Start and End procedures, I just want to write a print operation for a particular class. Is there any pattern that will provide me with this class behavior?

BTW I am working with C #

+4
3
Class A
{
    public void Print()
    { 
        StartingProcedure();
        SpecificPrint();
        EndingProcedure();
    }
    protected void StartingProcedure()
    {
        /// something 
    }
    protected void EndingProcedure()
    {
        /// something 
    }
    protected virtual SpecificPrint() // could possibly be abstract
    {
    }
}

Class A_1 : A
{
    public override void SpecificPrint()
    {
        /// class specific print operation
    }
}

Class A_2 : A
{
    public override void SpecificPrint()
    {
        /// class specific print operation
    }
}
+9

, , - Gang of Four (GoF).

Class A
{
    public virtual void Print()
    { 
    }
    protected void StartingProcedure()
    {
        /// something 
        Print();
    }
    protected void EndingProcedure()
    {
        /// something 
    }
}

, Template Method:

/// Template Method
protected void Run()
{
    StartingProcedure();
    Print();
    EndingProcedure();
}

class A_1 : A
{
    public override void Print()
    {
        /// class specific print operation
    }
    public A_1()
    {
       base.Run();
    }
}
+3

Rik-, , , , . , , .

class PrintDecorator : A
{
    private A wrappedA;
    public PrintDecorator(A a)
    {
        wrappedA = a;
    }
    public virtual void Print()
    {
        //wrap the derived logic with the pre- and post- processing
        StartingProcedure();
        wrappedA.Print();
        EndingProcedure();
    }
}
class A
{
    public virtual void Print()
    { 
    }
    protected void StartingProcedure()
    {
        /// something 
    }
    protected void EndingProcedure()
    {
        /// something 
    }
}

class A_1 : A
{
    public override void Print()
    {
        /// class specific print operation
    }
}

class A_2 : A
{
    public override void Print()
    {
        /// class specific print operation
    }
}

:

A dec = new PrintDecorator(new A_1());
dec.Print();
+1

All Articles