Tolga Evcimen
Tolga Evcimen

Reputation: 7352

Design pattern suggestion needed

I need a programming pattern but I couldn't figure out what it will be. I am not even sure if what I want is possible or not. Lets say I have a Class A and 10 class inherited from A. What I want is call the same method Method Print() implemented differently on each inherited class, but all of them need to repeat a same method of 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 see I dont want to keep writing StartingProcedure(); on each overridden Print method. I want to automate that procedure, when I inherit a new class from A I don't want to mind the Starting and Ending procedures, i want to just write the class specific print operation. Is there any pattern that will provide me this kind of a class behaviour?

BTW I work with C#

Upvotes: 4

Views: 204

Answers (3)

mao47
mao47

Reputation: 967

As an alternative to Rik's answer, if you can change the class hierarchy, and you really don't want to call the base methods in the derived methods, you can use composition instead of inheritance to accomplish this. By using the decorator pattern, you can specify a print method that wraps the derived functions.

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
    }
}

Example usage:

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

Upvotes: 1

O.O
O.O

Reputation: 11287

The pattern you are looking for in this case is the Template Pattern by Gang of Four (GoF).

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

usually you'll add another method called the 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();
    }
}

Upvotes: 3

Rik
Rik

Reputation: 29243

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
    }
}

Upvotes: 9

Related Questions