Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What design pattern is this?

Several years ago, I used to create interfaces like this :

class Base
{
  public:
    virtual ~Base
    {
    }

    void foo()
    {
      doFoo();
    }

  private:
    virtual void doFoo() = 0;
};

then a derived would be :

class Derived : public Base
{
  public:
    virtual ~Derived()
    {
    }

  private:
    virtual void doFoo()
    {
    }
};

I am sure I saw this as a design pattern somewhere, but now I can not find it anywhere, and can not remember how it is called.

So, how is this design pattern called?

like image 259
BЈовић Avatar asked Mar 30 '11 08:03

BЈовић


People also ask

How do you find design patterns?

To use design patterns effectively you need to know the context in which each one works best. This context is : Participants — Classes involved. Quality attributes — usability, modifiability, reliability, performance.

What are the 3 types of patterns?

Three Types of Design Patterns (Behavioral, Creational, Structural) Distinguish between Behavioral, Creational, and Structural Design Patterns.

What are the patterns of design?

As per the design pattern reference book Design Patterns - Elements of Reusable Object-Oriented Software , there are 23 design patterns which can be classified in three categories: Creational, Structural and Behavioral patterns. We'll also discuss another category of design pattern: J2EE design patterns.


2 Answers

Your foo method shouldn't be virtual. And in this case the design pattern is called NVI - non-virtual interface

like image 81
Armen Tsirunyan Avatar answered Sep 21 '22 15:09

Armen Tsirunyan


This is the template method pattern. Relevant excerpt from Wikipedia:

A template method defines the program skeleton of an algorithm. One or more of the algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed.

I've seen this pattern used a lot to "enforce" calling the base class implementation (which normally has to be done explicitly in the deriving class).

like image 27
Chris Schmich Avatar answered Sep 23 '22 15:09

Chris Schmich