Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-1 PS95&96-MEF-L14-1 Dr. M.E. Fayad Creationa.

Similar presentations


Presentation on theme: "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-1 PS95&96-MEF-L14-1 Dr. M.E. Fayad Creationa."— Presentation transcript:

1 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-1 PS95&96-MEF-L14-1 Dr. M.E. Fayad Creationa l Paradigm Shift, Inc. Software Factory Behaviora l Structural Lesson 7: More on Structural Patterns Object-Oriented Design Patterns

2 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-2 PS95&96-MEF-L14-2 Dr. M.E. Fayad Lesson Objectives oPresent the following Patterns: Proxy Composite oDiscuss in detail the structural patterns

3 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-3 PS95&96-MEF-L14-3 Dr. M.E. Fayad Structural Patterns  Proxy Pattern Topics –Proxy Definition –Structure –Document Editor Example –Proxy Properties –Applicability –Consequences –ErrorProxy Example

4 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-4 PS95&96-MEF-L14-4 Dr. 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).

5 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-5 PS95&96-MEF-L14-5 Dr. M.E. Fayad Proxy Pattern: Structure realSubject->Request(); Subject Request()...... RealSubject Request() ClientClass Proxy Request() Uses real Subject aClientObject subject aSubjectProxy realSubject aRealSubject Instance Diagram

6 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-6 PS95&96-MEF-L14-6 Dr. 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)

7 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-7 PS95&96-MEF-L14-7 Dr. M.E. Fayad Document Editor Example if (image == 0) image=Loadimage(fileName) image->Draw() GraphicItem Draw() GetEntent() Store() Load() RealSubject fileName extent DocumentEditor ImageProxy imageRep extent Items Image (creates) Draw() GetEntent() Store() Load() Draw() GetEntent() Store() Load() if (image == 0) return extent return image->GetExtent()

8 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-8 PS95&96-MEF-L14-8 Dr. 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

9 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-9 PS95&96-MEF-L14-9 Dr. 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.

10 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-10 PS95&96-MEF-L14-10 Dr. 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

11 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-11 PS95&96-MEF-L14-11 Dr. 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

12 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-12 PS95&96-MEF-L14-12 Dr. 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

13 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-13 PS95&96-MEF-L14-13 Dr. 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

14 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-14 PS95&96-MEF-L14-14 Dr. 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.

15 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-15 PS95&96-MEF-L14-15 Dr. 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.

16 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-16 PS95&96-MEF-L14-16 Dr. 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.

17 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-17 PS95&96-MEF-L14-17 Dr. 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

18 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-18 PS95&96-MEF-L14-18 Dr. 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();

19 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-19 PS95&96-MEF-L14-19 Dr. 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.

20 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-20 PS95&96-MEF-L14-20 Dr. 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)

21 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-21 PS95&96-MEF-L14-21 Dr. M.E. Fayad Structural Patterns  Composite Pattern Topics –Composite Definition –Structure –Graphic Application Example –Problems it Solves –Benefits –Related Patterns

22 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-22 PS95&96-MEF-L14-22 Dr. 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

23 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-23 PS95&96-MEF-L14-23 Dr. M.E. Fayad Composite Pattern: Example A CIRCLES Composite Circle2Circle1 S E L CR I C A Composite

24 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-24 PS95&96-MEF-L14-24 Dr. 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

25 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-25 PS95&96-MEF-L14-25 Dr. M.E. Fayad Graphic Application Example: Type Hierarchy Graphic Primitive (User Defined) Picture (Composite) User Defined Data Iterator

26 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-26 PS95&96-MEF-L14-26 Dr. 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 of graphics Rectangle Draw()

27 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-27 PS95&96-MEF-L14-27 Dr. 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

28 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-28 PS95&96-MEF-L14-28 Dr. 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

29 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-29 PS95&96-MEF-L14-29 Dr. 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

30 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-30 PS95&96-MEF-L14-30 Dr. 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

31 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-31 PS95&96-MEF-L14-31 Dr. 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

32 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-32 PS95&96-MEF-L14-32 Dr. M.E. Fayad Structural Patterns  Discussion & Conclusions Topics –Adapter versus Bridge versus Facade –Composite versus Decorator versus Proxy –Key Insights

33 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-33 PS95&96-MEF-L14-33 Dr. 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

34 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-34 PS95&96-MEF-L14-34 Dr. 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 interface work together AdapterFacade Makes two existing interfaces Defines a new interface work together

35 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-35 PS95&96-MEF-L14-35 Dr. 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

36 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-36 PS95&96-MEF-L14-36 Dr. 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?

37 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-37 PS95&96-MEF-L14-37 Dr. M.E. Fayad Discussion 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.


Download ppt "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-1 PS95&96-MEF-L14-1 Dr. M.E. Fayad Creationa."

Similar presentations


Ads by Google