View
217
Download
1
Embed Size (px)
Citation preview
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-1
PS95&96-MEF-L14-1Dr. M.E. Fayad
Creational
Paradigm Shift, Inc.Software Factory
Behavioral
Structural
Lesson 7:More on Structural Patterns
Lesson 7:More on Structural Patterns
Object-Object-OrientedOriented
DesignDesign PatternPatternss
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-2
PS95&96-MEF-L14-2Dr. M.E. Fayad
Lesson ObjectivesLesson Objectives
Objectives
Present the following Patterns:
Proxy
Composite
Discuss in detail the structural patterns
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-3
PS95&96-MEF-L14-3Dr. M.E. Fayad
Structural Patterns
Proxy Pattern
• Topics– Proxy Definition
– Structure
– Document Editor Example
– Proxy Properties
– Applicability
– Consequences
– ErrorProxy Example
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-4
PS95&96-MEF-L14-4Dr. M.E. Fayad
Proxy Pattern: Definition
• A Proxy is a surrogate or a placeholder for another object to control access to the real objects.
• The Proxy pattern is applicable when there is a need for sophistication in a reference to an object. There is added function between a client and the intended subject object.
• Sometimes the Proxy pattern is known as an Ambassador pattern (when dealing with distributed objects).
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-5
PS95&96-MEF-L14-5Dr. M.E. Fayad
Proxy Pattern: Structure
realSubject->Request();
Subject
Request()......
RealSubject
Request()
ClientClass
Proxy
Request()
Uses
realSubject
aClientObject
subject aSubjectProxy
realSubject aRealSubjectInstance Diagram
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-6
PS95&96-MEF-L14-6Dr. M.E. Fayad
• The Subject defines the common interface for the RealSubject and Proxy.
• The Proxy can be used any place a RealSubject is expected.
• The RealSubject defines the real object that the Proxy represents.
Proxy Pattern: Structure (2)
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-7
PS95&96-MEF-L14-7Dr. M.E. Fayad
Document Editor Example
if (image == 0) image=Loadimage(fileName)image->Draw()
GraphicItemDraw()GetEntent()Store()Load()
RealSubject
fileNameextent
DocumentEditor
ImageProxy
imageRepextent
Items
Image(creates)
Draw()GetEntent()Store()Load()
Draw()GetEntent()Store()Load()
if (image == 0) return extentreturn image->GetExtent()
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-8
PS95&96-MEF-L14-8Dr. M.E. Fayad
• Maintains the ability to access the real object, while screening requests for direct manipulation of the object
• Provides an identical interface to the Subject so that it can be substituted for the Subject in any instance
• Controls access to the Subject, even managing it
• Has other responsibilities defined by the type of Proxy:
– Remote Proxies
– Virtual Proxies
– Protection Proxies
– Smart Reference Proxies
Proxy Pattern Properties
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-9
PS95&96-MEF-L14-9Dr. M.E. Fayad
Remote Proxies
• Are responsible for encoding requests (with its accompanying arguments) to forward to its corresponding real object, which is found in another address space (Remote or Distributed Object).
• This Proxy is an “Ambassador” to a distributed object.
• This Proxy hides the fact from the user that the object is distributed.
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-10
PS95&96-MEF-L14-10Dr. M.E. Fayad
Virtual Proxies
• Cache information about the real object, so that they only access the object when it is absolutely necessary
• This Proxy is used to access expensive objects, such as objects high in storage, access time, or another limit resource, created on demand.
• Example: ImageProxy
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-11
PS95&96-MEF-L14-11Dr. M.E. Fayad
Protection Proxies
• Protect access to the original object
• This means that screen users to access the real object or only allow access to a limited public interface
• While the real object may have a private interface is then a subset of the real interface
• Example: KernelProxies in the Choices Operating System that provide a protected access to operating system objects
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-12
PS95&96-MEF-L14-12Dr. M.E. Fayad
Smart Reference Proxy
• A Smart Reference Proxy is a replacement for a bare pointer
• A Smart Reference Proxy performs additional actions when an object is accessed:
– Count the number of references to the real object so that it can be automatically freed when there are no more references
– Create a transient representation for a persistent object that is loading the object into memory
– Copy or write, that is, just increment the reference count when asked to copy the Subject, only doing the actual copy when the client requests an operation that modifies the Subject
– Check for locking before forwarding an access request
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-13
PS95&96-MEF-L14-13Dr. M.E. Fayad
Proxy Pattern: Participants & Collaborations
• Proxy
– See previous slides
• Subject
– defines the common interface for ConcreteSubject and Proxy so that Proxy can be used anywhere a ConcreteSubject is expected
• ConcreteSubject
– defines the real object that the Proxy represents
Collaborations
The Proxy forwards all requests to the ConcreteSubject. But before doing so, the Proxy performs additional actions depending on the kind of Proxy.
Collaborations
The Proxy forwards all requests to the ConcreteSubject. But before doing so, the Proxy performs additional actions depending on the kind of Proxy.
Participants
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-14
PS95&96-MEF-L14-14Dr. M.E. Fayad
Proxy Pattern: Consequences
• A Proxy pattern introduces a level of indirection when accessing an object.
• A Remote Proxy can hide the fact that an object resides in a different address space.
• A Virtual Proxy can perform optimizations like creating an object on demand.
• Both Protected Proxies and Smart References allow additional housekeeping tasks when an object is accessed.
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-15
PS95&96-MEF-L14-15Dr. M.E. Fayad
Proxy Pattern: Distinguishing Aspects
• The Proxy is a stand-in for a Subject when it is inconvenient or undesirable to access the Subject directly, as it may be on a remote machine, has restricted access, or is persistent.
• The Proxy maintains a reference to the Subject object and forwards all requests to the Concrete or RealSubject, but will perform additional actions before forwarding the request.
• The Proxy provides the same interface, or a subset, as the original object.
• The Proxy pattern models a one to one relationship and this relationship is a static relationship.
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-16
PS95&96-MEF-L14-16Dr. M.E. Fayad
Proxy Pattern: Implementation Issues in C++
1. Overloading the class member access operator ->
2. Writing the forwarding operators manually, when the Proxy needs to know which operations are being called, or wants to perform additional actions before forwarding the request.
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-17
PS95&96-MEF-L14-17Dr. M.E. Fayad
ErrorProxy Example:How Errors are Used in the System
• Errors can be reported and logged at any time while the system is running
• The information contained within an error is large
• A user may occasionally want to look at the error log, viewing the list of errors, and displaying some lightweight information about all the logged errors
• The user may decide to work with errors in the log and display or update the error information
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-18
PS95&96-MEF-L14-18Dr. M.E. Fayad
All of the Proxy get
routines would check
for errorPtr==NULL
first & read if necessary.
The read routine would
return a new -- Real-
ErrorItem. Display &
Change would always
read, as they would need
all of the data.
ErrorProxy Diagram
ErrorClient ErrorItem
ErrId GetId();ErrDate GetDate();ErrStatus GetStatus();Display();Change();
uses
RealErrorItem
ErrId GetId();ErrDate GetDate();ErrStatus GetStatus();Display();Change();
ErrId anId;ErrDate aDate;ErrStatus aStatus;
ProxyErrorItem
ErrId GetId();ErrDate GetDate();ErrStatus GetStatus();Display();Change();
ErrId anId;ErrDate aDate;ErrStatus aStatus;RealErrorItem* errorPtr;Persistor* persistPtr;
if (errorPtr != NULL) return errorPtr->GetId();else return anId;
if (errorPtr != NULL) { errorPtr = new RealErrorItem; persistPtr->Read(anId, errorPtr);};errorPtr->Display();
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-19
PS95&96-MEF-L14-19Dr. M.E. Fayad
Use of the ErrorProxy
• A Virtual Proxy “ProxyErrorItem” will be used to represent a “RealErrorItem” which spends most of its time out on DASD due to its size.
• ProxyErrorItem will contain some LightWeight information which will allow it to support several common method calls without having to Hydrate the RealError from the DASD unless it is really needed.
– Note: the good is to conserve system memory as much as possible.
• ProxyErrorItem is responsible for deleting the RealErrorItem it has created off DASD, as well as deleting the RealError out on DASD when the Proxy’s Destructor is called.
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-20
PS95&96-MEF-L14-20Dr. M.E. Fayad
Scenario Diagram Walkthrough
DASD
Persistor
ErrorProxyItem
ErrorLogClient
ObjectReportingError
ErrorBuilder
ContainerOfProxyErrorItems
RealErrorItem
A1: BuildError(ErrorType)
A2: Write RealErrorItem to DASD
A3: WriteObjectToDASD
A4: Add ProxyErrorItem
B1: getNext()ErrorProxyItem
C1: getErrorId()
D1: Display()
D5: Display()
D2: Create RealErrorItem
D4: Hydrate RealErrorItem
D3: RealError(ErrorID, RealErrorItem)
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-21
PS95&96-MEF-L14-21Dr. M.E. Fayad
Structural Patterns
Composite Pattern
• Topics– Composite Definition
– Structure
– Graphic Application Example
– Problems it Solves
– Benefits
– Related Patterns
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-22
PS95&96-MEF-L14-22Dr. M.E. Fayad
Composite Pattern: Definition
• Compose objects into tree structures to represent part-whole or containment hierarchies
• Allows user to recursively compose complex and primitive objects
• Treat primitive and complex objects the same way
• Motivation: User can group simple objects into complex ones, then complex objects together with primitives to create even more complex objects
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-23
PS95&96-MEF-L14-23Dr. M.E. Fayad
Composite Pattern: Example
A
CIRCLES
Composite
Circle2Circle1
SELCRIC
AComposite
Composite
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-24
PS95&96-MEF-L14-24Dr. M.E. Fayad
Composite Pattern: Structure
for all g in children g.Operation();
Composite
Operation()Add(Component)Remove(Component)Iterator()
Component
Operation()Iterator()
Leaf
Operation()
Client
Children
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-25
PS95&96-MEF-L14-25Dr. M.E. Fayad
Graphic Application Example:Type Hierarchy
Graphic
Primitive(User Defined)
Picture(Composite)
User Defined Data Iterator
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-26
PS95&96-MEF-L14-26Dr. M.E. Fayad
Graphic Application Example
for all g in graphics g.Draw();
Picture
Draw()Add(Graphic g)Remove(Graphic)Iterator()
Component
Operation()Iterator()
Text
Draw()
graphics
Line
Draw()
add g to list ofgraphics
Rectangle
Draw()
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-27
PS95&96-MEF-L14-27Dr. M.E. Fayad
Composite Pattern: What it Solves
Use the Composite pattern when
• You want to represent part-whole or containment hierarchies of objects
• You are adding a new component with no need to distinguish the primitive components from the complex
• The number of Composite layers is dynamic
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-28
PS95&96-MEF-L14-28Dr. M.E. Fayad
Composite Pattern: Participants
• Component (Graphic )
– declares the interface common to all objects in the composition
– declares an interface for enumerating its child Components
• Leaf (Rectangle, Text, Line, ..)
– represents leaf objects in the composition
– defines behavior for the primitive objects in the composition
• Composite (Picture)
– defines behavior for components
– stores child Components
– defines and implements services for enumerating its children
• Client
– manipulates the composition through the interface declared by Component
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-29
PS95&96-MEF-L14-29Dr. M.E. Fayad
Composite Pattern: Collaborations
Clients use the Component class interface to interact with objects in the Composite structure
– If the recipient is a Leaf, then the request is handled directly
– If the recipient is a Composite, then the Composite usually forwards requests to its child Components
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-30
PS95&96-MEF-L14-30Dr. M.E. Fayad
Composite Pattern: Benefits
• Composite pattern makes the client simple
– Clients can treat Composite structures and individual objects uniformly
– Clients normally don’t care whether they are dealing with a leaf or a Composite component
– This simplifies the client code
• Composite pattern makes it easier to add new kinds of components
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-31
PS95&96-MEF-L14-31Dr. M.E. Fayad
Composite Pattern: Related Patterns
• Decorator is often used with Composite
• Iterator offers flexible ways to traverse Composite
• Visitor localizes operations and behaviors that would otherwise be distributed across Composite and Leaf classes
• Flyweight can be used to keep components from having to know about their parents, and thus will let components be shared
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-32
PS95&96-MEF-L14-32Dr. M.E. Fayad
Structural Patterns
Discussion & Conclusions
• Topics
– Adapter versus Bridge versus Facade
– Composite versus Decorator versus Proxy
– Key Insights
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-33
PS95&96-MEF-L14-33Dr. M.E. Fayad
Adapter & Bridge: Common Attributes
• Both have class and object forms
• Both use multiple inheritance in their class form to couple independent classes
• Both promote flexibility in their object form by providing a level of indirection to another object
• Both forward requests to the object from an interface other than its own
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-34
PS95&96-MEF-L14-34Dr. M.E. Fayad
Adapter Pattern• Focus is on resolving incompatibilities
between two existing interfaces• Doesn’t focus on how those interfaces
are implemented• Doesn’t consider how those interfaces
might evolve independently• Makes things work after they are re-
designed
The Differences Between an Adapter & a Bridge
Bridge Pattern
• Focus is on bridging an abstraction and its (potentially numerous) implementations
• Provides a stable interface to clients
• Accommodates new implementations as the system evolves
• Makes things work before they are redesigned
AdapterFacade
Makes two existing interfaces Defines a new interfacework together
AdapterFacade
Makes two existing interfaces Defines a new interfacework together
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-35
PS95&96-MEF-L14-35Dr. M.E. Fayad
Composite & Decorator: Common Attributes
• Both have similar structure diagrams
• Both rely on recursive composition to organize an open-ended number of objects
• Both lead to the kind of design in which you can build applications just by plugging objects together without defining any new classes
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-36
PS95&96-MEF-L14-36Dr. M.E. Fayad
Difference Between Composite & Decorator
• Both have different intents
• Decorator is designed to let you add attributes and behaviors to objects without resorting or subclassing
• Composite focuses on structuring classes so that many related objects can be treated uniformly, and multiple objects can be treated as one
What are the common attributes and the differences between the Decorator and Proxy Patterns?
What are the common attributes and the differences between the Decorator and Proxy Patterns?
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 More on Structural Patterns - Page L7-37
PS95&96-MEF-L14-37Dr. M.E. Fayad
Discussion QuestionsDiscussion Questions
1. Define the different types of the Proxy pattern and list their responsibilities.
2. Mark (T) for true or (F) for false
( ) 1. Sometimes the Proxy pattern is known as an Ambassador when dealing with distributed objects.
( ) 2. Iterator offers flexible ways to traverse Composites.
( ) 3. Use Composite when you want to represent part-whole or containment hierarchies of objects.
( ) 4. Composite pattern makes the client simple. Why?
( ) 5. Decorator is designed to let you add attributes or behaviors to objects without resorting or subclassing.
( ) 6. An adapter makes two existing interfaces work together as opposed to defining an entire new one.
( ) 7. Decorator & Proxy describe how to provide a level indirection to an object, but they intended for different purposes.