5.5 Recipe for adapting the Composite pattern

5.5 Recipe for adapting the Composite pattern

Chapter 4 introduced the tags of the Composite pattern. We use the prefix 'C' as a shortcut for Composite in the UML-F tags. Figure 5.6 shows the structure of the pattern and Table 5.5 presents the generic cookbook recipe.

Figure 5.6. The structure of the Composite pattern
graphics/05fig06.gif

Table 5.5. Adaptation of a variation point based on the Composite pattern
Recipe 'Generic adaptation for the Composite pattern'
Intent The recipe describes how to adapt a variation point that is designed according to the Composite pattern. Framework-specific considerations are not provided here.
Classes
  • C Component is the top-level class in the hierarchy, representing a single object or a tree of objects.

  • C Composite provides the management of C Component objects.

  • Usually some specific C Component classes already exist. They are tagged as C Leaf . This recipe describes how to add more of them.

  • See Figure 5.6 for the UML-F class diagram.

Steps to Apply
  1. For each method C op understand its responsibilities.

    • Determine when and where the operation C op is used. Is it used by more than one client method?

  2. Determine whether there is already a (sub)class of C Leaf that provides the desired or a similar functionality. Could that be refined?

  3. If not, define a new subclass of C Component . Provide implementations of the methods marked with C-op if necessary. Determine whether you can reuse the provided default implementation through calling the inherited method. This is only possible if the inherited method is not abstract.

Discussion See the discussion of the Composite pattern in Gamma et al. (1995). Particularly relevant are the following aspects: "Clients can treat composite structures and individual objects uniformly." They do not have to know whether they are dealing with a leaf or a composite component.



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