Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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.

3 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.

4 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.

5 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.

6 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.

7 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; };

8 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); } };

9 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; } };

10 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; }

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

12 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.


Download ppt "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."

Similar presentations


Ads by Google