Visual Paradigm logo
Jump to Menu

Flyweight Pattern Tutorial

This tutorial is aimed to guide the definition and application of 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.

Compatible edition(s): Enterprise, Professional, Standard

  • October 14, 2009
  • Views: 7,546
  • PDF

Modeling

  1. Create a new project Design Patterns.
  2. Create a class diagram Flyweight.
    new diagram
  3. Select Class from diagram toolbar. Click on diagram to create a class. Name it as 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 as Operation and 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 Subject 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 from Flyweight by right clicking on them, and selecting Related Elements > Realize all Interfaces from the popup menu.
    realize interface
  12. Add an operation to ConcreteFlyweight by right clicking on ConcreteFlyweight, and selecting Add > Attribute from the popup menu. Name the attribute intrisicState.
    add concrete flyweight attribute
  13. Repeat the previous step to add attribute allState to UnsharedConcreteFlyweight.
    add attrs to subclasses
  14. Create a class Client at the empty region of the diagram.
    add client
  15. Make use of 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 ConcreteFlyweight class and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete flyweight
  18. In the Stereotypes tab of 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 on UnsharedConcreteFlyweight.
    stereotype concrete subclasses
  20. There maybe 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 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 Flyweight. Keep the file name as it. Click OK to procced.
    name pattern

Application

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

  1. Create a new project My Diagram Tool.
  2. Create a class diagram 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 at the bottom pane. Rename operation Operation to Draw, and parameter extrinsicState to gContext.
    rename flyweight
  7. Select ConcreteFlyweight in overview. At 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 overview. At the bottom pane, rename it to Row. Rename operation Operation to Draw, parameter extrinsicState to gContext.
    rename unshared concrete flyweight
  9. Select FlyweightFactory in overview. At 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 diagram.
  11. Tidy up the diagram. It should become:
    result



Turn every software project into a successful one.