4

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#

3
  • base.Print() has no implementation in it. It is a virtual method. Even if it did how would I be able to call inherited classes specific operations? Commented Nov 13, 2013 at 14:19
  • 1
    In addition, your "derived" classes are not inheriting from A. Commented Nov 13, 2013 at 14:19
  • sorry I just forgot to put them, corrected it. Commented Nov 13, 2013 at 14:22

3 Answers 3

9
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
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I personally would make SpecificPrint virtual, so that it does not need to be used.
override what? the classes are not inheriting anything.
Yep, that's what I was looking for :) I knew it was an easy thing but just couldn't think through it. Thanks. After 4 minute block I will accept this ;)
You will want to make the Print method "final" so that this behaviour isn't overridden in subclasses.
3

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();
    }
}

2 Comments

yes I am looking for something like that but, don't know how to call the virtual method. could you explain?
I knew it :) I knew the problem had something to do with Template Pattern but since I wasn't sure I didn't ment that. Thanks though.
1

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();

4 Comments

Liked this approach too, but could not figure out where will I use the PrintDecorator class.
@TolgaEvcimen: You'd construct this in place of your A_1, A_2, etc classes. When you construct it, you inject the A_1/A_2 instance into the constructor. If you are using a dependency injection framework, you can configure this to automagically happen when you use these objects.
I get it now ;) thanks a lot. I will keep this in mind. Actually I liked this solution better then the other one since it is more generic.But for now I used the other solution, since it is easier to implement ;) Thanks for the usage example too.
@TolgaEvcimen: I've edited to include a simple example, but based on how it fits in your situation you may need to modify it slightly or go with one of the other solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.