The Java Servlet filter framework is an example of chain of resposibility design.
Note that the chain.doFilter() is the method that should be called to make the chain roll. If the subclass missed it, the whole chain would be stopped or blocked.
Note that the chain.doFilter() is the method that should be called to make the chain roll. If the subclass missed it, the whole chain would be stopped or blocked.
Java Exception handling is another example of chain of responsibility design. 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.
Here comes a simple example, just to show how chain of responsibility works.
Whenever you spend company's money, you need get approval from your boss, or your boss's boss.
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 PurchasePower {
protected final double base = 500;
protected PurchasePower successor;
public void setSuccessor(PurchasePower successor){
this.successor = successor;
}
abstract public void processRequest(PurchaseRequest request);
}
class ManagerPPower extends PurchasePower {
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);
}
}
class DirectorPPower extends PurchasePower {
private final double ALLOWABLE = 20 * base;
public void processRequest(PurchaseRequest request ) {
if( request.getAmount() < ALLOWABLE )
System.out.println("Director will approve $"+ request.getAmount());
else
if( successor != null)
successor.processRequest(request);
}
}
class VicePresidentPPower extends PurchasePower {
private final double ALLOWABLE = 40 * base;
public void processRequest(PurchaseRequest request) {
if( request.getAmount() < ALLOWABLE )
System.out.println("Vice President will approve $" + request.getAmount());
else
if( successor != null )
successor.processRequest(request);
}
}
class PresidentPPower extends PurchasePower {
private final double ALLOWABLE = 60 * base;
public void processRequest(PurchaseRequest request){
if( request.getAmount() < ALLOWABLE )
System.out.println("President will approve $" + request.getAmount());
else
System.out.println( "Your request for $" + request.getAmount() + " needs a board meeting!");
}
}
class PurchaseRequest {
private int number;
private double amount;
private String purpose;
// constructor and getters/setters
}
____________________________________________________
ManagerPPower manager = new ManagerPPower();
DirectorPPower director = new DirectorPPower();
VicePresidentPPower vp = new VicePresidentPPower();
PresidentPPower president = new PresidentPPower();
manager.setSuccessor(director);
director.setSuccessor(vp);
vp.setSuccessor(president);
// enter ctrl+c to kill.
try {
while (true) {
System.out.println("Enter the amount to check who should
approve your expenditure.");
approve your expenditure.");
System.out.print(">");
double d = Double.parseDouble(new BufferedReader(
new InputStreamReader(System.in)).readLine());
new InputStreamReader(System.in)).readLine());
manager.processRequest(new PurchaseRequest(0, d,"General"));
}
} catch(Exception e) {
System.exit(1);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.