O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

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

Key OOP Concepts used with the Template Method

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?

Why Inheritance and Not Composition?

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 ...

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