Download presentation
Presentation is loading. Please wait.
1
Algorithm Programming 2 89-211 Structural Design Patterns Bar-Ilan University 2005-2006 תשס " ו by Moshe Fresko
2
Structural Patterns Structural Patterns are concerned with how classes and objects are composed to form larger structures. Examples: Adapter: Makes an interface to conform to another. So it makes a uniform abstraction of different interfaces. Composite: Describes how to build class hierarchy made up of classes for two kinds of objects: primitive and composite. Decorator: How to add responsibilities to objects dynamically. Proxy Flyweight
3
Intent Use sharing to support large number of fine- grained objects efficiently. Motivation An Object Oriented Document editor implementation may have objects representing each character. Even a moderate sized Document may need lots of character objects. The Flyweight pattern describes how to share objects to allow their use at fine granularities.
4
Flyweight
5
A Flyweight is a shared object that can be used in multiple contexts simultaneously Indistinguishable from a non-shared object Key concept is to distinguish between intrinsic and extrinsic state. Intrinsic state is stored in the Flyweight, and is independent of the context. Example: Character code, and its graphical style. Extrinsic state depends on the context and thus it cannot be shared Coordinate position, character ’ s typographic style.
6
Flyweight
7
StringExample class StringExample { public static void main(String[]args) { String ent1 = new String("This is a string") ; String ent2 = new String("This is another string") ; String ent3 = new String("This is another string") ; if (ent2==ent3) System.out.println("ent2 and ent3: same String ref.") ; else System.out.println("ent2 and ent3: different String ref.") ; }
8
UniqueString import java.util.* ; interface Entity { public String getName() ; } class EntityFactory { static class EntityWrapper implements Entity { String str = null ; public EntityWrapper(String str) { this.str = str ; } public String getName() { return str ; } } private static Map map = new HashMap() ; public static Entity getByName(String str) { if (! map.containsKey(str)) map.put(str,str) ; return new EntityWrapper((String)map.get(str)) ; } public static int getSize() { return map.size() ; } }
9
Unique String class UniqueString { public static void main(String[]args) { Entity ent1 = EntityFactory.getByName("This is a string"); Entity ent2 = EntityFactory.getByName("This is another string"); Entity ent3 = EntityFactory.getByName("This is another string"); System.out.println("# of Strings in Memory is "+ EntityFactory.getSize()) ; if (ent2.getName()==ent3.getName()) System.out.println("ent2 and ent3: same String ref.") ; else System.out.println("ent2 and ent3: different String ref.") ; } Output: # of Strings in Memory is 2 ent2 and ent3: same String ref.
10
FlyWeight - Applicability Use Flyweight when An application uses a large number of objects Storage costs are high Most object state can be made extrinsic Many groups of objects may be replaced with a few shared objects once extrinsic state is removed Application doesn ’ t depend on object identity
11
Structure
12
Flyweight - Participants Flyweight Declares an interface through which flyweights can receive and act on extrinsic state ConcreteFlyweight Implements the interface and stores intrinsic state UnsharedConcreteFlyweight Not all flyweight classes need to be shared FlyweightFactory Creates and manages the flyweights Ensures that flyweights are shared properly Client Maintains a reference to flyweights Computes and stores extrinsic state
13
Flyweight Implementation States must be characterized as intrinsic or extrinsic Clients should not instantiate Concrete Flyweights directly. Shared objects must be managed well. Consequences Reduction in total number of instances
14
Proxy Intent Provide a surrogate or placeholder for another object to control access to it Motivation On-demand creation. Like a document editor having expensive-to-create graphical objects. A proxy can be stand-in for the real image object.
15
Proxy
16
Proxy Structure
17
Proxy - Participants Proxy Maintains a reference to the real subject Provides interface for the subject so that it can be substituted with the subject Control access to the real subject Subject Defines the common interface for real Subject and Proxy RealSubject Defines the real object that the proxy represents
18
Proxy Proxy pattern Applicability 1. Remote Proxy: Provides a local representative for an object in a different address space 2. Virtual Proxy: Creates expensive objects on demand. 3. Protection Proxy: Controls access to the original object 4. Smart Reference: Replacement to a bare reference in order to perform additional actions when an object is accessed. Reference counting so that it can be freed when there are no more references to it Loading an object to memory when it is first accessed Synchronization protection
19
Unique String with Ref Count class EntityFactory { private static Map map = new HashMap() ; private static void removeStr(String str) { map.remove(str) ; } static class RefCountedEntity { private String str; private int cnt; RefCountedEntity(String str) { this.str=str ; cnt=0 ; } public void incr() { cnt++ ; } public void decr() { if ((--cnt)==0) removeStr(str) ; } public String getName() { return str ; } }... }
20
Unique String with Ref Count class EntityFactory {... static class EntityWrapper implements Entity { RefCountedEntity ref = null ; public EntityWrapper(RefCountedEntity ref) { this.ref = ref ; ref.incr() ; } public String getName() { return ref.getName() ; } public void finalize() { ref.decr() ; } } public static Entity getByName(String str) { if (! map.containsKey(str)) map.put(str,new RefCountedEntity(str)) ; return new EntityWrapper((RefCountedEntity)map.get(str)) ; } public static int getSize() { return map.size() ; } }
21
Unique String with Ref Count class UniqueStringRef { public static void main(String[]args) { Entity ent1 = EntityFactory.getByName("This is a string") ; Entity ent2 = EntityFactory.getByName("This is another string") ; Entity ent3 = EntityFactory.getByName("This is another string") ; System.out.println("# of Strings in Memory is "+EntityFactory.getSize()) ; if (ent2.getName()==ent3.getName()) System.out.println("ent2 and ent3: same String ref.") ; else System.out.println("ent2 and ent3: different String ref.") ; System.out.println("Clearing ent1..") ; ent1 = null ; System.gc() ; System.out.println("# of Strings in Memory now is “ +EntityFactory.getSize()) ; } Output: # of Strings in Memory is 2 ent2 and ent3: same String ref. Clearing ent1.. # of Strings in Memory now is 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.