The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1.

Slides:



Advertisements
Similar presentations
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Advertisements

Tutorial 12: Enhancing Excel with Visual Basic for Applications
1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Flyweight: Structural Pattern Prepared by Galina Walters.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Flyweight An Object Structural Design Pattern JM Imbrescia.
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.
Katie C. O’Shea Dennis T. Tillman 11 February 2K2 Flyweight.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Lecture 2: Classes and Objects, using Scanner and String.
Classes CS 21a: Introduction to Computing I First Semester,
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
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.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
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.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Working With Objects Tonga Institute of Higher Education.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Bridge Pattern (Structural) “All problems in computer science can be solved by another level of indirection.” – Butler Lampson ©SoftMoore ConsultingSlide.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Singleton Pattern (Creational)
Written by: Dr. JJ Shepherd
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Software Design and Architecture
Flyweight Design Pattern
Java Review: Reference Types
Decorator Intent Also known as Wrapper Example: a Text Window
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Units with – James tedder
Flyweight Pattern 1.
Introduction to Data Structure
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Presentation transcript:

The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1

Motivation Some applications would benefit from using objects, but a naive implementation would create thousands of small objects, which would consume lots of memory and incur unacceptable run-time overhead. Example: Using an object-oriented approach to implementing a document editor, should we use objects to represent each character in the document? The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive costs. ©SoftMoore ConsultingSlide 2

Intrinsic versus Extrinsic State A flyweight is a shared object that can be used in multiple contexts simultaneously. Flyweights cannot make assumptions about the context in which they operate. The state of an object is the complete set of all attribute values of that object at a point in time. A flyweight object can have both intrinsic state and extrinsic state. Intrinsic State –independent of the flyweight’s context –stored in the flyweight Extrinsic State –depends on and varies with the object’s context –cannot be shared (cannot be stored in the flyweight) ©SoftMoore ConsultingSlide 3

Intrinsic versus Extrinsic State (continued) Example: A document editor can create a flyweight for each letter of the alphabet. The character code is intrinsic state and is stored as part of the flyweight object for the character. The coordinate position of the character within the document and its typographic style are extrinsic. They can be computed from the text layout algorithms and formatting commands in effect wherever the character appears. ©SoftMoore ConsultingSlide 4

Flyweight Pattern Intent: Use sharing to support large numbers of fine-grained objects efficiently. Applicability: Apply the Flyweight pattern when all of the following are true: –An application uses a large number of objects. –Storage costs are high because of the sheer quantity of objects. –Most object state can be made extrinsic. –Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed. –The application doesn’t depend on object identity. Since flyweight objects are shared, identity tests will return true for conceptually distinct objects. Slide 5©SoftMoore Consulting

Flyweight Pattern (continued) ©SoftMoore ConsultingSlide 6 Client Structure Flyweight operation(extrinsicState) UnsharedConcreteFlyweight intrinsicState extrinsicState operation(extrinsicState) ConcreteFlyweight intrinsicState operation(extrinsicState) * FlyweightFactory getFlyweight(key) if (flyweight[key] exists) return existing flyweight else create new flyweight add new flyweight to pool return new flyweight

Flyweight Pattern (continued) Participants Flyweight –declares an interface through which flyweights can receive and act on extrinsic state. ConcreteFlyweight –implements the Flyweight interface and adds storage for intrinsic state (if needed). –shareable because it does not store an extrinsic state. UnsharedConcreteFlyweight –implements the Flyweight interface. –stores both intrinsic and extrinsic state. –not shareable. Slide 7©SoftMoore Consulting

Flyweight Pattern (continued) Participants (continued) FlyweightFactory –creates and manages Flyweight objects. –ensures that flyweights are shared properly. –FlyweightFactory is essentially an Object Cache as discussed in the section on the Object Pool pattern. Client –maintains references to Flyweights. –computes or stores the extrinsic states of Flyweights Slide 8©SoftMoore Consulting

Flyweight Pattern (continued) Collaborations The state that a flyweight needs must be characterized as either intrinsic or extrinsic. Intrinsic state is stored with the ConcreteFlyweight object. Extrinsic state is stored or computed by client objects. Clients pass this extrinsic state to a flyweight when they invoke its operations. Clients should not instantiate ConcreteFlyweight objects directly. Clients should obtain ConcreteFlyweight objects from the FlyweightFactory to ensure that they are shared properly. ©SoftMoore ConsultingSlide 9

Flyweight Pattern (continued) Consequences Flyweights may introduce run-time costs associated with transferring, finding, and/or computing extrinsic state. Such costs should be offset by space savings, which increase as more flyweights are shared. Storage savings are a function of several factors: –the reduction in the total number of instances that comes from sharing –the amount of intrinsic state per object –whether extrinsic state is computed or stored If stored in a hierarchical structure (composite), flyweight leaf nodes cannot store a reference to their parent. ©SoftMoore ConsultingSlide 10

Flyweight Pattern (continued) Implementation The pattern’s applicability is determined largely by how easy it is to identify extrinsic state and remove it from shared objects. Ideally extrinsic state can be computed from a separate object, one with far smaller storage requirements. Since flyweights are shared, clients shouldn’t instantiate them directly. FlyweightFactory lets clients locate a particular flyweight. FlyweightFactory often uses an associative store (map) to let clients look up flyweights of interest. ©SoftMoore ConsultingSlide 11

Immutable Class A class is said to be immutable if its instances cannot be modified after construction. An immutable object is an instance of an immutable class. Guidelines for making a class immutable –Don’t provide methods that modify the object’s state. –Ensure that the class can’t be extended (e.g., make it final). –Make all fields private and final. Examples in Java –String –Wrapper classes (Integer, Double, Character, etc.) –BigInteger ©SoftMoore ConsultingSlide 12

Using Immutable Objects Immutable objects can be freely shared without the need to clone or copy. A client of an immutable object cannot make changes that affect other clients. Immutable objects are inherently thread-safe. They perform correctly when accessed from multiple threads with no additional synchronization or other coordination on the part of their clients. ©SoftMoore ConsultingSlide 13 “Classes should be immutable unless there is a very good reason to make them mutable” – Joshua Bloch

Flyweights and Immutable Objects Since multiple clients share access to flyweights, when one client changes the state of the flyweight, every client is affected by that change. If the flyweights are immutable, then they can be shared freely – clients cannot make changes that affect one another. Shared flyweights (flyweights with only the intrinsic state) should generally be immutable. Shared flyweights that are not immutable require special consideration to ensure that changes by one client don’t adversely affect other clients. ©SoftMoore ConsultingSlide 14

Flyweight Pattern in Java The factory method Boolean.valueOf(boolean) returns a shared instance – essentially a flyweight. Since strings in Java are immutable, they are easily shared. In Java, strings specified at compile-time are flyweights; i.e., strings with the same sequence of characters are automatically shared. String s1 = "flyweight"; String s2 = "flyweight"; System.out.println(s1 == s2); // prints true ©SoftMoore ConsultingSlide 15

Flyweight Pattern in Java (continued) The GUI classes in Java Swing provide a border factory that creates sharable flyweight borders. Border b1 = BorderFactory.createRaisedBevelBorder(); Border b2 = BorderFactory.createRaisedBevelBorder(); System.out.println(s1 == s2); // prints true ©SoftMoore ConsultingSlide 16

Example: Using Java Enums to Implement Flyweights public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); Coin (int coinValue) { this.coinValue = coinValue; } public int coinValue() { return coinValue; } private final int coinValue; } ©SoftMoore ConsultingSlide 17

“Faking” Java Enums (Prior to Version 5 of Java) public final class Coin { private final int coinValue; public static final Coin PENNY = new Coin(1); public static final Coin NICKEL = new Coin(5); public static final Coin DIME = new Coin(10); public static final Coin QUARTER = new Coin(25); Coin (int coinValue) { this.coinValue = coinValue; } ©SoftMoore ConsultingSlide 18 (continued on next slide)

“Faking” Java Enums (continued) public int coinValue() { return coinValue; } ©SoftMoore ConsultingSlide 19

Value Types (Note: Not yet implemented in Java) Proposed future enhancements to the JVM and Java language include support for small, immutable, identityless value types. As envisioned, value types will combine the properties of both classes and primitive types. –live on the runtime stack rather than the heap (no identity) –think immutable records or structs. Value types would provide additional support for the implementation of the Flyweight Pattern. ©SoftMoore ConsultingSlide 20 “Codes like a class, works like an int!”

Related Patterns The flyweight pattern is often combined with the Composite pattern to implement a logically hierarchical structure in terms of a directed-acyclic graph with shared leaf nodes. It is often best to implement State and Strategy objects as flyweights. FlyweightFactory is an example of an Object Cache. (see slides on Object Pool Pattern) ©SoftMoore ConsultingSlide 21

References Flyweight pattern (Wikipedia) Flyweight (doFactory) Make your apps fly (David Geary – JavaWorld) Enums in Java (One More Time) (John Moore – onJava.com, O’Reilly Media) State of the Values (J. Rose, B. Goetz, G. Steele) ©SoftMoore ConsultingSlide 22