Presentation is loading. Please wait.

Presentation is loading. Please wait.

Zaki Alasadi 87231101 Supervisor:Dr noorhosseini.

Similar presentations


Presentation on theme: "Zaki Alasadi 87231101 Supervisor:Dr noorhosseini."— Presentation transcript:

1 Zaki Alasadi 87231101 Supervisor:Dr noorhosseini

2 Intent Motivation Applicability Structure Participants Implementation Consequences Example Related patterns Attention 2

3 Intent Avoid coupling sender of request to its receiver by giving more than one object chance to handle request. Chain receiving objects and pass request along until an object handles it. 3

4 Consider a context sensitive help facility for a GUI The user can obtain help information on any part of the interface just by clicking on it. The help that is provided depend on the parts of the interface that’s selected and its context. Sometimes we will encounter with help about a particular button, a particular dialog, or the application in general based on whether help is available on that subject 4

5 the help information is organized according to its generality - from the most specific to the most general The problem is that the object that ultimately provides the help is not known explicitly to the object that initiates the help request We need a way to decouple the button that initiates the help request from the objects that provide help information 5

6 The first object in the chain receives the request and will either handle it or forward it to the next object on the chain and so on. The important thing to remember, is that the object that made the request, has no knowledge of which object is handling the request. The request has an implicit receiver. 6

7 To forward request along the chain, and to ensure receivers remain implicit, Each object in the chain can share a common interface for handling requests and accessing its successor on the chain. 7

8 When more than one object may handle a particular request and the handler isn’t known ahead of time When you want to issue a request to one of several objects without specifying the receiver explicitly When the set of objects to handle a request should be specified dynamically 8

9 Structure 9

10 Handler defines an interface for handling the requests (optional) implements the successor link ConcreteHandler handles requests it is responsible for can access its successor if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor Client initiates the request to a ConcreteHandler object on the chain 10

11 Implementing the successor chain Define new links (in Handler or ConcreteHandler) Use existing links (such as the parent references from the Composite pattern) Connecting successors Representing requests 11

12 12

13 13

14 14

15 15

16 +Reduced Coupling The sender doesn’t need to know the specific receiver and the receiver doesn’t need to know the sender +Added flexibility in assigning responsibilities to objects By changing the chain you can change responsibilities for handling a request at run time –Receipt Isn’t Guaranteed A request may fall off the end of the chain 16

17 Whenever you spend company's money, you need get approval from your boss, or your boss's boss. Let's say, the leadership chain is: Manager-->Director-->Vice President-->President abstract class Approver { protected final double base = 500; protected Approver successor; public void setSuccessor(Approver successor){ this.successor = successor; } abstract public void processRequest(PurchaseRequest request); } class Manager extends Approver { private final double ALLOWABLE = 10 * base; public void processRequest(PurchaseRequest request ) { if( request.getAmount() < ALLOWABLE ) System.out.println("Manager will approve $"+ request.getAmount()); else if( successor != null) successor.processRequest(request); }

18 Manager manager = new Manager(); Director director = new Director(); VicePresident vp = new VicePresident(); President president = new President() manager.setSuccessor(director); director.setSuccessor(vp); vp.setSuccessor(president); try{ while (true) { System.out.println("Enter the amount to check who should approve yourexpenditure."); System.out.print(">"); double d = Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine()); manager.processRequest(new PurchaseRequest(0, d, "General")); } }catch(Exception e){ System.exit(1); }

19 Chain of responsibility is often applied in conjunction with Composite.

20 Chain of responsibility pattern vs Decorator Pattern 20

21 A Decorator usually wraps the decorated object: clients point to the decorator and not the object A Decorator does not have to forward the same message A decorated object does not have to know that it is wrapped With a chain of responsibility, the client asks the first chain objects explicitly. 21

22 In chain of responsibility: you can break the chain at any point Decorators can be thought of as executing all at once without any interaction with the other decorators Links in a chain can be thought of as executing one at a time, because they each depend on the previous link 22

23 Thank you 23

24 ? 24


Download ppt "Zaki Alasadi 87231101 Supervisor:Dr noorhosseini."

Similar presentations


Ads by Google