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?
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.
Three Types of Design Patterns (Behavioral, Creational, Structural) Distinguish between Behavioral, Creational, and Structural Design Patterns.
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.
Your foo
method shouldn't be virtual. And in this case the design pattern is called NVI - non-virtual interface
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With