Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Design Patterns Pepper. Find Patterns Gang of Four created 23 Siemens published another good set x
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
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.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Client/Server Software Architectures Yonglei Tao.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
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.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Programming in C# Observer Design Pattern
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
Distribution and components. 2 What is the problem? Enterprise computing is Large scale & complex: It supports large scale and complex organisations Spanning.
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.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
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.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Proxy.
Proxy Design Pattern By:Diksha Agarwal.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Software Design and Architecture Muhammad Nasir Structural Design Patterns
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Chapter 10 Design Patterns.
Observer Design Pattern
Design Patterns with C# (and Food!)
object oriented Principles of software design
More Design Patterns 1.
Design Patterns Satya Puvvada Satya Puvvada.
More Design Patterns 1.
Informatics 122 Software Design II
Informatics 122 Software Design II
Presentation transcript:

Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel

Where Do Surrogates Fit into This Introduction – What is a surrogate? Webster’s Dictionary defines it as “one appointed to act in place of another”. The general context is that of a lawyer who represents a client before a court. A more general concept is to consider a surrogate as someone or something that performs a process or task that is unable or unwilling to be performed by another. Garbage collector, postal service, etc. From the software perspective, surrogates can be thought of as “helper” type classes. These patterns provide valuable services by allowing aspects of a design to vary based on the needs of the software. These varying aspects include such things as interfaces, implementations, structures, responsibilities, etc. (Patterns 30)

Where Do Surrogates Fit into This Things to consider when selecting Surrogates: The requirements of the design may be such that multiple surrogate patterns should be implemented to obtain the cleanest design solution. In the file system example, the COMPOSITE pattern served as a solution when considering files and folders/directories. However, the PROXY served as a better solution when implementing symbolic links. One or more additional surrogate patterns may be required in the implementation in support of the software design. In the file system example, the OBSERVER pattern was used to notify all proxies when a file was deleted to prevent “dangling” pointers.

Where Do Surrogates Fit into This Working with base classes: As a design evolves and the implementation develops, there is a tendency to treat a base class as a dumping ground for additional capability. This results in base classes that are tough to understand, cumbersome maintain, and difficult to implement. The author makes the assertion that a primary goal in the design of bases classes is to provide a minimal set of operations allowing open-ended functionality. If you find yourself adding functionality to a base class to support a single subclass, perhaps a pattern is a desirable alternative.

Where Do Surrogates Fit into This Using multiple surrogate patterns: Some designs may result in multiple patterns being implemented in classes that are interdependent. These associations are result in “dense” composition of patterns. Having dense compositions can result in “profound” code (good stuff in a small space). However, care must be taken to ensure that the desired patterns do not get lost after implemented.

Where Do Surrogates Fit into This Reviewing surrogate usage: Before adding functionality to base classes in the design software, consider surrogate patterns as a better alternative. Use multiple surrogate patterns as needed to support the software design. This can serve to generate significant code in a small space. Be careful to ensure that software patterns do not get lost after it has been implemented (can happen in “dense” composition patterns).

Proxy Pattern Intent Provide a surrogate or placeholder for another object to control access to it Other Names Surrogate

Proxy Pattern: Applicability Forms of the proxy pattern: Remote proxy – Provides a local representative for an object in a different address space. Virtual Proxy – Creates expensive objects on demand. Protection Proxy – Controls access to the original object. Smart References – Additional functionality pointers. Smart Pointers Initial loading of persistent objects. Object locking.

Proxy Pattern: Participants Proxy Maintains a reference to the real subject. Provide identical interface to Subject so the Proxy can be substituted. Controls access to the real subject and may be responsible for creating and deleting the real subject. RealSubject Defines the real object that the proxy represents. Subject Defines the common interface for RealSubject and Proxy so that the proxy to be used wherever a RealSubject is expected.

Proxy Pattern: Structure

Proxy Pattern: Example class Image; class ImagePtr { public: ImagePtr(const char* file); virtual ~ImagePtr(); virtual Image* operator->(); virtual Image& operator*(); private: Image*_image; const char* _file; Image* LoadImage(); }; Image* ImagePtr::LoadImage(){ If (_image == 0 ) { _image = LoadAnImageFile(_file); } return _image; } Image* ImagePtr::operator->() { return LoadImage(); } Image& ImagePtr::operator*(){ return *LoadImage(); } To implement the Real Subject methods: ImagePtr image = ImagePtr(“aFile”); image->Draw(Point(50,100)); //(image.operator->())->Draw(Point(50,100))

Proxy Pattern: Consequences Each proxy introduces a level of indirection. This may result in hiding detail from the implementer. A remote Proxy can hide the fact that object resides in a different address space. A Virtual Proxy can perform optimizations such as creation on demand. Protection Proxy and Smart References can allow additional housekeeping tasks when object is accessed. Copy-On-Write This hides optimization in which an object is not copied until it’s attributes are modified.

Proxy: Related Patterns Adapter Provides a different interface to an object. Since a proxy may deny request based on access, the interface is will be a subset. Decorator Similar implementation to proxy but has a different purpose. This adds responsibilities to an object rather than controlling access.

Observer Pattern Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically Also Known As Dependents Publish-Subscribe

Observer Pattern: Motivation Partitioning a system into a collection of cooperating classes results in a need to maintain consistency between objects. It is undesirable to achieve consistency via tightly coupled dependencies as this limits reusabiltiy.

Observer Pattern: Applicability The following situations are prime examples for implementation of the observer pattern. When an abstraction has two aspects with one dependant on the other. When a change to one object requires changes to one or more other objects. When an object needs to notify other objects but remain loosely coupled.

Observer Pattern: Participants Subject Maintains knowledge of the Observers. Defines an interface for attaching and detaching Observer objects. Observer Defines an interface for objects to be notified when a subject changes.

Observer Pattern: Participants ConcreteSubject Maintains the state Subject. Notifies the Observers via the Subject when its state changes. ConcreteObserver Maintains a reference to the ConcreteSubject. Stores state that should stay consistent with the subject’s. Implements the updating interface.

Observer Pattern: Structure

Observer Pattern: Collaborations

Observer Pattern: Consequences Abstract coupling between Subject and Observer. Support for broadcast (multicast) communication. Unexpected updates caused by cascading updates to observers and their dependant objects.

Observer: Implementation Mapping subjects to their observers. Local storage is fast but may consume memory. Associative mapping saves space but requires is slower. Observing multiple subjects by extending the update method in the Observer. Who triggered the update? The state-setting operations in the Subject determine when the notification occurs. The client(s) are responsible for determining the appropriate time to notifiy.

Observer: Implementation Dangling references to deleted subjects. Subjects must notify Observers of the intent to be deleted. Ensuring Subject state is “self-consistent” before notification. Avoiding observer-specific update protocols. Push Model – Information delivered as part of the notification. Pull Model – Observers ask for details in response to the notification.

Observer: Implementation Specifying modifications of interest explicitly. State information is classified into “aspects” which are identified during notification. Encapsulating complex update semantics through the implementation of a “Change-Manager”. Takes responsibility of maintaining references to observers away from the Subject. Defines a particular update strategy. Updates all dependent observers at the request of a subject. Combining the Subject and Observer. May be useful when multiple inheritance not supported by language, both interfaces may reside in one class.

Observer: Related Patterns Mediator The ChangeManager encapsulates complex update semantics, thus acting as a mediator between the Subject and its Observers. Singleton ChangeManager may be unique and globally accessible.

Visitor Pattern Intent Lets you define a new operation without changing the classes on which they operate. Motivation Allows for increased functionality of a class(es) while streamlining base classes. As stated in the general section concerning surrogates, the author asserts that a primary goal of designs should be to ensure that base classes maintain a minimal set of operations. Encapsulates common functionality in a class framework.

Visitor Pattern Motivation (cont) Visitors avoid type casting that is required by methods that pass base class pointers as arguments. The following code describes how a typical class could expand the functionality of an existing composite. Void MyAddition::execute( Base* basePtr) { if( dynamic_cast (basePtr)){ // Perform task for child type A. } else if ( dynamic_cast (basePtr)){ // Perform task for child type B. } else if( dynamic_cast (basePtr)){ // Perform task for child type C. }

Visitor Pattern: Applicability The following situations are prime examples for implementation of the visitor pattern. When an object structure contains many classes of objects with different interfaces and you want to perform functions on these objects that depend on their concrete classes. When you want to keep related operations together by defining them in one class. When the class structure rarely change but you need to define new operations on the structure.

Visitor Pattern: Participants Visitor Declares a Visit Operation for each class of Concrete Elements in the object structure. Concrete Visitor Implements each operation declared by Visitor. Element Defines an Accept operation that takes the visitor as an argument.

Visitor Pattern: Participants Concrete Element Implements an accept operation that takes the visitor as an argument. Object Structure Can enumerate its elements. May provide a high level interface to all the visitor to visit its elements. May either be a composite or a collection.

Visitor Pattern: Structure

Visitor Pattern: Collaborations

Visitor Pattern: Consequences Makes adding new operations easier. Collects related functionality. Adding new Concrete Element classes is difficult. Can “visit” across class types, unlike iterators. Accumulates states as they visit elements. May require breaking object encapsulation to support the implementation.

Visitor: Implementation Implementation by the element classes. The base element class contains an abstract method to accept the visitor. Virtual void accept(Visitor&) = 0; The concrete element classes implement the accept call in an identical manner. void ElementA::accept(Visitor& v) {v.visit(this);} void ElementB::accept(Visitor& v) {v.visit(this);}

Visitor: Implementation Implementation of a single Visitor class can be used to implement simpler functionality. A single visitor class may contain the set of methods for implementing all concrete element types. void visit(ElementA*) ; The client implements the single visitor and be passed into any element pointer type call. Visitor v; anElementPtr->visit(v);

Visitor: Implementation Implementation of functionality for multiple visitor types is easy through inheritance. A base visitor class contains an abstract method as a placeholder for each concrete element. void visit(Element*)=0 ; Each concrete visitor implements the functionality for all concrete elements. void VisitorTypeA::visit(ElementA*) ; void VisitorTypeA::visit(ElementB*) ; void VisitorTypeB::visit(ElementA*); void VisitorTypeB::visit(ElementB*);

Visitor: Implementation The client implements the a instance of EACH visitor type passes into any element pointer type call. VisitorTypeA vA; VisitorTypeB vB; anElementPtr->visit(vA); anElementPtr->visit(vB);

Visitor: Related Patterns Composites Visitors can be used to apply an operation over an object structure defined by the composite pattern. Interpreter Visitors may be applied to do the interpretation.