Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Design and Architecture

Similar presentations


Presentation on theme: "Software Design and Architecture"— Presentation transcript:

1 Software Design and Architecture
Structural Design Patterns Muhammad Nasir

2 Flyweight Intent Use sharing to support large numbers of fine-grained objects efficiently. Flyweight is a category in boxing which includes fighters weighing less than 112 lb (51 kg) but above 108 lb (49 kg).

3 Flyweight Motivation Imagine a word processor (Document Editor) that represents text documents consisting of pages, rows, words, and characters. For homogeneity, it would be nice to treat the concepts of pages, rows, words, and characters as objects. Problem: A book with 300 pages can easily contain 840,000 characters… ! huge overhead if modeled as 840,000 regular objects! will consume lots of memory and may incur unacceptable run-time overhead.

4 Flyweight

5 Flyweight The key concept here is the distinction between intrinsic and extrinsic state. Intrinsic state is stored in the flyweight; it consists of information that's independent of the flyweight's context, thereby making it sharable. Extrinsic state depends on and varies with the flyweight's context and therefore can't be shared. Example Intrinsic State: character code (e.g. Unicode) Extrinsic State: font, text style (bold, italics, regular), position

6 Flyweight

7 Flyweight Logically there is an object for every occurrence of a given character in the document. Physically, however, there is one shared flyweight object per character, and it appears in different contexts in the document structure. Each occurrence of a particular character object refers to the same instance in the shared pool of flyweight objects. A flyweight representing the letter "a" only stores the corresponding character code; it doesn't need to store its location or font.

8 Flyweight Structure

9 Flyweight 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 large quantity of objects. Many groups of objects may be replaced by relatively few shared objects once extrinsic state (font, text style) is separated

10 Flyweight Participants Flyweight ConcreteFlyweight (Character)
Declares an interface for flyweights ConcreteFlyweight (Character) Implements the Flyweight interface and adds storage for Intrinsic state, if any. A ConcreteFlyweight object must be sharable UnsharedConcreteFlyweight (Row, Column) Not all Flyweight subclasses need to be shared. It's common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level (as the Row and Column classes have).

11 Flyweight FlyweightFactory Client
Creates and manages flyweight objects. Ensures that flyweights are shared properly. When a client requests a flyweight, the FlyweightFactory object supplies an existing instance or creates one, if none exists. Client Interacts with FlyweightFactory to request Flyweight objects. Manages intrinsic and extrinsic information.

12 Flyweight Collaborations
Flyweight objects store intrinsic and extrinsic state and supply it into flyweight's operations. Clients don't create flyweight objects directly. A flyweight factory makes sure flyweight objects are correctly shared.

13 Flyweight Consequences
Reduced memory consumption comes at the cost of increased runtime, because client has to compute or access stored extrinsic state information, and pass it into flyweight objects. Memory saving depends on… Degree of reduction of objects Size of intrinsic state Whether extrinsic state is stored or calculated

14 Flyweight Related Patterns
Flyweight often combined with composite pattern, to build hierarchy of objects with shared (flyweight) leaves.

15 The End Thanks for listening Questions would be appreciated…


Download ppt "Software Design and Architecture"

Similar presentations


Ads by Google