5.2 A sample cookbook recipe

5.2 A sample cookbook recipe

Reconsider the RoundingPolicy variation point realized through the Separation construction principle (Figure 5.2). It provides an abstract class RoundingPolicy together with three specific implementations of the hook method round() in subclasses. Let us assume this example is part of a framework and let us define a recipe for its adaptation. What is required for adapting the rounding variation point?

Figure 5.2. Keeping the rounding policy flexible by means of the Separation construction principle
graphics/05fig02.gif

If the framework provides all the required implementations of RoundingPolicy, an adaptation by composition is feasible. In this case, the application developer plugs an appropriate instance of a RoundingPolicy subclass into a CurrencyConvertor object. If the UML-F fixed tag is attached to the generalization, additional rounding policies cannot be defined.

If, instead, the framework does not provide a complete set of subclasses of the RoundingPolicy class, the generalization should be marked as adapt-static or adapt-dyn . The application developer can then use one of the predefined classes, but can also add specific ones. The cookbook recipe in Table 5.2 describes this straightforward adaptation.

Table 5.2. Cookbook recipe for adapting the rounding policy of a CurrencyConverter instance
Recipe 'CurrencyConverter: How to adapt the rounding behavior'
Intent To adapt the rounding behavior of a currency converter.
Classes
  • CurrencyConverter provides the common conversion behavior in the convert() template method.

  • RoundingPolicy represents the variation point that can be redefined.

  • See Figure 5.2 for a UML-F class diagram.

Steps to Apply
  1. Things to know about the CurrencyConverter functionality:

    • The convert() method is used to convert between currencies. It may be invoked by various clients of the framework.

    • The convert() method's responsibility is to perform the conversion and to round the result.

    • Method round() is used only in the context of convert().

  2. Determine whether there is already a subclass of RoundingPolicy that provides the desired rounding algorithm. The framework provides DefaultRPol, Rpol10, and RPol100.

  3. If these three are not appropriate rounding policies, define a new subclass of RoundingPolicy.

    • Implement the round() method accordingly.

    • You cannot invoke super() within your implementation of the round() method, since the base class only provides an abstract method.

  4. Ensure that your new class is being used: the rounding class is selected by passing it as parameter to the constructor of the CurrencyConverter class.

Discussion See discussion above.

Figure 5.3 presents a sample adaptation. The tag adapt-static explicitly indicates that the application developer may add new classes. Furthermore, some existing classes (such as RPol10) have been omitted in the diagram instead, an application class RPolByBase has been added. The presentation tag is attached to that class to indicate that all methods and instance variables provided by it are shown. Class RPolByBase allows the definition of the rounding base dynamically, thus providing additional flexibility.

Figure 5.3. A sample adaptation following the cookbook recipe
graphics/05fig03.gif

The UML-F tags indicate that the rounding policy variation point is based on the Separation pattern. Thus, a generic recipe for the Separation pattern would usually suffice for adapting variation points that rely on that construction principle. If more specific recipes are required, they could build on generic recipes. Sections 5.3 5.5 introduce the generic recipes for the Unification and Separation construction principles and for the Composite pattern.[1] For understanding and adapting the Decorator and Chain of Responsibility patterns, we refer to the GoF pattern catalog (Gamma et al., 1995).

[1] As the Composite, Decorator, and Chain of Responsibility construction principles overlap with the GoF patterns, we use the term 'pattern' for them for example, Composite pattern instead of Composite constructure principle.

Generic recipes for the adaptation of UML-F annotated variation points allow an efficient definition of more elaborate recipes for patterns and framework variation points. More specific recipes, which rely on and build on the generic recipes, typically add domain or framework-dependent information such as default implementations for the variation points, possible side effects, relevant constraints, and related variation points.



The UML Profile for Framework Architectures
The UML Profile for Framework Architectures
ISBN: 0201675188
EAN: 2147483647
Year: 2000
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net