© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Reactor Design Patterns: Command and Observer.

Slides:



Advertisements
Similar presentations
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Generator Design Patterns: The Factory Patterns.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
MVC Nick Lopez Duplication of course material for any commercial purpose without the explicit written permission of the professor is prohibited.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Seyed Mohammad Ghaffarian ( ) Computer Engineering Department Amirkabir University of Technology Fall 2010.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Establishing the overall structure of a software system
Object-Oriented Analysis and Design
Application Architectures Vijayan Sugumaran Department of DIS Oakland University.
1 Observer Design Pattern By Eric Perret Pages in Applying UML and Patterns.
Event-Driven Architecture Team 4 – Idris Callins, Jestin Keaton, Bill Pegg, Steven Ng.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 11 Slide 1 Architectural Design.
BY VEDASHREE GOVINDA GOWDA
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
System Analysis & Design
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
MVC pattern and implementation in java
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Tony Nguyen.  Architecture  Advantages  Disadvantages  Setup JSF Environment  JSF & Database  Conclusion  Demo.
MVC and MVP. References enter.html enter.html
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 A Discipline of Software Design.
An Introduction to Software Architecture
Architectural Design portions ©Ian Sommerville 1995 Establishing the overall structure of a software system.
Case Studies on Design Patterns Design Refinements Examples.
Architectural Design To explain the advantages and disadvantages of different distributed systems architectures To discuss client-server and distributed.
Architecture styles Pipes and filters Object-oriented design Implicit invocation Layering Repositories.
Architecture GRASP Realization of use cases in interaction diagrams Design class diagram Design ( how )
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 09. Review Introduction to architectural styles Distributed architectures – Client Server Architecture – Multi-tier.
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Architectural Styles.
Architectural Design lecture 10. Topics covered Architectural design decisions System organisation Control styles Reference architectures.
 Repository Model  Client-Server Model  Layered Model  Modular decomposition styles  Object Models  Function Oriented Pipelining  Control Styles.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Design CIS 4800 Kannan Mohan Department of CIS Zicklin School of Business, Baruch College Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2008 Course.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 13. Review Shared Data Software Architectures – Black board Style architecture.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
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.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns: Iteration and Iterators.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.
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.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns Categories.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Broker Design Patterns: Façade and Mediator.
Model View ViewModel Architecture. MVVM Architecture components.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Broker Design Patterns: Adapters and Proxy.
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Slide 1 Chapter 8 Architectural Design. Slide 2 Topics covered l System structuring l Control models l Modular decomposition l Domain-specific architectures.
Model View Presenter Design Pattern Jay Smith PMO Architect and Evangelist Tyson Foods, Inc.
CHAPTER 5 GENERAL OOP CONCEPTS.
Delegates and Events 14: Delegates and Events
Observer Design Pattern
Observer Design Pattern
Part 3 Design What does design mean in different fields?
Model-View-Controller Patterns and Frameworks
Model-View-Controller (MVC) Pattern
Patterns.
Software Architecture
Observer Design Pattern
An Introduction to Software Architecture
Design pattern Lecture 9.
Advanced ProgramMING Practices
Design Patterns Lecture part 1.
Advanced ProgramMING Practices
Presentation transcript:

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Reactor Design Patterns: Command and Observer

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2 Objectives  To present the structure, behavior, and characteristics of reactor patterns  To compare and contrast the uses of reactor patterns and event-driven architectures in event-driven design  To present the Command pattern  To present the Observer pattern

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Topics  Event-driven design  Reactor patterns  Function objects  The Command pattern  The Observer pattern

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 Event-Driven Design  Event-driven design is an approach to program design that focuses on events to which a program must react. An event is a significant occurrence Contrasts with stepwise refinement  Event handlers are components that react to or respond to events.  Reactor patterns assist in event-driven design.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 Reactor Pattern Structure The Client must access the Target and the Reactor so it can register the Reactor with the Target.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6 Behavioral Phases  Setup Phase—The Client registers the Reactor with the Target. Client interacts with the Target  Operational Phase—The Reactor responds to events in the Target on behalf of the Client. Client is not involved

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7 Reactor Pattern Behavior

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8 Reactor Pattern Advantages  Client and Target Decoupling—Once the client registers the reactor with the target, the client and target need not interact again.  Low Reactor and Target Coupling—The target only knows that the reactor is a receiver of event notifications.  Client Decomposition—The reactor takes over client responsibilities for reacting to target events.  Operation Encapsulation—The event handler in a reactor is an encapsulated operation that can be invoked in other ways for other reasons.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Event-Driven Architectures vs. Reactor Patterns  Commonalities Support event-driven design Event announcement and handling Two-phase behavior  Differences Level of abstraction Event dispatcher completely decouples targets and reactors Event dispatchers are complex and harder to use Event dispatch may damage performance

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10 Function Objects  In some languages operations can be treated as values (stored, passed, etc.).  This is not usually true in object-oriented languages—use objects to encapsulate operations. A function object or functor is an object that encapsulates an operation; the encapsulating class is a function class.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 Function Object Example 1

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 Function Object Example 2 Collection sortCollection = new ArrayList() sortCollection.add( new InsertionSort() ) sortCollection.add( new BubbleSort() )... print "Sort Time1 Time2 Time3... Timek" for each element sorter of sortCollection print sorter.toString() for each array a startTime = now() sorter.sort(a) endTime = now() print( endTime – startTime ) printline

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13 Function Object Advantages  Additional features can be added to the function class besides the encapsulated operation.  The function class can include operations and data that the encapsulated operation needs.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14 The Command Patterns  The reactor is a function object  Simple and very widely used way to implement callback functions in user interfaces  Analogy: emergency phone number card

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15 Command Pattern Structure

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16 Command Pattern Behavior

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Command Pattern Example

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 When to Use the Command Pattern  Use the Command pattern to delegate a client’s response to events from an invoker class to another class that encapsulates the reaction.  Use the Command pattern to decompose clients, lower coupling between clients and invokers, and to encapsulate event- handling code.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 The Observer Pattern  Reduces coupling between classes while preserving their ability to interact  Can be used whenever one or more objects (observers) must react to events in another object (subject)  Analogy: current awareness service

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20 Observer in the MVC Architecture  A model in an MVC architecture can keep track of its views and controller Strongly couples the model to its views and controllers Changing the UI forces changes in the model  The model can be a subject and the views and controllers can be observers Decouples the model from its views and controllers Changing the UI has no effect on the model

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21 Subject and Observer Operations  Subject Registration operations Notification control operations Query operations  Observer Notification operation

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 22 Observer Pattern Structure

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23 Observer Pattern Behavior

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24 Observer Pattern Example

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25 When to Use the Observer Pattern  Use the Observer pattern whenever one object must react to changes in another object, especially if the objects must be loosely coupled. User interface components Clock-driven components  The main drawback of the Observer pattern is that notification may be expensive. This can be dealt with in several ways depending on the situation.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26 Summary 1  Reactor patterns use a Reactor to which a Client delegates responsibility for responding to events in a Target.  Reactor patterns help realize event-driven designs in a cheaper and easier way than event-driven architectures at the expense of slightly higher component coupling.  The reactor patterns help decouple targets from their both clients and reactors.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27 Summary 2  The Command pattern uses a function object as a reactor; the function object encapsulates the reaction and can be used for other purposes as well.  The Observer pattern has a subject with which observers register; the subject then notifies its observers of changes, and the observers query to subject to determine how to react.