Composite Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) composite design pattern. By reading this tutorial, you will know how to develop a model for the composite pattern, and how to apply it in practice.

October 7, 2009
Views: 48,228
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Composite.
    new diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it Client.
    create client
  4. Move the mouse cursor over the Client class and drag out Association > Class to create an associated class named Component.
    create component
  5. Right-click on Component and select Model Element Properties > Abstract to set it as abstract.
    set component abstract
  6. Right-click on the Component class and select Add > Operation from the popup menu.
    new component oper
  7. Name the operation Operation().
  8. Right-click on Operation and select Model Element Properties > Abstract to set it as abstract.
    set oper abstract
  9. Repeat steps 6 to 8 to create the operations Add(component : Component), Remove(component : Component), and GetChild(index : int) : Component.
    add component opers
  10. Move the mouse cursor over the Component class and drag out Generalization > Class to create a subclass named Leaf. Repeat this step to create another subclass named Composite from Component.
    create sub classes
  11. Leaf and Composite inherit the operations from Component. Select both Leaf and Composite, then right-click on them and select Related Elements > Realize all Interfaces from the popup menu.
    realize interfaces
  12. Move the mouse cursor over the Component class and drag out Composition > Class to Component. Name the Component's role "children."
    composition created
  13. In practice, there may be multiple operations in Component. To represent this, stereotype the Component class as PTN Members Creatable. Right-click on Component and select Stereotypes > Stereotypes... from the popup menu.
    set component stereotypes
  14. In the Stereotypes tab of the Class Specification dialog box, select PTN Members Creatable and click > to assign it to the Component class. Click OK to confirm.
    set ptn members creatable
    Up to now, the diagram should look like this:
    composite pattern modeled

Defining a Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    define pattern
  3. In the Define Design Pattern dialog box, specify the pattern name as Composite. Keep the file name as is and click OK to proceed.
    name composite pattern

Applying a Design Pattern to a Class Diagram

In this section, we will apply the composite pattern to model a furniture shop's furniture catalog.

  1. Create a new project named Furniture Shop.
  2. Create a class diagram named Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply pattern
  4. In the Design Pattern dialog box, select Composite from the list of patterns.
    select composite
  5. Click on Component in the overview.
    select component
  6. Rename Component to Furniture, and the "component" parameters in various operations to "furniture" in the bottom pane.
    rename component
  7. Rename Operation to ShowPrice.
    rename operation
  8. Besides the ShowPrice operation, we also need one more for ShowId. Keep Component selected, click the + button in the bottom pane, and select New Operation... from the popup menu.
    new oper
  9. In the Operation Specification dialog box, name the operation ShowId. Check the Abstract box at the bottom of the dialog.
    add show id
  10. Select Leaf in the overview and rename it to Chair in the bottom pane. Also, rename the Operation operation to ShowPrice. Note that if the Auto Rename option is on, renaming the operation is not necessary as this will be done automatically.
    rename leaf
  11. Select Composite in the overview and rename it to FurnitureSet in the bottom pane. Also, rename Operation to ShowPrice. Click OK to apply the pattern to the diagram.
    rename composite
    This is the result:
    pattern applied