flyweight: structural pattern prepared by galina walters

11
Flyweight: Structural Pattern Prepared by Galina Walters

Upload: albert-quinn

Post on 16-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Flyweight: Structural Pattern Prepared by Galina Walters

Flyweight: Structural Pattern

Prepared by Galina Walters

Page 2: Flyweight: Structural Pattern Prepared by Galina Walters

Flyweight pattern

describes how to share objects to allow their use at fine granularities without prohibitive cost. A flyweight is a shared object that can be used in multiple contexts simultaneously.

Page 3: Flyweight: Structural Pattern Prepared by Galina Walters

Name– Flyweight

Intent– Use sharing to support large numbers of fine-grained objects efficiently

Context– Desire to Use Objects Throughout Design

• Including the Finest Level of Granularity• Expensive if the Objects are not Shared

– Desire to Reduce System Footprint• Memory is Limited

Problem– Placing the same information in many different place

• Mistakes can be made in copying the information • If the data changes, all occurrences of the information must be located and changed. This

makes maintenance of the document difficult • Documents can become quite large if the same information is repeated over and over

again

– Issues• RAM Overhead Associated with each Instance• CPU Overhead Associated with Memory Management• CPU Overhead Associated with Equality Comparison

Page 4: Flyweight: Structural Pattern Prepared by Galina Walters

Solution Create “Flyweight” Objects for Intrinsic State

– Intrinsic State - Information independent of the object’s context, sharable (e.g. State might include name, postal abbreviation, time zone, etc.). This is included in the Flyweight and instead of storing the same information n times for n objects, it is only stored once

– Extrinsic State - Information dependent on the object’s context, unsharable, stateless, having no stored values, but values that can be calculated on the spot (e.g. State might need access to region). This is excluded from the Flyweight

“Flyweight” Objects– Shared, Pooled Instances

– Used in Multiple Contexts Simultaneously

– Indistinguishable From Non-Shared Instance

– Cannot Make Assumptions about Context

“Flyweight” Requires Special Coding– Deserialization Must Replace Objects as Loaded

– Copying an Instance Must Return the Instance

Page 5: Flyweight: Structural Pattern Prepared by Galina Walters

Example: OO Document Editor Using an object for each character in the document:

– Promote flexibility at the finest levels in the application– Characters and embedded elements could be treated uniformly with respect to how they

are drawn and formatted– The application could be extended to support new character sets without disturbing other

functionality– The application’s object structure could mimic the document’s physical structure logically: physically:

Creating a flyweight for each letter of the alphabet:

– Intrinsic state: a character code – Extrinsic state: coordinate position in the document

» typographic style (font, color)» is determined from the text layout algorithms and formatting commands

in effect wherever the character appears

Page 6: Flyweight: Structural Pattern Prepared by Galina Walters

Flyweight Pattern Participants The Flyweight

– is the set of intrinsic information that a set of objects share in common. It is abstract

The ConcreteFlyweight – is a subclass of the Flyweight that contains all of its information, as well as how to

calculate extrinsic information for a particular (yet arbitrary) object. It is shared among more than one object

The FlyweightFactory – serves to dispense particular flyweights requested. When a Flyweight with certain

properties is requested, it checks to see if one already exists, and if so, returns that flyweight. If the requested flyweight does not exist, it creates the required flyweight, stores it, and then returns it

The Client– in creating a new object must assign a flyweight to it, so it asks the

FlyweightFactory for a particular flyweight, receives that flyweight, and creates a reference to it in the object it is creating.

Page 7: Flyweight: Structural Pattern Prepared by Galina Walters

Flyweight Pattern Structure

Page 8: Flyweight: Structural Pattern Prepared by Galina Walters

public class Client extends Object /* Uses the Flyweight Factory to create the flyweight it needs. When the Client needs the flyweight to do something, it calls the

appropriate method of the flyweight, passing it any extrinsic state information it might need as its context */

private Flyweight flyweight private FlyweightFactory flyweightFactory public abstract class Flyweight extends Object /*The abstract Flyweight class. The concrete subclasses may or may not be shared. */

public abstract Object doSomething(Object context) /* The task the flyweight performs. */

Parameters: context - The extrinsic state information needed to perform the task. Returns: The appropriate result.

public class ConcreteFlyweightA extends Flyweight /* A concrete flyweight for a specific class of tasks. */

public Object doSomething(Object context) /* The task this flyweight performs. */

Parameters: context - The extrinsic state information needed to perform the task. Returns: The appropriate result.

public class FlyweightFactory extends Object /* Maintains a pool of instantiated flyweights. When the Client requests a shareable flyweight, the factory first searches the pool to see if there is one and returns that instance. Otherwise it instantiates a new flywieght,adds it to the pool and returns a reference to the client. */

flyweightPool private Vector flyweightPool /* A pool of instantiated shared flyweights. These are the only instances needed for these specific flyweights. */

makeFlyweight public Flyweight makeFlyweight(Object parameter)

/* Search the pool for a shared flyweight as per the given parameter. Return either an existing instance or create a new one if

there is none already. */

Parameters: parameter - Used to determine which concrete flyweight is needed. Returns: Either an existing or new concrete instance.

Page 9: Flyweight: Structural Pattern Prepared by Galina Walters

Consequences “Flyweights” Lose Equality, Gain Identity

– Same Instance used Many Places

“Flyweights” Must Separate Intrinsic/Extrinsic– “Flyweight” Representing a Node in a Graph Cannot Point to Parent (Parent must

be Passed)

“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)

– Computing Extrinsic State (Previously Stored)

– Transferring the Extrinsic information

Page 10: Flyweight: Structural Pattern Prepared by Galina Walters

Flyweight is not for everything…

The Flyweight pattern should not be used if the shared information can vary over time. This would increase the maintenance burden of the document

Readability of the document can suffer if the Flyweight pattern is used, readers are forced to reference a different section of the document when looking at the contents

Page 11: Flyweight: Structural Pattern Prepared by Galina Walters

References

1. http://hillside.net/patterns/DPBook/0-width.pdf

2. http://exciton.cs.rice.edu/JavaResources/DesignPatterns/FlyweightPattern.htm

3. http://www.xml.com/pub/a/2000/01/19/feature/?page=3

4. http://www.xmlpatterns.com/FlyweightMain.shtml

5. http://www.ciol.com/content/technology/sw_desg_patt/101080901.asp

6. http://www.cis.ksu.edu/~hankley/d644/Patterns/Flighweight/FlyPattern.html

7. www.microgold.com/version2/articles/article2/article2.html

8. www.cijug.org/presentation/CIJUG-Patterns.ppt