Definition -
This is a pattern where in there are a series of handlers which cater to a specific part or order of the request. In this pattern the client will not know who is addressing the request. The handlers decide among themselves who will server a particular request.
This can also result in none of the handlers serving the request.
Scenario -
Lets take an example of a warehouse machine whose job is sort the incoming materials based on the type. For simplicity lets take there are two types of containers with tags of solid and liquid.
In this we will have a
1. The object here is a package so we will declare a class called pckg.
This is a pattern where in there are a series of handlers which cater to a specific part or order of the request. In this pattern the client will not know who is addressing the request. The handlers decide among themselves who will server a particular request.
This can also result in none of the handlers serving the request.
Scenario -
Lets take an example of a warehouse machine whose job is sort the incoming materials based on the type. For simplicity lets take there are two types of containers with tags of solid and liquid.
In this we will have a
1. The object here is a package so we will declare a class called pckg.
package ChainofCommandPattern;
public class pckg {
String type;
public pckg(String objtype) {
this.type=objtype;
}
public String gettype() {
return this.type;
}
}
2. A packagehandler which will have the functions that needs to be implemented.
package ChainofCommandPattern;
public interface packagehandler {
void setnext(packagehandler ph);
void handlerequest(pckg pkg);
}
3. A solidpackagehandler and liquidpackagehandler which will have respective functions to handle the package.
solidpackagehandler.java
package ChainofCommandPattern;
public class solidpackagehandler implements packagehandler {
packagehandler next;
@Override
public void setnext(packagehandler ph) {
next=ph;
}
@Override
public void handlerequest(pckg pkg) {
if(pkg.type.equalsIgnoreCase("Solid")) {
System.out.println("Handled the Solid object");
} else if(pkg.type.equalsIgnoreCase("Liquid")) {
next.handlerequest(pkg);
}
}
}
liquidpackagehandler.java
package ChainofCommandPattern;
public class liquidpackagehandler implements packagehandler{
packagehandler next;
@Override
public void setnext(packagehandler ph) {
next=ph;
}
@Override
public void handlerequest(pckg pkg) {
if(pkg.type.equalsIgnoreCase("Liquid")) {
System.out.println("Handled the Liquid object");
} else if(pkg.type.equalsIgnoreCase("Solid")) {
next.handlerequest(pkg);
}
}
}
4. Finally a client which will use the above classes.
package ChainofCommandPattern;
public class chainofcommandclient {
public static void main(String args[]) {
packagehandler pckg1=new solidpackagehandler();
packagehandler pckg2=new liquidpackagehandler();
pckg1.setnext(pckg2);
pckg1.handlerequest(new pckg("Solid"));
pckg2.handlerequest(new pckg("Liquid"));
}
}
Place to use -
1. When you have multiple handlers which can process the same request.
2. A request not being handled by any handler is an accepted scenario.
Place not to use -
1. Can be slower since it has to follow a specified path.
2. A request might not be addressed by the system implementing this pattern.
3. It can be difficult to maintain if the system has too many handlers and complex routes.
No comments:
Post a Comment