Flyweight Pattern Tutorial

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

October 14, 2009
Views: 41,500
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Flyweight.
    new diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class. Name it FlyweightFactory.
    flyweight class
  4. Right-click on the FlyweightFactory class and select Add > Operation from the popup menu.
    new flyweight factory oper
  5. Name the operation GetFlyweight(key).
  6. Move the mouse cursor over the FlyweightFactory class and drag out Aggregation > Class to create an aggregated class Flyweight.
    create flyweight
  7. Create an operation in Flyweight, name it Operation, and it takes an argument extrinsicState.
    add oper in flyweight
  8. Right-click on Flyweight and select Model Element Properties > Abstract to set it as abstract.
    set flyweight abstract
  9. Right-click on Operation in Flyweight and select Model Element Properties > Abstract to set it as abstract.
    set flyweight oper abstract
  10. Move the mouse cursor over the Flyweight class and drag out Generalization > Class to create subclasses ConcreteFlyweight and UnsharedConcreteFlyweight.
    create flyweight subclasses
  11. Make ConcreteFlyweight and UnsharedConcreteFlyweight inherit the abstract operations provided by Flyweight by right-clicking on them and selecting Related Elements > Realize all Interfaces from the popup menu.
    realize interface
  12. Add an attribute to ConcreteFlyweight by right-clicking on ConcreteFlyweight and selecting Add > Attribute from the popup menu. Name the attribute intrinsicState.
    add concrete flyweight attribute
  13. Repeat the previous step to add attribute allState to UnsharedConcreteFlyweight.
    add attrs to subclasses
  14. Create a class Client in the empty region of the diagram.
    add client
  15. Use the resource-centric interface to associate Client and FlyweightFactory.
    associate client and flyweight factory
  16. Associate Client with ConcreteFlyweight and UnsharedConcreteFlyweight.
    associate client with concrete subclasses
  17. In practice, there may be multiple ConcreteFlyweight classes. To represent this, stereotype the ConcreteFlyweight class as PTN Cloneable. Right-click on the ConcreteFlyweight class and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete flyweight
  18. In the Stereotypes tab of the class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
    select ptn cloneable
  19. Repeat step 17 and 18 for UnsharedConcreteFlyweight.
    stereotype concrete subclasses
  20. There may be multiple operations in Flyweight. To represent this, stereotype the Flyweight class as PTN Members Creatable. Repeat steps 17 and 18 to stereotype Flyweight as PTN Members Creatable. Up to now, the pattern should look like this:
    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 design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name as Flyweight. Keep the file name as is. Click OK to proceed.
    name pattern

Applying a Design Pattern to a Class Diagram

In this section, we will try to use the flyweight pattern to model a part of a diagram editor.

  1. Create a new project named My Diagram Tool.
  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 design pattern
  4. In the Design Pattern dialog box, select Flyweight from the list of patterns.
    select flyweight
  5. Click on Flyweight in the overview.
    select flyweight in overview
  6. Rename it to Glyph in the bottom pane. Rename operation Operation to Draw, and parameter extrinsicState to gContext.
    rename flyweight
  7. Select ConcreteFlyweight in the overview. In the bottom pane, rename it to Character. Rename operation Operation to Draw, parameter extrinsicState to gContext and attribute intrinsicState to char.
    rename concrete flyweight
  8. Select UnsharedConcreteFlyweight in the overview. In the bottom pane, rename it to Row. Rename operation Operation to Draw, parameter extrinsicState to gContext.
    rename unshared concrete flyweight
  9. Select FlyweightFactory in the overview. In the bottom pane, rename it to GlyphFactory and operation GetFlyweight to GetGlyph.
    rename flyweight factory
  10. Click OK to confirm editing and apply the pattern to the diagram.
  11. Tidy up the diagram. It should look like this:
    result