Docsity

Flyweight Pattern: Efficient Management of Small Objects in Programming - Prof. Atif M. Me, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Flyweight Pattern
Apr. 05, 2007
2
Example
• We want to draw a small folder
icon with a name under it for
each person in a an
organization.
• We want two types of icons,
one for “is Selected” and one
for “not Selected.”
• We can have an icon objec t for
each person, with its own
coordinates, name and selected
state.
– Each icon can then draw()
itself.
• Lets code it.
3
Efficiency Issues
• If this is a large organization, there could be a
large number of such icons, but they are actually
all the same graphical image.
• Even if we have two icons, one for “is Selected”
and one for “not Selected” the number of different
icons is small.
• In such a system, having an icon object for each
person, with its own coordinates, name and
selected state is a waste of resources.
4
A “better” Approach
• Instead, we’ll create a
FolderFactory that returns
either the selected or the
unselected folder drawing
class, but does not create
additional instances once
one of each has been
created.
• Since this is such a simple
case, we just create them
both at the outset and then
return one or the other.
pf3
pf4

Partial preview of the text

Download Flyweight Pattern: Efficient Management of Small Objects in Programming - Prof. Atif M. Me and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Flyweight PatternSpring 2007

Apr. 05, 2007

Example

organization.each person in a anicon with a name under it forWe want to draw a small folder

for “not Selected.”one for “is Selected” and oneWe want two types of icons,

  • state.coordinates, name and selectedeach person, with its ownWe can have an icon object for itself.^ Each icon can then draw()

Lets code it. 3

Efficiency Issues

all the same graphical image.large number of such icons, but they are actuallyIf this is a large organization, there could be a

icons is small.and one for “not Selected” the number of differentEven if we have two icons, one for “is Selected”

selected state is a waste of resources.person, with its own coordinates, name andIn such a system, having an icon object for each

A “better” Approach

created.one of each has beenadditional instances onceclass, but does not createunselected folder drawingeither the selected or theFolderFactory that returnsInstead, we’ll create a

return one or the other.both at the outset and thencase, we just create themSince this is such a simple

5

More Complex Cases

weren’t already in the table.already created and only create new ones if theyfactory could keep a table of the ones it hadFor cases where more instances could exist, the

Flyweight Pattern

name to be drawn into the folder when we draw it.example is that we pass the coordinates and theThe unique thing about using Flyweights in this

These coordinates are the

extrinsic

data that allow

– an instance’screate only two instances.us to share the folder objects, and in this case

intrinsic

data makes the instance unique,

and the

extrinsic

data is passed in as arguments.

7

The Folder class

folder at the point you specify.other and has a public Draw method that draws thefolder instance with one background color or theWe’ll develop a folder class that simply creates a

What is it?

generate a very large number of small class instances to represent data.There are cases in programming where it seems that you need to

fundamentally the same except for a few parameters.you need to instantiate if you can recognize that the instances areSometimes you can greatly reduce the number of different classes that

be greatly reduced.them in as part of a method call, the number of separate instances canIf you can move those variables outside the class instance and pass

classes.The Flyweight design pattern provides an approach for handling such

It refers to the instance’s (^) intrinsic (^) data that makes the instance unique, and the (^) extrinsic (^) data which is passed in as arguments.

individual characters or icons on the screen.The Flyweight is appropriate for small, fine-grained classes like