Unit V
Unit V
Objectives
S. No Topic PPT Slides
3. Proxy
4. Flyweight 5. Repeated key points for Structural Patterns
L3
L4 L5
28 40
41 47 48 52
L6
L7
53 57
58 58
L1
L1
Decorator creates an aggregated linked list of Decoration objects ending with the basic Substance object.
L1
Pattern: Decorator
objects that wrap around other objects to add useful features
L1
Decorator pattern
decorator: an object that modifies behavior of, or adds features to, another object
decorator must maintain the common interface of the object it wraps up
used so that we can add features to an existing simple object without needing to disrupt the interface that client code expects when using the simple object examples in Java:
multilayered input streams adding useful I/O methods adding designs, scroll bars and borders to GUI controls
L1
normal InputStream class has only public int read() method to read one letter at a time decorators such as BufferedReader or Scanner add additional functionality to read the stream more easily
// InputStreamReader/BufferedReader decorate InputStream InputStream in = new FileInputStream("hardcode.txt"); InputStreamReader isr = new InputStreamReader(in);
L1
L1
An example: Decorator
Intent: Allow to attach responsibilities to objects, extend their functionality, dynamically Motivation: Consider a WYSIWYG editor The basic textWindow needs various decorations: borders, tool bars, scroll bars,
L1
L1
L1
Structure, participants, collaborations: Component an interface, describes the operations of a (GUI) component
ConcreteComponent (extends Component)
class of objects to which decorations can be added (e.g. the original TextWindow)
Decorator (extends Component) a class of
L1
Implementation:
Each decorator has a (private) reference to a Component object (CC or D) For each operation, the decorator performs its decoration job, & delegates Component operations to the Component object it contains
Decorator extends Component (can accept
Component requests)
L1
L1
Decorator
ConcreteComponent
Various decorators
L1
Consequences : Decoration can be done dynamically, at run-time When it makes sense, a decoration can be added several times The # of decorators is smaller than the # of sub-classes in original solution; avoids lots of feature-heavy classes in the inheritance hierarchy
L1
Issues to remember : A decorator and the Component object it contains are not identical Many small objects; one has to learn their roles, the legal connections, etc.
(but, still much better than sub-classing)
L1
L1
L1
L2
Faade (2)
Example: graph interface to a simulation engine
SchematicEditor Graph
2
Director
Relation
Port
0..*
Entity
CompositeEntity
BufferedRelation
AtomicEntity Actor
Token
L2
Solution:
A single Facade class implements a high-level interface for a subsystem by invoking the methods of the lower-level classes.
Example. A Compiler is composed of several classes: LexicalAnalyzer, Parser, CodeGenerator, etc. A caller, invokes only the Compiler (Facade) class, which invokes the contained classes.
L2
Facade
service()
Class1 service1()
Class2 service2()
Class3 service3()
Facade: Consequences
Consequences:
L2
L2
Facade: Motivation
Clients communicate with the package (subsystem ) by sending requests to Facade, which forwards them to the appropriate package object(s).
L2
Facade: Applicability
To provide simple interface to a complex package, which is useful for most clients. To reduce the dependencies between the client and the package, or dependencies between various packages.
L2
Facade: Consequences
It shields clients from package components, thereby reducing the number of objects that clients deal with and making the package easier to use.
It promotes weak coupling between the package and its clients and other packages, thereby promoting package independence and portability.
L3
Proxy
You want to
delay expensive computations, use memory only when needed, or check access before loading an object into memory
Proxy
has same interface as Real object stores subset of attributes does lazy evaluation
L3
L3
L3
L3
L3
Proxy: Consequences
Proxy promotes:
L3
Efficiency: avoids time-consuming operations when necessary. Correctness: separates design and code that are independent of retrieval/efficiency from parts concerned with this issue. Reusability: design and code that are independent of retrieval efficiency are most likely to be reusable. Flexibility: we can replace one module concerned with retrieval with another. Robustness: isolates parts that check for the validity of retrieved data.
L3
L3
0..1
L3
Proxy: Consequences
Consequences:
L3
Pattern Hatching
Proxy Pattern
Subject request() realSubject->request();
RealSubject request()
realSubject
Proxy request()
L3
Pattern Hatching
Proxy Pattern
We need to find a common structure for the proxy pattern with our composite pattern As we recognize, our common interface that we still want to use for the file-system is Node And because the Composite structure uses a common interface already, we can combine the Proxy Subject Interface into our Node Interface
L3
Pattern Hatching
Node
Proxy Pattern
subject
children
Composite Pattern
L4
L4
L4
L4
L4
L4
L4
Flyweight: Consequences
Space savings increase as more flyweights are shared.
L5
An example: Decorator
Intent: Allow to attach responsibilities to objects, extend their functionality, dynamically Motivation: Consider a WYSIWYG editor The basic textWindow needs various decorations: borders, tool bars, scroll bars,
UNIT-V 48
L5
UNIT-V
49
L5
UNIT-V
50
L5
Structure, participants, collaborations: Component an interface, describes the operations of a (GUI) component
ConcreteComponent (extends Component)
class of objects to which decorations can be added (e.g. the original TextWindow)
Decorator (extends Component) a class of
UNIT-V
51
L5
Implementation:
Each decorator has a (private) reference to a Component object (CC or D) For each operation, the decorator performs its decoration job, & delegates Component operations to the Component object it contains
Decorator extends Component (can accept
Component requests)
UNIT-V
52
L6
L6
Faade (2)
Example: graph interface to a simulation engine
SchematicEditor Graph
2
Director
Relation
Port
0..*
Entity
CompositeEntity
BufferedRelation
AtomicEntity Actor
UNIT-V
Token
54
L6
Solution:
A single Facade class implements a high-level interface for a subsystem by invoking the methods of the lower-level classes.
Example. A Compiler is composed of several classes: LexicalAnalyzer, Parser, CodeGenerator, etc. A caller, invokes only the Compiler (Facade) class, which invokes the contained classes. UNIT-V 55
L6
Facade
service()
Class1 service1()
Class2 service2()
Class3 service3()
UNIT-V
56
Facade: Consequences
Consequences:
L6
UNIT-V
57
L7