OOMPA Lecture 9 Design Patterns Composite Pattern Observer Pattern.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Lecture 12 Observer Pattern UML Class Diagrams (repetition) Use Case Diagrams Sequence Diagrams Statechart Diagrams.
 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 Design Patterns Composite Pattern Observer Pattern.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
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.
CSE Software Engineering: Analysis and Design, 2002Lecture 7B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/DMS/2002/15.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns CS is not simply about programming
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.
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.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
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.
© SERG Software Design (OOD Patterns) Object-Oriented Design Patterns Topics in Object-Oriented Design Patterns Compliments of Spiros Mancoridis Material.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Model View.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
ECE450S – Software Engineering II
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
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.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
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.
Design Patterns: MORE Examples
Observer Design Pattern
Composite Pattern SE2811 Software Component Design
More Design Patterns 1.
Design Patterns - A few examples
More Design Patterns 1.
Web Programming Language
Design Patterns A Case Study: Designing a Document Editor
Jim Fawcett CSE776 – Design Patterns Summer 2003
Observer Pattern 1.
Design pattern Lecture 9.
13. Composite Pattern SE2811 Software Component Design
Informatics 122 Software Design II
13. Composite Pattern SE2811 Software Component Design
Presentation transcript:

OOMPA Lecture 9 Design Patterns Composite Pattern Observer Pattern

Design Patterns Literature: Design Patterns Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Addison-Wesley, Professional Computing Series Thinking in Patterns with JAVA Bruce Eckel, (electronic book, preliminary)

Design Patterns ”Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” Christopher Alexander

Design Patterns Each design pattern names, explains and evaluates an important and recurring design in object-oriented systems. A design pattern makes it easier to reuse successful designs and architectures. Design patterns help you choose design alternatives that make a system reusable and avoid alternatives that compromise reusability.

Design Patterns The pattern name is a handle to describe the design problem, its solutions and consequences. The problem describes when to apply the pattern. It explains the problem and its context. The solution describes the elements (classes and objects) that make up the design, their relationships, responsibilities and collaborations. The consequences are the results and trade-offs of applying the pattern. They may address language and implementation issues as well.

Creational Patterns Abstract Factory, Factory Method, Singleton Concerned with object creation Who creates what, when and how

Structural Patterns Adapter, Façade, Composite Concerned with the composition of classes or objects Structural class patterns use inheritance to compose interfaces or implementations. Structural object patterns describe ways to compose objects to realize new functionality.

Behavioral Patterns Command, Observer, Mediator, Strategy Characterize the ways in which classes or objects interact and distribute responsibility Behavioral patterns are concerned with algorithms and the assignment of responsibilties between objects. Behavioral patterns also describe patterns of communication between objects and classes.

Design Guidelines Design objectives High cohesion Low coupling Protected variations Information Expert Polymorphism Indirection

Composite Pattern Intent: Compose Objects into tree structures to represent part-whole hierarchies. Composite let clients treat individual objects and compositions of objects uniformly. Motivation: Graphics applications like drawing editors let users build complex diagrams out of simple components. The user can group components to form larger components, which in turn can be grouped to form still larger components.

Composite Pattern A simple implementation could define classes for graphical primitives such as Rectangles and Ellipses plus other classes that act as containers for these primitives. The problem is that code that uses these classes must treat primitive and container objects differently, even if most of the time the user treats them identically. Having to distinguish these objects makes the application more complex. The Composite pattern describes how to use recursive composition so that clients don’t have to make that distinction.

Composite Pattern The key to the Composite pattern is an abstract class that represents both primitives and their containers. For the graphic system, the abstract class Graphic declares operations like Draw, Move, Delete that are specific to graphical objects. It also declares operations that all composite objects share, such as operations for accessing and managing its children, like Add, UnGroup.

Composite Pattern Graphic Draw() Move() Add(Graphic) UnGroup() Rectangle Draw() Move() Ellipse Draw() Move() Composite Draw() Move() Add(Graphic) UnGroup() graphics N 1

Composite Pattern

The subclasses Ellipse, Rectangle etc. Define primitive figure objects. These classes implement Draw to draw rectangles, ellipses resepctively. Since primitive figures have no children, none of these subclasses implements child related operations such as Add or Ungroup.

Composite Pattern The Composite class defines an aggregate of Figure objects. Composite implements Draw to call Draw on its children, and it implements child-related operations accordingly. Because the Composite interface conforms to the Figure interface, Composite can compose other Composites recursively.

Composite Tree Structure Composite Object Composite Object Ellipse Object Rectangle Object Rectangle Object Ellipse Object Rectangle Object

Abstract Base Class Graphic abstract class Figure { // graphic related operations abstract void Draw(Window w); abstract void Move(int dx, int dy); // child related operations (for Composite) abstract void Add(Figure f); abstract Vector Ungroup(); }

Subclass Composite class Composite extends Figure { private Vector figures; void Draw(Window w) { for (int i=0; i<figures.size(); i++) ((Figure) figures[i]).Draw(w); } void Move(int dx, int dy) {... } void Add(Figure figure) { figures.add(figure); } Vector Ungroup() {... } }

Subclass Rectangle class Rectangle extends Figure { private int x, y; private int width, height; void Draw(Window w) {...} void Move(int dx, int dy) { x+=dx; y+=dy; } void Add(Figure g) { throw new AddToNonCompositeException(); } Vector Ungroup() {... } }

Composite Pattern General Component Operation() Add(Component) Remove(Component) GetChild(int n) Leaf Operation() Composite Operation() Add(Component) Remove(Component) GetChild(int n children N 1 Forall g in children g.Operation() Client

Composite Pattern Class Diagram

Composite Sequence Diagram

Applicability of Composite Use the Composite pattern when You want to represent part-whole hierarchies of objects You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

Composite Tree Structure Composite Object Composite Object Leaf Object Leaf Object Leaf Object Leaf Object

Participants of Composite Component Declares the interface for objects in the composition Implements default behavior for the interface common to all classes, as appropriate Declares an interface for accessing and managing its child components (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that is appropriate

Participants of Composite Leaf Represents leaf objects in the composition. A leaf has no children. Defines behavior for primitive objects in the composition

Participants of Composite Composite Defines behavior for components having children Stores child components Implements child-related operations in the Component interface Client Manipulates objects in the composition through the Component interface

Collaborations of Composite Client uses 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 it is usually forwards requests to its child components, possibly performing additional operations before or/and after forwarding.

Consequences of Composite Makes the client simple: Clients can treat composite structures and individual objects uniformly. Clients normally don’t know and should not care whether they are dealing with a leaf or composite component. Makes it easier to add new kinds of components. Newly defined Composite or Leaf classes work automatically with exisiting structures and client code. Can make your design overly general. The disadvantage of making it easy to add new components is that it makes it hard to restrict the components of a composite.

Implementation Issues Explicit parent references Maintaining references from child components to their parent can simplify the traversal and management of a composite structure. The usual place to define the parent reference is in the Component class. Leaf and Composite can inherit the reference and the operations that manage it. It is necessary to maintain the invariant that children of a composite have as their parent the composite that in turn has them as children.

Implementation Issues class Component { protected Component parent;... } void Composite::Add(Component c) { c.parent=this;... }

Implementation Issues Declaring the child management operations Defining the child management interface at the root of the class hierarchy gives you transparency, because you can treat all components uniformly. It costs you safety, however, because clients may try to do meaningless things like add objects to leaves. Defining child management in the Composite class gives you safety because any attempt to add objects to leaves will be caught at compile- time. But you loose transparency, because leaves and composites have different interfaces.

Composite Pattern Component Operation() Add(Component) Remove(Component) GetChild(int n) Leaf Operation() Composite Operation() Add(Component) Remove(Component) GetChild(int n children N 1 Forall g in children g.Operation() Client

Composite Pattern Class Diagram

Child Management in Base Class abstract class Component { abstract void Operation(); abstract void Add(Component c); // child management functions declared in base class } class Composite extends Component { private Vector children; void Operation() {... } void Add(Component c) { children.add(c); }

Safety vs. Transparency Vector components = new Vector(); components.add(new Leaf()); components.add(new Composite()); ((Component) components.elementAt(2)). Add(components.elementAt(0)); // transparent works because Add() is member function of // base class Component ((Component) components.elementAt(1)). Add(components.elementAt(0)); // unsafe fails as it tries to add a Leaf to a Leaf, // possible solution throw an exception for Leaf::Add()

Composite Class Diagram

Composite Pattern Component Operation() Leaf Operation() Composite Operation() Add(Component) Remove(Component) GetChild(int n children N 1 Forall g in children g.Operation() Client

Child Management in Subclass abstract class Component { abstract void Operation(); } class Composite extends Component { private Vector components; void Operation() {... } void Add(Component c) { components.add(c); } // child management functions declared in subclass }

Safety vs. Transparency Vector components=new Vector(); components.add(new Leaf()); components.add(new Composite()); ((Component) components.elementAt(2)). Add(components.elementAt(0)); // non-transparent : // fails because Add() is not a member function of // base class Component ((Composite) components.elementAt(2)). Add(components.elementsAt(0)); // explicit type cast to Composite ((Component) components.elementAt(1). Add(components.elementAt(0)); // safe : causes compiler error because Add() is not a member // function of base class Component

Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Example: Graphical editor with multiple views of the same graphic objects. If a graphical object is manipulated in one view, it notifies the other views to display the changes made to it.

Observer Pattern A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. The key objects in the Observer pattern are Subject and Observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. In response, each observer will query the subject to synchronize its state with the subject´s state.

Observer Pattern The interaction between subject and observer in the observer pattern is also known as publish-subscribe. The subject is the publisher of notifications. It sends out these notifications without having to know who its observers are. Any number of observers can subscribe to receive notifications.

Observer Applicability Use the Observer pattern in any of the following situations When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independtly. When a change to one object requires changing others, and you do not know how many objects need to be changed. When an object should be able to notify other objects without making assumptions about who these objects are.

Observer Structure Subject Attach(Observer) Detach(Observer) Notify() Observer Update() observers Forall o in observers o->Update() ConcreteSubject GetState() SetState() subject_state ConcreteObserver Update() observer_state subject observer_state= subject->GetState()

Observer Class Diagram

Observer Participants Subject Knows its observers. Any numberof Observer objects may observe a subject. Provides an interface for attaching and detaching Observer Objects. Observer Defines an updating interface for objects that should be notified of changes in a subject

Observer Participants ConcreteSubject Stores a state of interest to ConcreteObserver objects. Sends a notification to its observers when its state changes. ConcreteObserver Maintains a reference to a ConcreteSubject object Stores state that should stay consistent with the subject state. Implements the Observer updating interface to keep its state consistent with the subject state.

Observer Collaborations ConcreteSubject notifies its observers whenever a change occurs that could make its observer’s state inconsistent with its own. After being informed of a change in the ConcreteSubject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the object.

Observer Sequence Diagram ConcreteSubject Object ConcreteObserver ObjectA ConcreteObserver ObjectB Notify() SetState() Update() GetState() Update() GetState()

Observer Sequence Diagram

Note that the Observer object that initiates the change request with SetState() postpones its update until it gets a notification from the subject. In this scenario Notify() is called by the subject, but it can be called by an observer or by another kind of object (see implementation issues)

Observer Sequence Diagram

Observer.h #include class Subject; class Observer { public: Observer(); virtual void update(Subject* subject)=0; };

Observer.h class Subject { public: virtual ~Subject() {}; void attach(Observer* obs); void detach(Observer* obs); void notify(); private: list observers_; };

Observer.h class ConcreteSubject : public Subject { public: ConcreteSubject(string newname); void setstate(int newstate); int getstate(); string getname(); private: int state_; string name_; };

Observer.h class ConcreteObserver : public Observer { public: ConcreteObserver(string newname); void attachsubject(ConcreteSubject* subject); void detachsubject(ConcreteSubject* subject); virtual void update(Subject* subject); private: list subjects_; string name_; };

Observer.cc #include "observer.h" Observer::Observer() {}; void Subject::attach(Observer* obs) { observers_.push_back(obs); }; void Subject::detach(Observer* obs) { observers_.erase(find(observers_.begin( ), observers_.end(),obs)); };

Observer.cc void Subject::notify() { for (list ::iterator iter=observers_.begin(); iter!=observers_.end(); ++iter) { (*iter)->update(this); }; ConcreteSubject::ConcreteSubject(string newname) : Subject(), name_(newname), state_(0) { };

Observer.cc void ConcreteSubject::setstate(int newstate) { state_=newstate; notify(); // notify all observers that state // has changed } int ConcreteSubject::getstate() { return state_; } string ConcreteSubject::getname() { return name_; }

Observer.cc ConcreteObserver::ConcreteObserver(string newname) : Observer(), subjects_(0), name_(newname) { }; void ConcreteObserver::AttachSubject (ConcreteSubject* subject) { subjects_.push_back(subject); subject->attach(this); };

Observer.cc void ConcreteObserver::detachsubject (ConcreteSubject* subject) { subjects_.erase(find(subjects_.begin(), subjects_.end(),subject)); subject->detach(this); cout << "Subject detached!" << endl; };

Observer.cc void ConcreteObserver::update(Subject* subject) { list ::iterator iter; iter=find(subjects_.begin(),subjects_.e nd(),subject); if (iter != subjects_.end()) cout getname() getstate() << endl; }

Main.cc #include "observer.h" int main(){ ConcreteSubject s1("s1"); ConcreteSubject s2("s2"); ConcreteObserver o1("o1"); ConcreteObserver o2("o2"); ConcreteObserver o3("o3"); o1.AttachSubject(&s1); o1.AttachSubject(&s2); o2.AttachSubject(&s1); o3.AttachSubject(&s2); s1.SetState(5); s2.SetState(2); }

Program Output In s1.setState(5) notify observers o1 and o2 Observer o1 queries Subject s1 for new state 5 Observer o2 queries Subject s1 for new state 5 In s2.setState(2) notify observers o1 and o3 Observer o1 queries Subject s2 for new state 2 Observer o3 queries Subject s2 for new state 2

Observer Consequences The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing observers, and vice versa. It lets you add observers without modifying the subject or other observers.

Observer Consequences Abstract coupling between Subject and Object All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class. The subject does not know the concrete class of any observer. Support for broadcast communication Unlike and ordinary request, the notification that a subject sends need not specify its receiver. The notification is broadcast automatically to all interested objects that subscribed to it.

Observer Consequences Unexpected Updates Because observers have no knowledge of each other’s presence, they can be blind to the ultimate cost of changing the subject. A seemingly innocuous operation to the subject may cause a cascade of updates to observers and their dependent objects. This problem is aggravated by the fact that the simple update protocol provides no details on what changed in the subject. Without additional protocol observers have to query the entire state of the subject to deduce the changes.

Observer Implementation Mapping subjects to their obervers The simplest way for a subject to keep track of the obervers it should notify is to store references to them explicitly in the subject. An alternative is to use an associative look-up (multimap) to maintain the subject-observer mapping.

Observer Implementation Observing more than one subject It might make sense in some situations for an observer to depend on more than one subject. It is necessary to extend the Update interface in such cases to let the observer know which subject is sending the notification. The subject can simply pass itself as a parameter in the Update operation, thereby letting th observer know which subject to examine.

Observer Implementation Who triggers the update The subject and its observers rely on the notification mechanism to stay consistent. But what object actually calls Notify() to trigger the update? There are two options. Have state-setting operations on the Subject call Notify after they change the subject’s state Make clients responsible for calling Notify at the right time.

Observer Implementation Subject calls Notify The advantage of this approach is that clients do not have to remember to call Notify on the subject. The disadvantage is that several consecuitve operations will cause several consecutive updates, which may be inefficient Clients calls Notify The advantage here is that the client can wait to trigger the update until after a series of state changes has been made. The disadvantage is that clients have an added responsibility to trigger the update, causing possible errors.

Subject calls Notify

Client/Observer calls Notify

Observer Implementation Subject calls Notify void ConcreteSubject::setstate(int newstate) { state_=newstate; notify(); // notify all observers that // state has changed } Subject s1; subject.setstate(6); // automatic call to notify Subject.setstate(5);

Observer Implementation Observer or client calls Notify void ConcreteSubject::setstate(int newstate) { state_=newstate; } Subject s1; subject.setstate(6); subject.setstate(5); Subject.notify(); // explicit call to notify

Observer Implementation Dangling references to deleted subjects Deleting a subject should not produce dangling references in its observers. One way to avoid dangling references is to make the subject notify its observers as it is deleted so that they can reset their reference. In general simply deleting the observers is not an option, because other objects may reference them, or they may be observing other subjects as well.

Observer Implementation Making sure subject is self-consistent before notification It is important to make sure Subject is self- consistent before calling Notify, because obervers query the subject for its current state in the course of updating their own state. This self-consistency rule is easy to violate when Subject subclass operations call inherited operations.

Observer Implementation Avoiding observer-specific update protocols : the push and pull model Implementations of the Observer pattern often have the subject broadcast additional information about the change. The subject passes this information as an argument to Update. Push model: the subject sends obervers detailed information about the change Pull model: the subject sends only a minimal notification and observers ask for details explicitly

Observer Implementation Pull model: The pull model emphasizes the subject’s ignorance of its obervers. The pull model may be inefficient, because Observer classes must infer what changed without help from Subject. Push model: The push model assumes that the subject knows something about its obervers’ needs. The push model might make observers less reusable because Subject classes make assumptions about Observer classes that might not always be true.

Observer Implementation Specifying modifications of interest explictly. You can improve update efficiency by extending the subject’s registration interface to allow registering observers only for specific events of interest. When such an event occurs, the subject informs only those observers that have registered interest in that event. One way to support this is to use the notion of Aspects for Subject objects. void Attach(Observer o, Aspect interest); void Update(Subject s, Aspect interest);