The subclass method behavioral pattern allows many different subclasses to define their own behavior while reusing common functionality from the base class.

This is a simple pattern with a subtle intent and usage. It’s very much the opposite of the template method design pattern.

While the template method pattern defines a normal method in a base class that sets the overall structure of what the method should do and lets derived classes fill in specific details by overriding protected virtual methods, this subclass method pattern defines a pure virtual method in a base class that derived classes must override. It then provides non-virtual protected methods that offer common and useful functionality for the derived classes to use.

You can use this pattern anytime you have many different derived classes that all need to implement uniques but related behavior while avoiding code duplication by putting common functionality in helper methods in the base class. Putting the common behavior in one place also helps reduce coupling between each of the derived classes and the rest of the application code. the derived classes are coupled to the base class and it’s the base class that’s then coupled to the rest of the code.

This pattern also works well with the interpreter design pattern. You can use this pattern to define major behavior differences and the interpreter design pattern to customize the behavior with data.


What's on your mind?
On a scale of 0 to 10, how likely are you to refer us to friends?