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.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Flyweight.

- Select Class from the diagram toolbar. Click on the diagram to create a class. Name it FlyweightFactory.

- Right-click on the FlyweightFactory class and select Add > Operation from the popup menu.

- Name the operation GetFlyweight(key).
- Move the mouse cursor over the FlyweightFactory class and drag out Aggregation > Class to create an aggregated class Flyweight.

- Create an operation in Flyweight, name it Operation, and it takes an argument extrinsicState.

- Right-click on Flyweight and select Model Element Properties > Abstract to set it as abstract.

- Right-click on Operation in Flyweight and select Model Element Properties > Abstract to set it as abstract.

- Move the mouse cursor over the Flyweight class and drag out Generalization > Class to create subclasses ConcreteFlyweight and UnsharedConcreteFlyweight.

- 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.

- Add an attribute to ConcreteFlyweight by right-clicking on ConcreteFlyweight and selecting Add > Attribute from the popup menu. Name the attribute intrinsicState.

- Repeat the previous step to add attribute allState to UnsharedConcreteFlyweight.

- Create a class Client in the empty region of the diagram.

- Use the resource-centric interface to associate Client and FlyweightFactory.

- Associate Client with ConcreteFlyweight and UnsharedConcreteFlyweight.

- 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.

- In the Stereotypes tab of the class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.

- Repeat step 17 and 18 for UnsharedConcreteFlyweight.

- 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:

Defining a Pattern
- Select all classes on the class diagram.

- Right-click on the selection and select Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Flyweight. Keep the file name as is. Click OK to proceed.

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.
- Create a new project named My Diagram Tool.
- Create a class diagram named Domain Model.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Flyweight from the list of patterns.

- Click on Flyweight in the overview.

- Rename it to Glyph in the bottom pane. Rename operation Operation to Draw, and parameter extrinsicState to gContext.

- 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.

- Select UnsharedConcreteFlyweight in the overview. In the bottom pane, rename it to Row. Rename operation Operation to Draw, parameter extrinsicState to gContext.

- Select FlyweightFactory in the overview. In the bottom pane, rename it to GlyphFactory and operation GetFlyweight to GetGlyph.

- Click OK to confirm editing and apply the pattern to the diagram.
- Tidy up the diagram. It should look like this:
