1 / 37

Creational

Behavioral. Paradigm Shift, Inc. Software Factory. Creational. Structural. Lesson 7: More on Structural Patterns. Object-Oriented. Patterns. Design. Lesson Objectives. Present the following Patterns: Proxy Composite Discuss in detail the structural patterns. Structural Patterns.

huyen
Download Presentation

Creational

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Behavioral Paradigm Shift, Inc. Software Factory Creational Structural Lesson 7:More on Structural Patterns Object-Oriented Patterns Design

  2. Lesson Objectives • Present the following Patterns: • Proxy • Composite • Discuss in detail the structural patterns

  3. Structural Patterns • Topics • Proxy Definition • Structure • Document Editor Example • Proxy Properties • Applicability • Consequences • ErrorProxy Example • Proxy Pattern

  4. 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. Uses Subject ClientClass Request() ...... real Subject RealSubject Proxy Request() Request() realSubject->Request(); Proxy Pattern: Structure aClientObject subject aSubjectProxy realSubject aRealSubject Instance Diagram

  6. Proxy Pattern: Structure (2) • 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.

  7. Document Editor Example GraphicItem Items DocumentEditor Draw() GetEntent() Store() Load() RealSubject ImageProxy if (image == 0) image=Loadimage(fileName) image->Draw() Image (creates) Draw() GetEntent() Store() Load() Draw() GetEntent() Store() Load() imageRep extent fileName extent if (image == 0) return extent return image->GetExtent()

  8. Proxy Pattern Properties • 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

  9. 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. 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. 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. 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. Proxy Pattern: Participants & Collaborations Participants • 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.

  14. 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. 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. 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. 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. ErrorProxy Diagram uses ErrorItem ErrorClient 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. ErrId GetId(); ErrDate GetDate(); ErrStatus GetStatus(); Display(); Change(); RealErrorItem ProxyErrorItem ErrId GetId(); ErrDate GetDate(); ErrStatus GetStatus(); Display(); Change(); ErrId GetId(); ErrDate GetDate(); ErrStatus GetStatus(); Display(); Change(); if (errorPtr != NULL) return errorPtr->GetId(); else return anId; ErrId anId; ErrDate aDate; ErrStatus aStatus; if (errorPtr != NULL) { errorPtr = new RealErrorItem; persistPtr->Read(anId, errorPtr); }; errorPtr->Display(); ErrId anId; ErrDate aDate; ErrStatus aStatus; RealErrorItem* errorPtr; Persistor* persistPtr;

  19. 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. Scenario Diagram Walkthrough B1: getNext() ErrorProxyItem ErrorLogClient D1: Display() ContainerOfProxyErrorItems C1: getErrorId() D2: Create RealErrorItem ErrorProxyItem A4: Add ProxyErrorItem D3: RealError(ErrorID, RealErrorItem) D5: Display() A2: Write RealErrorItem to DASD RealErrorItem ErrorBuilder Persistor D4: Hydrate RealErrorItem A1: BuildError(ErrorType) A3: WriteObjectToDASD ObjectReportingError DASD

  21. Structural Patterns • Topics • Composite Definition • Structure • Graphic Application Example • Problems it Solves • Benefits • Related Patterns • Composite Pattern

  22. 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. Composite Pattern: Example Composite A CIRCLES A Composite Composite I L S C R C E Circle1 Circle2

  24. Composite Pattern: Structure Component Operation() Iterator() Client Children Leaf Composite for all g in children g.Operation(); Operation() Operation() Add(Component) Remove(Component) Iterator()

  25. Graphic Application Example:Type Hierarchy Graphic Primitive (User Defined) Picture (Composite) User Defined Data Iterator

  26. Graphic Application Example Component Operation() Iterator() graphics Rectangle Line Text Picture for all g in graphics g.Draw(); Draw() Draw() Draw() Draw() Add(Graphic g) Remove(Graphic) Iterator() add g to list of graphics

  27. 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. 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. 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. 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. 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. Structural Patterns • Topics • Adapter versus Bridge versus Facade • Composite versus Decorator versus Proxy • Key Insights • Discussion & Conclusions

  33. 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. The Differences Between an Adapter & a Bridge 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 • 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 • Adapter Facade • Makes two existing interfaces Defines a new interface • work together

  35. 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. 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. 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.

More Related