Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Flyweight.
- Select Class from diagram toolbar. Click on diagram to create a class. Name it as 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 as Operation and 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 Subject class, and drag out Generalization > Class to create subclasses ConcreteFlyweight and UnsharedConcreteFlyweight.
- 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.
- Add an operation to ConcreteFlyweight by right clicking on ConcreteFlyweight, and selecting Add > Attribute from the popup menu. Name the attribute intrisicState.
- Repeat the previous step to add attribute allState to UnsharedConcreteFlyweight.
- Create a class Client at the empty region of the diagram.
- Make use of 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 ConcreteFlyweight class and select Stereotypes > Stereotypes... from the popup menu.
- In the Stereotypes tab of class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
- Repeat step 17 and 18 on UnsharedConcreteFlyweight.
- 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:
Defining 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 Flyweight. Keep the file name as it. Click OK to procced.
Applying Design Pattern on Class Diagram
In this section, we will try to make use of the flyweight pattern to model a part of a diagram editor.
- Create a new project My Diagram Tool.
- Create a class diagram 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 at the bottom pane. Rename operation Operation to Draw, and parameter extrinsicState to gContext.
- 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.
- Select UnsharedConcreteFlyweight in overview. At the bottom pane, rename it to Row. Rename operation Operation to Draw, parameter extrinsicState to gContext.
- Select FlyweightFactory in overview. At the bottom pane, rename it to GlyphFactory and operation GetFlyweight to GetGlyph.
- Click OK to confirm editing and apply the pattern to diagram.
- Tidy up the diagram. It should become: