user3879250
user3879250

Reputation:

What is the name of this inheritance design pattern?

Instead of having a public virtual method, you have a public sealed method that calls a protected virtual method. Something like this:

public class Test {

    public void DoStuff(){
        // Do stuff here...
        ProtectedDoStuff();
        // Do more stuff...
    }

    protected virtual void ProtectedDoStuff(){
        // Do stuff...
    }
}

Instead of:

public class Test {

    public virtual void DoStuff(){
        // Do stuff here...
        // Do a lot of stuff...
        // Do more stuff...
    }
}

public class Test2 : Test {

    public override void DoStuff(){
        // Do same stuff as base
        // Do different stuff
        // Do more stuff just like base
    }
}

This avoid having to re-implement all the functionality from the public method if it will be needed all time. I know this has already been asked on stackoverflow but I can't find the question.

Upvotes: 4

Views: 82

Answers (1)

taskinoor
taskinoor

Reputation: 46027

This is template method pattern. From Wikipedia:

The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure.

Upvotes: 4

Related Questions