O'Reilly logo

Professional PHP Design Patterns by Aaron Saray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18.1. Problem and Solution

Some programmers do not immediately see a need to implement the Template Design Pattern. Generally, these are programmers who rely heavily on the inheritance of objects without understanding the actual intent of inheritance. Their catch-all answer to a parent method's not doing everything that they need it to do is to simply overwrite it. While some of this theory is used in the Template Design Pattern, it provides a more robust solution.

The Template Design Pattern creates a class that is intended to be used as a parent for another class. It is designed to enforce the existence of certain functionality and methods in the child classes. The Template object is a bit leaner than other classes with less bundled functionality. In most cases, it also strongly discourages or restricts instantiation of itself directly. This usually is achieved by making the class abstract.

Generally, the Template object defines a shared public method that is common to the goal of any other child class. Inside of that method, it calls the basic steps of the design by requesting other methods belonging to the object. These methods, which will belong to a child object, are either required or optional. If they are required, the class will define that the child class needs to have them. If they are optional for the child class, the Template class may create them as a stub so as not to disable the operation of the shared public method. Another way to accomplish the inclusion of an ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required