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.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Chapter 8, Object Design Introduction to Design Patterns
Copyright © Active Frameworks, Inc.. - All Rights Reserved - V2.0 Introduction - Page L1-1 PS95&96-MEF-L1-1 Dr. M.E. Fayad Creationa l Paradigm.
Copyright © Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Algorithm Programming Structural Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Client/Server Software Architectures Yonglei Tao.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
BTS530: Major Project Planning and Design The Composite Pattern All References and Material From: Design Patterns,by (GOF!) E.Gamma, R.Helm, R.Johnson,
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
SDP Structural Pattern. SDP-2 Structural Patterns Concerned with how classes and objects are composed to form large structures Class Patterns use.
Structural Design Patterns
ECE450S – Software Engineering II
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns V More Structural Patterns.
02 - Structural Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design Patterns Structural Patterns. Adapter Convert the interface of a class into another interface clients expect Adapter lets classes work together.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Proxy.
Proxy Design Pattern By:Diksha Agarwal.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Introduction
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Software Design and Architecture Muhammad Nasir Structural Design Patterns
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
Software Design Patterns
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Decorator Design Pattern
More Design Patterns 1.
Design Patterns Satya Puvvada Satya Puvvada.
Design Patterns - A few examples
More Design Patterns 1.
Design Patterns A Case Study: Designing a Document Editor
Jim Fawcett CSE776 – Design Patterns Summer 2003
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
Structural Patterns: Adapter and Bridge
Lesson 5: More on Creational Patterns
Informatics 122 Software Design II
Software Design Lecture : 38.
Software Design Lecture 10.
Presentation transcript:

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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).

Copyright © 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

Copyright © 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)

Copyright © 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()

Copyright © 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

Copyright © 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.

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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.

Copyright © 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.

Copyright © 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.

Copyright © 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

Copyright © 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();

Copyright © 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.

Copyright © 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)

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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()

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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

Copyright © 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?

Copyright © 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.