Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
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.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
BY VEDASHREE GOVINDA GOWDA
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
CS 210 Introduction to Design Patterns September 28 th, 2006.
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.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Software Design Patterns
CS 210 Introduction to Design Patterns September 7 th, 2006.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Lexi case study (Part 2) Presentation by Matt Deckard.
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.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
ECE450S – Software Engineering II
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Title Carolina First Steering Committee October 9, 2010 Online Voting System Design Yinpeng Li and Tian Cao May 3, 2011.
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.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Introduction to Object-Oriented Programming Lesson 2.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Observer Pattern.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CS 5150 Software Engineering Lecture 16 Program Design 3.
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.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
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.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Chapter 10 Design Patterns.
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
MPCS – Advanced java Programming
Behavioral Design Patterns
Observer Design Pattern
Presented by Igor Ivković
Model-View-Controller Patterns and Frameworks
Introduction to Behavioral Patterns (1)
CSC 480 Software Engineering
Behavioral Patterns Part-I introduction UNIT-VI
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
Advanced ProgramMING Practices
Advanced ProgramMING Practices
CSC 480 Software Engineering
Presented by Igor Ivković
Presentation transcript:

Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.  They use inheritance to control code flow.  They define and produce process and run-time flow and identify hierarchies of classes and when and where they become instantiated in code. class instance  Some define class instance work from one class to another  Some hand off work from one class to another, and placeholdersother functionality.  Some provide placeholders for other functionality.

Behavioral Patterns Command pattern Interpreter Iterator pattern Memento pattern Observer pattern Strategy pattern Visitor pattern

Command Pattern  An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes.  This is the definition of a macro  The Macro represents a command that is built from the reunion of a set of other commands, in a given order. without knowing requested operation requesting object  Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object.

Command Pattern  Command pattern allows requests to an object to exist as objects. What does that mean? for some function an object request  It means that if you send a request for some function to an object, the command object can house that request inside the object. undoing redoing storing an action  This is useful in the case of undoing or redoing some action, or simply storing an action in a request queue on an object.  When you send the request, it is stored in the object.  if you need to access that same request or apply the request or instead of calling the object’s method directly. some method on the request to an object, you can use the request object instead of calling the object’s method directly.

Command Pattern  The Command pattern has three main components: the commands and the receiver The invoker component acts as a link between the commands and the receiver houses the receiver and the individual commands as they are sent. The command is an object that encapsulates a request to the receiver. each request. The receiver is the component that is acted upon by each request.

Command Pattern  Command design pattern provides the options to queue commands, undo/redo actions other manipulations. Intent  encapsulate a request in an object  allows the parameterization of clients with different requests  allows saving the requests in a queue

Implementation The implementation of the Command design pattern is quite simple  Command - declares an interface for executing an operation  ConcreteCommand - extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver.  It defines a link between the Receiver and the action.  Client - creates a ConcreteCommand object and sets its receiver;  Invoker - asks the command to carry out the request;  Receiver - knows how to perform the operations;

Implementation steps  The Client asks for a command to be executed.  The Invoker takes the command  encapsulates it  places it in a queue, and  The Concrete Command that is in charge of the requested command, sending its result to the Receiver.

Placing Orders for Buying and Selling Stocks public interface Order { public abstract void execute ( ); } // Receiver class. class StockTrade { public void buy() { System.out.println("You want to buy stocks"); } public void sell() { System.out.println("You want to sell stocks "); }

// Invoker. class Agent { private m_ordersQueue = new ArrayList(); public Agent() { } void placeOrder(Order order) { ordersQueue.addLast(order); order.execute(ordersQueue.getFirstAndRemove()); }

//ConcreteCommand Class. class BuyStockOrder implements Order { private StockTrade stock; public BuyStockOrder ( StockTrade st) { stock = st; } public void execute( ) { stock. buy( ); }

//ConcreteCommand Class. class SellStockOrder implements Order { private StockTrade stock; public SellStockOrder ( StockTrade st) { stock = st; } public void execute( ) { stock. sell( ); }

// Client public class Client { public static void main(String[] args) { StockTrade stock = new StockTrade(); BuyStockOrder bsc = new BuyStockOrder (stock); SellStockOrder ssc = new SellStockOrder (stock); Agent agent = new Agent(); agent.placeOrder(bsc); // Buy Shares agent.placeOrder(ssc); // Sell Shares }

Observer Pattern  This pattern facilitates communication between a parent class and any dependent child classes, allowing changes to the state of the parent class to be sent to the dependent child classes.  We can use this pattern to allow the state changes in a class to be sent to all its dependent classes.  The class relationship is one-to-many between the class and all its dependents.

Observer Pattern The pattern generally consists of two base classes.  The first is called the Subject class  this class acts as the notification engine. receivers subject notifications  The Observer classes act as receivers of the subject notifications. concrete subject concrete observer.  From these two base class types the concrete implementations for each type are derived: concrete subject and concrete observer.

Observer Pattern  we have a stock system which provides data for several types of client.  We want to have a client implemented as a web based application  we will later need to add clients for mobile devices, Palm or Pocket PC,  Later the system have a requirement to notify the users with sms alerts.  Now it's simple to see what we need from the observer pattern  we need to separate the subject(stocks server) from it's observers(client applications) in such a way that adding new observer will be transparent for the server.

Observer Pattern  Observer pattern defines a one-to-many dependency between objects  When one object changes state, all its dependents are notified and updated automatically.

Observer Pattern  Observable - interface or abstract class defining the operations for attaching and de-attaching observers to the client (Subject)  ConcreteObservable - concrete Observable class. It maintain the state of the object and when a change in the state occurs it notifies the attached Observers.  Observer - interface or abstract class defining the operations to be used to notify this object.  ConcreteObserverA, ConcreteObserver2 - concrete Observer implementations.

Observer Pattern ConcreteObservable  The main framework instantiate the ConcreteObservable object.  it instantiate and attaches the concrete observers to it using the methods defined in the Observable interface.  Each time the state of the subject it's changing it notifies all the attached Observers using the methods defined in the Observer interface.  When a new Observer is added to the application, all we need to do is to instantiate it in the main framework and to add attach it to the Observable object.  The classes already created will remain unchanged

Observer Pattern Example  Model View Controller Pattern  The observer pattern is used in the model view controller (MVC) architectural pattern.  In MVC the this pattern is used to decouple the model from the view.  View represents the Observer and the model is the Observable object.

Observer Pattern Example  Event management  the Observer patterns is extensively used.  Swing and.Net are extensively using the Observer pattern for implementing the events mechanism.

Observer Pattern: a News Agency

 A news agency gather news and publish them to different subscribers.  We need to create a framework for and agency to be able to inform immediately when event occurs  The subscribers can receive the news in different ways: s, SMS,...  The solution need to be extensively enough to support new types of subscribers  New subscribers may be new communication technologies

Observer Pattern: a News Agency  The agency is represented by an Observable(Subject) class named NewsPublisher.  This one is created as an abstract class because the agency want to create several types of Observable objects  in the beginning only for business news, but after some time sport and political new will be published.  The concrete class is BusinessNewsPublisher..

Observer Pattern: a News Agency  In the main class a publisher(Observable) is built and a few subscribers(Observers).  The subscribers are subscribed to the publisher and they can be unsubscribed.  new types of subscribers can be easily added(instant messaging,...)  new types of publisherscan be easily added (Weather News, Sport News,...).

Observer Pattern: a News Agency  The observer logic is implemented in NewsPublisher.  It keeps a list of all it subscribers and it informs them about the latest news.  The subscribers are represented by some observers (SMSSubscriber, Subscriber).  Both the observers mentioned above are inherited from the Subscriber.  The subscriber is the abstract class which is known to the publisher.  The publisher doesn't know about concrete observers, it knows only about their abstraction