4.2. Separation principle – adaptation through composition

The Separation construction principle places the template and hook methods in separate classes. Let's rework the CurrencyConverter example. The hook method round() becomes part of another class RoundingPolicy. The class with the hook method is, in most cases, an abstract class or a Java interface (see below). An instance of CurrencyConverter refers to an object of static type RoundingPolicy through an instance variable rPol. Further, CurrencyConverter typically offers a method that allows a change of the rounding policy. This is method defineRoundingPolicy() in Example 4.4. In other words, the behavior of the template method convert() in CurrencyConverter can be changed by plugging in ...

Get UML Profile for Framework Architectures, The now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.