flyweight design pattern

20
Flyweight A STRUCTURAL DESIGN PATTERN Amit B Jambusaria Varun M Chidananda Syracuse University

Upload: varun-mc

Post on 24-May-2015

2.773 views

Category:

Education


0 download

DESCRIPTION

This set of slide decks was prepared by me and Amit for use in demonstrating Flyweight Pattern in our Design Pattern class at SU. You can download the code examples (C++ visual studio projects) from here: http://sdrv.ms/TsvEQl

TRANSCRIPT

Page 1: Flyweight Design Pattern

FlyweightA STRUCTURAL DESIGN PATTERN

Amit B Jambusaria

Varun M Chidananda

Syracuse University

Page 2: Flyweight Design Pattern

Agenda

Motivation Topic Introduction Structure of the Flyweight Real World Examples Code walk through Demo Summary

Page 3: Flyweight Design Pattern

Motivation

Looks alright, right?

Page 4: Flyweight Design Pattern

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.

Page 5: Flyweight Design Pattern

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

Page 6: Flyweight Design Pattern

Real World Example

Page 7: Flyweight Design Pattern

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

Page 8: Flyweight Design Pattern

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.

Page 9: Flyweight Design Pattern

Aircraft Example Lets suppose we have to model some complex aircrafts

such as Airbus 380 and Boeing 797

Page 10: Flyweight Design Pattern

Aircraft

Wingspan

Speed

Capacity

Serial Number

Date bought

Flyweight Object

Wingspan

Speed

Capacity

Page 11: Flyweight Design Pattern

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;

};

Page 12: Flyweight Design Pattern

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 ; }};

Page 13: Flyweight Design Pattern

// 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();

}

Page 14: Flyweight Design Pattern

Dialog box example

Common Objects like Icons and Buttons can be shared among Dialog box classes.

Page 15: Flyweight Design Pattern

Design of Dialog class with Flyweight Design Pattern

Page 16: Flyweight Design Pattern

FileSelection

CommitTransaction

Client:

Go

Stop

Select

Stop

Select

Undo

Stop

Select

Go

Undo

Page 17: Flyweight Design Pattern

Flyweight Factory:

Page 18: Flyweight Design Pattern

Code Walk Through& Demo

Page 19: Flyweight Design Pattern

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)

Page 20: Flyweight Design Pattern

Questions?

Thank You