As one of only two design patterns that use inheritance instead of composition in the Behavioral category of design patterns, the Template Method would seem to be a hard sell when it comes to proposing the design pattern as a paragon of good OOP. After all, the second principle put forth by GoF is to favor composition over inheritance. Why even bother with a design pattern that seems to favor inheritance over composition? Further, is the Hollywood Principle unique to the design patterns using inheritance, or is it a general principle usable in both design patterns using composition as well?
Whenever a design pattern uses inheritance as a key element instead of composition, you need to consider the reason. To understand the reason, you need to fully understand the principle of favoring composition over inheritance as a general principle in good OOP. The principle's established because of certain advantages of composition over inheritance, especially the composition advantage of not breaking encapsulation. However, Gamma, Helm, Johnson and Vlissides (GoF) note that inheritance also has certain advantages. One such advantage is that when using subclasses where some but not all operations are overridden, modifying the reused implementation is easier. Because the Template Method design pattern does exactly that—uses some but not all operations that can be overridden to achieve flexibility—it incorporates ...
No credit card required