Hook
Every framework incorporates the two essential construction principles, Unification and Separation, no matter how simple or how complex the particular template and hook methods are. Fine-grained classes apply the same construction principles as complex classes in order to introduce flexibility. They
Many entries in the pattern catalog can be regarded as small frameworks,
[6]
consisting of a few classes, that apply the essential construction principles in various more or less
[6] These are Template Method, Factory Method; Bridge, Abstract Factory, Builder, Command, Interpreter, Observer, Prototype, State, Strategy; Composite, Decorator, Chain of Responsibility
The catalog pattern Template Method discusses the basic concept of template and hook methods in a quite generic way analogous with the Unification construction principle. The Factory Method pattern keeps the creation of objects flexible and relies on a unification of template and hook methods in one class. The template and hook methods are in class Creator, the hook method has the name factoryMethod(). The hook semantics determine the pattern name. Figure 4.21 annotates the structure of the Factory Method pattern (see Gamma et al., 1995) and points out which of the methods in class Creator is the template and which one is the hook method.
The Abstract Factory pattern results from the Factory Method pattern by separating template and hook methods in two classes. This is analogous with keeping the hook method round() in the same class as the template method versus separating the two methods.
A significant portion of the framework-centered pattern catalog entries relies on a separation of template and hooks, that is on the basic Separation principle. The catalog pattern Bridge discusses the abstract coupling mechanism generically. Other catalog entries that use template-hook separation introduce more specific semantics for their hooks: Abstract Factory, Builder, Command, Interpreter, Observer, Prototype, State, and Strategy. The names of these catalog patterns
| Catalog entry | Class with hook | Hook method | Class with template | Hook semantics |
|---|---|---|---|---|
| Abstract Factory | AbstractFactory | CreateProduct() | Client | Families of product objects. |
| Builder | Builder | BuildPart() | Director | How a complex object is created. |
| Command | Command | Execute() | Invoker | When and how a request is fulfilled. |
| Interpreter | AbstractExpression | Interpret(…) | Client | Interpretation of a language. |
| Observer | Observer | Update() | Subject | How the dependent objects stay up to date. |
| Prototype | Prototype | Clone() | Client |
Class of object that is
|
| State | State | Handle() | Context | States of an object. |
| Strategy | Strategy | AlgorithmInterface() | Context | An algorithm. |
Of these Separation-based patterns, the Abstract Factory, Builder, Observer, State, and Strategy patterns represent rather generic examples of hook semantics. In particular, the necessity to keep object creation flexible - as in the two patterns Abstract Factory and Builder - is likely to occur in many frameworks.
Besides the unification and separation of template and hooks, three principles result from template-hook combinations that allow recursive graph compositions of objects - for example, in trees and lists. For the sake of completeness, we include the derivation of these principles as a result of considering the possible template-hook combinations. The pattern catalog discusses the details of how and when to apply these construction principles.
In the pattern catalog entry Composite, the class with the template method (T) inherits from the class with the hook method (H). The association between the two classes is a zero to N relationship - that is, a T object can refer to any number of H objects (see Figure 4.22(a)). The name Composite expresses the
The recursive composition where template and hooks are separated and where a T object can refer to zero or one H object (see Figure 4.22(b)) represents the Decorator pattern. The name expresses that objects can be dynamically adorned with additional behavior. Note that T need not be a direct subclass of H - that is, several other classes might be in between T and H. This is valid for both class diagrams (a) and (b) in Figure 4.22.
Finally, a kind of degenerated recursive composition where template and hooks melt in one class TH, and where a TH object can refer to zero or one other TH object (see Figure 4.23) is the Chain of Responsibility pattern in the catalog.