Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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 details about each car’s ownership (owner name, tag number, last registration date). Flyweight Pattern  Example: Car Registrations /* Car class, un-optimized. */ class Car { public Car ( model, year, owner, tag, renewDate) { this.model = model; this.year = year; this.owner = owner; this.tag = tag; this.renewDate = renewDate; { //---------------------------------------------------------------- }

3 Flyweight Pattern  Intent Use sharing to support a large number of fine-grained objects efficiently. The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive storage cost  Motivation converting many independent objects into a few shared objects, reducing the amount of resources needed to run web applications The trick is to separate the intrinsic state from the extrinsic state of each object: - Extrinsic state: information that depends and varies with the flyweight's context ( thus shouldn't be shared ). This information should be stored outside the flyweight. - Intrinsic state: information that's independent of the flyweight's context. This information should be store inside the flyweight.

4 Flyweight Pattern  Structure Flyweight operation(extrinsicState) FlyweightFactory getFlyweight(key) Client ConcreteFlyweight operation(extrinsicState) intrinsicState UnsharedConcreteFlyweight operation(extrinsicState) allState if(flyweight(key) exists) { return existing flyweight; } else { create new flyweight; add it to pool of flyweights return the new flyweight; }.

5 Flyweight Pattern  Participants Flyweight : declares the interface that flyweights use to act on extrinsic state. ConcreteFlyweight : implements the flyweight and adds storage for the intrinsic state (which should be sharable). UnsharedConcreteFlyweight: not all flyweight need to be shared. Client: stores the extrinsic state of the flyweights. FlyweightFactory: creates and manages flyweight objects.ensures that flyweights are shared properly.

6 Flyweight Pattern  Collaborations |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Client FlyweightFactory Flyweight getFlyweight() FlyweightFound= FindFlyweight() [! FlyweightFound] New() flyweight Operation (extrinsic state)

7 Categorizing an object’s data as intrinsic or extrinsic: the physical car data ( model, year) is intrinsic, and the owner data (owner name, tag number, last registration date) is extrinsic. This means that only one car object is needed for each combination of model, and year. Flyweight Pattern  Example: Car Registrations /* Car class, optimized as a flyweight. */ class Car { public Car ( model, year) { this.model = model; this.year = year; } //-------------------------------------- }

8 Instantiation Using a Factory: Factory ensures that only a single copy of each unique intrinsic state is created: Flyweight Pattern  Example: Car Registrations /* CarFactory singleton. */ class CarFactory { private Car createdCars []; public Car createCar( model, year) { // Check to see if this particular combination has been created // before. otherwise create a new instance and save it. if(! createdCars[ model + '-' + year]) { var car = new Car(model, year); createdCars[ model + '-' + year] = car; } return createdCars[model + '-' + year]; } //------------------------------------------------------------------ }

9 Extrinsic State Encapsulated in a Manager:All of the data that was removed from the Car objects has to be stored somewhere; you use a singleton as a manager to encapsulate that data. Flyweight Pattern  Example: Car Registrations /* CarRecordManager singleton. */ class CarRecordManager { private CarRecord carRecordDatabase []; // Add a new car record into the city's system. public void addCarRecord (model, year, owner, tag, renewDate) { car = CarFactory.createCar( model, year); carRecordDatabase[tag] = { owner: owner, renewDate: renewDate, car: car } //------------------------------------------------------------ }

10 Flyweight Pattern  Applicability The Flyweight pattern's effectiveness depends heavily on how and where it's used. Flyweight should only be used when all the following are true: An application uses a large number of objects. Storage costs are high because of the sheer quantity of objects. At least some of the data stored within each of these objects must be able to be made extrinsic. Objects can be replaced by few shared objects once the extrinsic state is removed

11 Flyweight Pattern  Consequences By increasing the amount of data that is shared, we can increase our savings in space (memory, disk, etc). This saving is heavily influenced by the number of flyweight we need to create. However, the use of Flyweights may introduce some performance overhead. Since some data is stored outside the object, there are some performance penalties (retrieving that data).

12 Flyweight Pattern  Implementation Removing extrinsic state :The first step when building a flyweight is to decide what data should be intrinsic and what data should be extrinsic. This decision is critical since it will heavily influence the flyweight's performance. Managing shared objects: Because objects are shared, clients shouldn't instantiate them directly. FlyweightFactory lets clients locate a particular flyweight.

13 Flyweight Pattern  Example: Lexi case study Object Structure

14 Flyweight Pattern  Example: Lexi case study Object Sharing

15 Flyweight Pattern  Example: Lexi case study class Character : public Glyph { public: Character(char); virtual void Draw(Window*, GlyphContext&); private: char _charcode; }; class GlyphFactory { public: GlyphFactory(); virtual ~GlyphFactory(); virtual Character* CreateCharacter(char); virtual Row* CreateRow(); virtual Column* CreateColumn(); //... private: Character* _character[]; };

16 Flyweight Pattern  Example: Lexi case study class GlyphContext { public: GlyphContext(); virtual ~GlyphContext(); virtual void Next(int step = 1); virtual void Insert(int quantity = 1); virtual Font* GetFont(); virtual void SetFont(Font*, int span = 1); private: int _index; BTree* _fonts; }; Character* GlyphFactory::CreateCharacter (char c) { if (!_character[c]) { _character[c] = new Character(c); } return _character[c]; }

17 Flyweight Pattern  Example: Lexi case study BTree

18 Flyweight Pattern  Example: Lexi case study GlyphContext gc; Font* times12 = new Font("Times-Bold-12"); //--------------------------- gc.SetFont(times12, 42);

19 Suppose we want to draw a small folder icon with a name under it for each person in an organization. they are actually all the same graphical image. Even if we have two icons-one for "is Selected" and one for "not Selected“- the number of different icons is small. Flyweight Pattern  Example: Folder Objects

20 Flyweight Pattern  Example: Folder Objects class Folder{ public Folder(Color c){ color = c; } public void Draw(Graphics g, int tx, int ty, String name){ //draw folder } class FolderFactory{ Folder unSelected, Selected; public FolderFactory(){ Selected = new Folder(Color.brown); unSelected = new Folder(Color.yellow); { public Folder getFolder(boolean isSelected)} if (isSelected) return Selected; else return unSelected; {

21 Flyweight Pattern  Example: Folder Objects public void FolderFactory :: paint(Graphics g){ Folder f; String name; //go through all the names and folders for (int i = 0; i< names.size(); i++)} name = (String)names.elementAt(i); if(name.equals(selectedName)) f = fact.getFolder(true); else f = fact.getFolder(false); //have that folder draw itself at this spot f.Draw(g, x, row, name); //----------- }

22


Download ppt "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."

Similar presentations


Ads by Google