Flyweight: Structural Pattern Prepared by Galina Walters.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Procedures of Extending the Alphabet for the PPM Algorithm Radu Rădescu George Liculescu Polytechnic University of Bucharest Faculty of Electronics, Telecommunications.
Computer Science 313 – Advanced Programming Topics.
Plab – Tirgul 12 Design Patterns
Imagine that you need to create a system to represent all of the cars in a city. You need to store the details about each car ( model, and year) and the.
VBA Modules, Functions, Variables, and Constants
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Flyweight An Object Structural Design Pattern JM Imbrescia.
Design Patterns CS is not simply about programming
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Algorithm Programming Structural Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Katie C. O’Shea Dennis T. Tillman 11 February 2K2 Flyweight.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
 Missing (or duplicate) semicolons can make the browser completely ignore the style rule.  You may add extra spaces between the properties/values to.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
A First Program Using C#
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
Strategy Design Patterns CS 590L - Sushil Puradkar.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
GoF: Document Editor Example Rebecca Miller-Webster.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns V More Structural Patterns.
02 - Structural Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Team 6 “The Unparseables” Design Patterns Chain of Responsibility Observer Flyweight 1.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Patterns Introduction
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Embedded Computer - Definition When a microcomputer is part of a larger product, it is said to be an embedded computer. The embedded computer retrieves.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Examples (D. Schmidt et al)
Factory Patterns 1.
Behavioral Design Patterns
Understanding File Management
Software Design and Architecture
UNIT 3 – LESSON 5 Creating Functions.
Flyweight Design Pattern
Intent (Thanks to Jim Fawcett for the slides)
Object Pool Pattern 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Singleton Pattern Pattern Name: Singleton Pattern Context
Flyweight Pattern 1.
Exercise 9 Skills You create and use styles to create formatting rules that can easily by applied to other pages in the Web site. You can create internal.
Invitation to Computer Science 5th Edition
Presentation transcript:

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.

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

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

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

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.

Flyweight Pattern Structure

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.

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

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

References