Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

Chain of Responsibility Pattern Gof pp Yuyang Chen.
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Chubaka Producciones Presenta :.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Zaki Alasadi Supervisor:Dr noorhosseini.
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
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns Introduction. What is a Design Pattern?  A technique to repeat designer success.  Borrowed from Civil and Electrical Engineering domains.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Case Studies on Design Patterns Design Refinements Examples.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Computing IV Singleton Pattern Xinwen Fu.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
ECE450S – Software Engineering II
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
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.
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.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
Design Patterns Introduction
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
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.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
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:
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.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design Software University
Design Patterns Behavioral Patterns. Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Chapter 10 Design Patterns.
Introduction to Design Patterns
Behavioral Design Patterns
object oriented Principles of software design
Object Oriented Design Patterns - Behavioral Patterns
Behavioral Patterns Part-I introduction UNIT-VI
McDonald’s calendar 2007.
McDonald’s calendar 2007.
2015 January February March April May June July August September
Presentation transcript:

Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain of Responsibility Intent: Pass requests down a sequence of receivers until it’s handled. Command Intent: Encapsulate a request as an object so the request can be invoked when needed. Interpreter Intent: Represent the grammar of a language so its syntax may be viewed as a composite. Iterator Intent: Enable the sequential access of an aggregate object’s elements without exposing its structure. Mediator Intent: Encapsulate the interaction of a set of objects, preventing them from referring to each other explicitly. Memento Intent: Enable an undo capability without violating encapsulation. Observer Intent: Maintain a list of dependent objects, with automatic updates when one of the objects is updated. State Intent: Permit an object to alter its behavior when its internal state changes. Strategy Intent: Encapsulate each algorithm in a family of algorithms and make them interchangeable. Template Method Intent: Define an algorithm’s basic structure in an operation, deferring its details to subclasses. Visitor Intent: Separate an algorithm from the structure of the object on which it operates.

Behavioral Pattern: Chain of Responsibility C h a p t e r 5 – P a g e 129 On occasion, a system might have more than one mechanism for dealing with requests. The Chain of Responsibility pattern sets up the automatic selection of the appropriate mechanism for handling a particular request. By passing the request along the chain until some mechanism handles it, loose coupling is promoted between the mechanisms’ classes.

The Chain of Responsibility Pattern C h a p t e r 5 – P a g e 130 The Client initiates a request to a ConcreteHandler on the chain. The Handler defines an interface for handling the request and (optionally) implements the successor link. Each ConcreteHandler handles the requests for which it is responsible and can access its successor. If a ConcreteHandler can handle a request, it does so; otherwise it forwards the request to its successor.

C h a p t e r 5 – P a g e 131 Non-Software Example: ATM Withdrawal The base ATM class calls the handlers in succession until the requested withdrawal amount is achieved. Each handler repeatedly subtracts its designated amount from the sum requested until that sum exceeds the designated amount. The handler indicates to the ATM whether or not the next handler needs to be called.

C h a p t e r 5 – P a g e 132 Software Example: Calendar When requesting the number of days in a specified month, the 31DayHandler begins the chain, followed by the 30DayHandler, then the FebruaryHandler, and finally the MistakeHandler.

C h a p t e r 5 – P a g e 133 Calendar Code in C++ #include using namespace std; class MonthHandler { public: MonthHandler() { successorMonthHandler = 0; } void setSuccessor(MonthHandler *smh) { successorMonthHandler = smh; } void add(MonthHandler *mh) { if (successorMonthHandler) successorMonthHandler->add(mh); else successorMonthHandler = mh; } // The "chain" method in the base class always delegates to the next object virtual int numberOfDays(string monthName, int year) { return successorMonthHandler->numberOfDays(monthName, year); } protected: MonthHandler *successorMonthHandler; };

C h a p t e r 5 – P a g e 134 class ThirtyOneDayHandler: public MonthHandler { public: int numberOfDays(string monthName, int year) { if ( (monthName == "January") || (monthName == "March") || (monthName == "May") || (monthName == "July") || (monthName == "August") || (monthName == "October") || (monthName == "December") ) return 31; else return successorMonthHandler->numberOfDays(monthName, year); } }; class ThirtyDayHandler: public MonthHandler { public: int numberOfDays(string monthName, int year) { if ( (monthName == "April") || (monthName == "June") || (monthName == "September") || (monthName == "November") ) return 30; else return successorMonthHandler->numberOfDays(monthName, year); } };

C h a p t e r 5 – P a g e 135 class FebruaryHandler: public MonthHandler { public: int numberOfDays(string monthName, int year) { if (monthName == "February") if (year % 4 == 0) return 29; else return 28; else return successorMonthHandler->numberOfDays(monthName, year); } }; class MistakeHandler: public MonthHandler { public: int numberOfDays(string monthName, int year) { return 0; } };

C h a p t e r 5 – P a g e 136 void main() { string monthName; int year; int nbrOfDays; ThirtyOneDayHandler handler31; ThirtyDayHandler handler30; FebruaryHandler handlerFeb; MistakeHandler handlerMistake; handler31.add(&handler30); handler31.add(&handlerFeb); handler31.add(&handlerMistake); handlerMistake.setSuccessor(&handler31); cout << "How many days in any given month?" << endl; cout << endl << "Let's find out... " << endl << endl; cout << "Enter a year (use 0 to quit): "; cin >> year; while (year != 0) { cout << "Enter the name of a month: "; cin >> monthName; nbrOfDays = handler31.numberOfDays(monthName, year); if (nbrOfDays != 0) cout << monthName << ' ' << year << " has " << nbrOfDays << " days!" << endl << endl; else cout << "Sorry, but there IS no month called " << monthName << "!!!" << endl << endl; cout << "Want another? Enter a year (use 0 to quit): "; cin >> year; }

C h a p t e r 5 – P a g e 137

Chain of Responsibility Pattern Advantages C h a p t e r 5 – P a g e 138 Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain. The Chain of Responsibility pattern can be used to deal with long if-then-else statements. A much more descriptive path can be made by coding it into a framework of successive objects that keep a reference to one another in the path, so that a logical path is built by the objects themselves. The Chain of Responsibility pattern is often used in conjunction with the Composite structural pattern, with a component’s parent acting as its successor.