Você está na página 1de 18

Flyweight Pattern

Submitted by :

Gurtej Singh
Design Patterns

“Each pattern describes a problem that occurs over and over again in our environment, and then
describes the core of the solution to that problem, in such a way that you can use this solution a
million times over, without ever doing it the same way twice”. — Christopher Alexander
Types of Design Patterns
1. Creational
These design patterns are all about class instantiation or
object creation. For ex : Factory Method, Abstract Factory

2. Structural
These are about organizing different classes and objects to
form larger structures and provide new functionality.
For ex : Facade, Flyweight, Private Class Data and Proxy.

3. Behavioral
These are about identifying common communication
patterns between objects and realize these patterns.
For ex : Chain of responsibility, Command, Interpreter, Iterator,
Mediator, Memento, Null Object, Observer, State, Strategy,
Template method, Visitor
● You want to draw a forest of trees!
● How would you implement this?
● Millions of tree objects in the program?
○ Memory constraint
● What is the optimal solution?
● Can we implement it so that we create only 1 tree
Flyweight Pattern
● That’s where flyweight comes in!
● If the objects you create have so much common
properties (like trees, only positions are different).
● The activity diagram may be like this with only one

● Then, we have to store positions somewhere!
○ Client can do this for us.

● Flyweight pattern is a pattern for sharing

objects, where each instance does not contain
its own state but stores it externally.

● This allows efficient sharing of objects to save

space when there are many instances but only
a few different types

● We implement the creation of Attacker and Defender in the

game of Counter Strike.
● So we have 2 classes
○ Attacker
○ Defender
● Whenever a player asks for a weapon we assign him the asked
weapon. In the mission,attacker’s task is to plant a bomb while
the defenders have to diffuse the bomb.
Intrinsic State

● Here ‘player’ is an intrinsic state.

● We can have some other states like their color

or any other properties which are similar for all
the Attackers/Defenders in their respective
Attacker/Defender class.
Extrinsic State

● Attacker / Defender is an extrinsic property.

● Weapon is an extrinsic state since each player

can carry any weapon of his/her choice.

● Weapon need to be passed as a parameter by

the client itself.
Class Diagram
● Run time costs
○ Client is responsible for extrinsic states
(storing, fetching etc)
○ Normally they are part of the object.
● Space saving
○ Will increase as more flyweights are shared.
How much you share, that much you save
● Factory
○ Flyweight must be created only via factory!
Implementation Issues

● Extrinsic states can be stored but what about we

want to change the intrinsic state of a flyweight?
○ Modify the factory as to create new flyweights with
a different intrinsic state also.
○ Not so efficient, slowly breaking the flyweight
● UnsharedConcreteFlyweight is normally a totally
different object but we can also make them sharable
via factory.
○ If they become sharable later, the oldly created
ones should be added to factory

1. https://en.wikipedia.org/wiki/Flyweight_pattern
2. GoF Design Patterns Book
3. Definition from: C# design patterns, James cooper
4. http://www.dofactory.com/Patterns/PatternFlyweight.
5. http://fc07.deviantart.net/fs32/f/2008/218/8/7/Pine_fo
6. Oreilly Head First: Design Patterns Book
7. Fry of Futurama for Questions Picture
8. Demo: http://www.javacamp.org/designPattern/flyweight.html
9. http://javatechniques.com/public/java/docs/basics/stri
Thank You !