Sie sind auf Seite 1von 9

Design Pattern -7

Intent:

Chain of responsibility

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

Data-driven concepts are used to have a loose coupling.

Motivation: Decouples the sender of the request to the receiver. The responsibility of handling the request data is given to any of the members of the chain. Data is send without knowing who will handle. Each object on the chain shares a common interface for handling requests and for accessing its successor on the chain Only common link is the request that is passed between handlers. Applicability: You have more than one handler that can handle a request and there is no way to know which handler to use. The handler must be determined automatically by the chain.

You want to issue a request to one of several objects without specifying which one explicitly. You want to be able to modify the set of objects dynamically that can handle requests.

Examples:

Sample Code:
public class CORDemo { public static void main(String[] args) { Purchase p=new Purchase(); p.setPrice(3000); Employee e=new Manager(); e.handleRequest(p); } } class Purchase{ float price; void setPrice(float price){ this.price=price; } float getPrice(){ return price; } } interface Employee{ public void handleRequest(Purchase p); public void setApprover(Purchase p); }

class Manager implements Employee{

public void handleRequest(Purchase p) { if(p.price<1000) System.out.print("Manager handles request"); else{ setApprover(p); } }

@Override public void setApprover(Purchase p) { Employee e=new Director(); e.handleRequest(p); } }

class Director implements Employee{

@Override public void handleRequest(Purchase p) { if(p.price>=1000 && p.price<2000) System.out.print("Director handles request"); else{ setApprover(p); } }

@Override public void setApprover(Purchase p) { Employee e=new President(); e.handleRequest(p); } }

class President implements Employee{

@Override public void handleRequest(Purchase p) { if(p.price>=2000 && p.price<5000) System.out.print("President handles request"); else{ setApprover(p); } } @Override public void setApprover(Purchase p) { Employee e=new Director(); e.handleRequest(p); System.out.print("Your request needs board meeting discussion"); } }

Question & Answers: 1. Ques: The most obvious example of the Chain of Responsibility is the class inheritance structure itself. Explain with example? Ans: The most obvious example of the Chain of Responsibility is the class
inheritance structure itself. If you call for a method to be executed in a deeply derived class that method is passed up the inheritance chain until the first parent class containing that method is found. The fact that further parents contain other implementations of that method does not come into play. super is used to call a parent class method.

Document styles such as Content Style Sheets (CSS) often support style inheritance. In the element hierarchy (a Composite pattern), style attributes defined in one element may be inherited by its children. The method for locating an inherited style attribute for an element checks an attribute table in that element. If the attribute is not found there, a message with the same method is sent to the element's parent. The method recurs until the attribute value is found.

Java's AttributeSet class is used for styling elements in a text document. It uses a similar style inheritance mechanism.

Inheritance mechanisms in classless OOLs may use a Chain of Responsibility design pattern to minimize the memory footprint of objects. Search for an object member starts in its own member table. If the member is not found there the search is forwarded to the object's prototype, which may forward to its prototype, and so on.

Q15::Exception Handling in JAVA uses Chain of Responsibility. Comment with suitable example and Class diagram? The responsibility of handling the request data is given to any of the members of the chain. If the first link of the chain cannot handle the responsibility, it passes the request data to the next level in the chain, i.e. to the next link. For readers, familiar with concepts of Java, this is similar to what happens in an Exception Hierarchy.

We may have sequence of exceptions listed in catch statements and when there is an exception thrown, the catch list is scanned one by one from top. If first exception in catch can handle it the job is done, else the responsibility is moved to next in line and so on till it reaches finally block if we have one.

Suppose the code written throws an ArrayIndexOutOfBoundsException. Now, this exception is because of some bug in coding and so, it gets caught at the correct level. Suppose, we have an application specific exception in the catch block. This will not be caught by that. It will find for an Exception class and will be caught by that as both the application specific exceptions and the

ArrayIndexOutOfBoundsException are sub-classes of the class Exception.

Once get caught by the exception, which is the base class, it will then not look for any other exception. This is precisely the reason why, we get an Exception is unreachable message when we try to add a catch block with the exception below the parent exception catch block.

So, in short, the request rises in hierarchy till some object takes responsibility to handle this request.

When an error occurs, the exception call will look for a handling class. If there is no handler, the super Exception class will be called to throw the exception. Otherwise, the handler class will handle it. Processing stops after an event is handled.

Q16:: Context sensitive help implements X pattern. Explain Context sensitive help implements COR pattern the help thats provided depends on the part of the interface thats selected and its context The help information is organized according to its generality - from the most specific to the most general.

In context sensitive help systems, a user requests help for a particular widget. If the widget has no help available, the user will be presented with help for the widget's owner, or the widget's owner's owner. If no context is found in the help system, help for the application will appear. In this example, if responsibility is not taken for context sensitive help, then general help is offered.

It is because when a context sensitive help is invoked say on print button then help system will forward its name or id, if the first module can handle the request then a help message will be displayed else it will be forwarded to next module/button in this way to all button, then All controls then general help. In this way it follows a chain from more specific to more general help Print ButtonFile ButtonAll ButtonsAll ControlsGeneral Help

Design Pattern- 8 Observer Pattern

Das könnte Ihnen auch gefallen