FlyweightA STRUCTURAL DESIGN PATTERN
Amit B Jambusaria
Varun M Chidananda
Syracuse University
Agenda
Motivation Topic Introduction Structure of the Flyweight Real World Examples Code walk through Demo Summary
Motivation
Looks alright, right?
Introduction
Designing objects down to the lowest levels of system granularity provides optimal flexibility, but can be unacceptably expensive in terms of performance and memory usage.
The Flyweight Pattern is a pattern for greatly reducing memory requirements.
The flyweight pattern applies to a program using a huge number of objects that have part of their internal state in common where the other part of state can vary.
The idea behind flyweight pattern is shareable objects.
Structure of the Flyweight
It can be confusing at first to understand how the flyweight
pattern works. Let’s first take a high-level look at the structure.
Each object is divided into two pieces:
-- the state-independent (intrinsic) part
-- and the state-dependent (extrinsic) part
Intrinsic state is stored (shared) in the Flyweight object.
Extrinsic state is stored or computed by client objects, and
passed to the Flyweight when its operations are invoked.
stud
ent
SUID
First Name
Last Name
Department
University Name
Real World Example
Word Processor
A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a word processor.
The term flyweight pattern was first coined and extensively explored by Paul Calder and Mark Linton in 1990 to efficiently handle glyph information in a WYSIWYG document editor.
For each character in a document, we can create a glyph object containing information such as the font family, size, weight of each character and other formatting data.
The problem here is that a lengthy document might contain tens of thousands of characters and corresponding objects - quite a memory killer !!!
The Flyweight pattern addresses the problem by creating a new object to store such information, which is shared by all characters with the same formatting.
The intrinsic state here could be the shared information such as font-family, font-size, font weight and so one.
The extrinsic state would be the information that distinguishes each physical character such as row information or the x-y co-ordinate of the character.
So, if I had a ten-thousand word document, with 800 characters in Bold Times-New-Roman size-14pt, these 800 characters would contain a reference to a flyweight object that stores their common formatting information.
The key here is that you only store the information once, so memory consumption is greatly reduced.
Aircraft Example Lets suppose we have to model some complex aircrafts
such as Airbus 380 and Boeing 797
Aircraft
Wingspan
Speed
Capacity
Serial Number
Date bought
Flyweight Object
Wingspan
Speed
Capacity
public class Boeing797 : public IModel
{
private:
std::string name;
std::string wingspan;
int capacity;
std::string speed;
std::string range;
public:
Boeing797 ()
{
name = "Boeing797";
wingspan = "79.8 meters";
capacity = 555;
speed = "912 km per hr";
range = "10370 km";
}
void print ()
{
cout << "Name : " << name << "\n";
cout << "Wingspan : " << wingspan << "\n";
cout << "Capacity : " << capacity << "\n";
cout << "Speed : " << speed << "\n";
}
};
public class Airbus380 : public IModel
{
private:
std::string name;
std::string wingspan;
int capacity;
std::string speed;
std::string range;
public: Airbus380()
{
name = "AirBus380";
wingspan = "80.8 meters";
capacity = 1000;
speed = "1046 km per hr";
range = "14400 km";
}
void print ()
{
cout << "Name : " << name << "\n";
cout << "Wingspan : " << wingspan << "\n";
cout << "Capacity : " << capacity << "\n";
cout << "Speed : " << speed << "\n";
}
};
public class IModel
{
private:
std::string name;
std::string wingspan;
int capacity;
std::string speed;
std::string range;
public: virtual void print () = 0;
};
public class FlyweightFactory {
private:
static Airbus380 *a;
static Boeing797 *b;
public:
static IModel * GetObj (int typeNo) {
if (typeNo == 380) {
if(a == NULL)
a = new Airbus380();
return a;
}
else { //if typeNo is 797
if(b == NULL)
b = new Boeing797();
return b;
}
}
};
public class Aircraft { private:
IModel *_type;int _serialNo;std::string _dateBought;
public:Aircraft (IModel *type, int
serialNo, std::string dateBought)
{_type = type ;_serialNo = serialNo ;_dateBought = dateBought ;
}
void print() { _type->print(); cout << "Serial No : " << _serialNo << "\n"; cout << "Date Bought : " << _dateBought << "\n";
cout << endl ; }};
// Global Static object pointer
Airbus380 * FlyweightFactory::a;
Boeing797 * FlyweightFactory::b;
// Client:
void main()
{
Aircraft one = Aircraft( FlyweightFactory::GetObj(380) , 1001,"9th feb 2011");
Aircraft two = Aircraft( FlyweightFactory::GetObj(380) , 1002, "11th sept 2011");
Aircraft three = Aircraft( FlyweightFactory::GetObj(797) , 1003, "12th oct 2011");
Aircraft four = Aircraft( FlyweightFactory::GetObj(797) , 1004, "13th dec 2011");
one.print();
two.print();
three.print();
four.print();
}
Dialog box example
Common Objects like Icons and Buttons can be shared among Dialog box classes.
Design of Dialog class with Flyweight Design Pattern
FileSelection
CommitTransaction
Client:
Go
Stop
Select
Stop
Select
Undo
Stop
Select
Go
Undo
Flyweight Factory:
Code Walk Through& Demo
Summary• “Flyweights” Lose Equality, Gain Identity• Same Instance used Many Places
• “Flyweights” Must Separate Intrinsic/Extrinsic
• “Flyweights” Save Space• More “Flyweights” Shared Yields More Savings• More Intrinsic State Yields More Savings
• “Flyweights” Are Found, Not Instantiated• Slight Shift in Terminology
• “Flyweights” May Introduce CPU Costs• Finding “Flyweights” (Large Pool Increases)
Questions?
Thank You