flyweight pattern

21
Flyweight Pattern Structural Pattern

Upload: hueseyin-ergin

Post on 24-May-2015

1.971 views

Category:

Technology


0 download

DESCRIPTION

Based on GoF Book

TRANSCRIPT

Page 1: Flyweight Pattern

Flyweight Pattern

Structural Pattern

Page 2: Flyweight Pattern

2

Motivation 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 object?

Page 3: Flyweight Pattern

3

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

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

Page 4: Flyweight Pattern

4

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

Page 5: Flyweight Pattern

5

Text Document Example You want to hold a text document with so

many customizations as style. Each letter in with a different object? Creating the letter ‘E’, may be already there

before,but with a different style!

So we will have aCharFactory to returnus the already createdinstance OR createsit from scratch!

Page 6: Flyweight Pattern

6

Structure

Page 7: Flyweight Pattern

7

Participants Flyweight

Letter as an abstract class SharedConcreteFlyweight

LetterA UnsharedConcreteFlyweight

Not all flyweights must be shared! Row information for example

FlyweightFactory Creates and manages flyweights (Character

factory) Client

The styles are stored here, also call to factory

Page 8: Flyweight Pattern

8

Intrinsic & Extrinsic States From course slides (7.Quality of Design) B is extrinsic to A if A can be fully defined with

no notion of B E.g., class Dog is extrinsic to class Person

B is intrinsic to A if B captures some characteristics inherent to A E.g., class Date is intrinsic to class Person (by date

of birth) What is with us?

Extrinsic states may be stored in client! Intrinsic states may be stored in flyweight itself!

Page 9: Flyweight Pattern

9

Sequence Diagram

Page 10: Flyweight Pattern

10

Applicability Use flyweight pattern iff:

You will use large number of objects. You want to reduce storage costs. Most of object states can be extrinsic. Many group of objects > Fewer shared objects

with states App doesnot care object identity.

Ex: Drawing lots of circleswith different colors and sizes

Page 11: Flyweight Pattern

11

Consequences 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 space!

Factory Flyweight must be created only via factory!

Page 12: Flyweight Pattern

12

Demo Circle Drawing. (ref at the end) We want to draw10000 circles in 6 different

colors. Circles have color in common. (intrinsic state) Position and radius information is given as

extrinsic state. (randomly generated)

Page 13: Flyweight Pattern

13

Implementation Issues Storing extrinsic states can be tricky

An object structure for extrinsic states In text document example, you can store a map of

typographic information in seperate structure, instead of storing the font and style information with each object.

This map can keep track of numbers of characters with same attribute.

Accessing a specific object We have to design the system

according to that need. In drawing tree example,

we just draw and go. We can make the access

with a pointer.

Page 14: Flyweight Pattern

14

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

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

Page 15: Flyweight Pattern

15

Relation with Other Patterns You can combine Composite pattern with

Flyweight. Each UnsharedConcreteFlyweight may be

component with SharedConcreteFlyweights as children.

In text document:Rows are componentsor UnsharedConcFly.Letters are leafsor SharedConcFly.

Page 16: Flyweight Pattern

16

Relation with Other Patterns – cont’d FlyweightFactory produces flyweights as in Factory Method.

No abstract Creator (but we can) FlyweightFactory is the ConcreteCreator. Flyweight is Product. Subclasses of Flyweight is ConcreteProducts.

It only looks for an already created instance instead of returning new ConcreteProduct() immediately.

Page 17: Flyweight Pattern

17

Relation with Other Patterns – cont’d In State Pattern, you can share states with

Flyweight Pattern. Flyweight is State. SharedConcreteFlyweight is ConcreteState. Client is Context.

With that way, Context can share states if the state is already created. Handle method may

also get extrinsicstate as parameter.

Page 18: Flyweight Pattern

18

Relation with Other Patterns – cont’d The same logic works with the Strategy

Pattern. Strategy can be shared, and algorithm can be run

with only different inputs (extrinsic state). The algorithm itself is the intrinsic state.

Difference is: flyweights can be used in many context but strategy is in one context.

Page 19: Flyweight Pattern

19

Practical Example Strings in Java language.

If you don’t explicitly construct a string and put the value with assignment operator, they will be same.

Actually Java has another method called intern for sharing strings.

Page 20: Flyweight Pattern

20

Thanks

Questions?

Page 21: Flyweight Pattern

21

References GoF Design Patterns Book Definition from: C# design patterns, James

cooper http://www.dofactory.com/Patterns/PatternFlyweig

ht.aspx#_self1

http://fc07.deviantart.net/fs32/f/2008/218/8/7/Pine_forest_by_softwalls.jpg

Oreilly Head First: Design Patterns Book Fry of Futurama for Questions Picture Demo: http://

www.javacamp.org/designPattern/flyweight.html http://javatechniques.com/public/java/docs/basics

/string-equality.html