行为模式涉及到算法和对象间职责的分配。
chain:使多个对象有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。
package behaviour.chain;
/**
* The interface of the chain
* You can use AddChain function to modify the chain dynamically
*/
public interface Chain {
public abstract void addChain(Chain c);
public abstract void sendToChain(String mesg);
public abstract Chain getChain();
}
package behaviour.chain;
/**
* A beginner of the chain
* The resposibility of manager is to get a project
*/
import java.io.*;
public class Manager implements Chain {
private Chain nextChain = null;
private String responsibility = "Get Project";;
public Manager() {
}
public void addChain(Chain c) {
nextChain = c;
}
public Chain getChain() {
return nextChain;
}
public void sendToChain(String mesg) {
if(mesg.equals(responsibility)) {
System.out.println("A manager --> Get a Project");
} else {
if(nextChain != null) {
nextChain.sendToChain(mesg);
}
}
}
}
package behaviour.chain;
/**
* The end of the chain
* The resposibility of Others is handle exeception
*/
import java.io.*;
public class Others implements Chain {
private Chain nextChain = null;
private String responsibility = "";
public Others() {
}
public void addChain(Chain c) {
nextChain = c;
}
public Chain getChain() {
return nextChain;
}
public void sendToChain(String mesg) {
System.out.println("No one can handle --> " + mesg);
}
}
package behaviour.chain;
/**
* A member of the chain
* The resposibility of Programmer is coding
*/
import java.io.*;
public class Programmer implements Chain {
private Chain nextChain = null;
private String responsibility = "Coding";
public Programmer() {
}
public void addChain(Chain c) {
nextChain = c;
}
public Chain getChain() {
return nextChain;
}
public void sendToChain(String mesg) {
if(mesg.equals(responsibility)) {
System.out.println("A Programmer --> Coding");
} else {
if(nextChain != null) {
nextChain.sendToChain(mesg);
}
}
}
}
package behaviour.chain;
/**
* A member of the chain
* The resposibility of PM is to design the project
*/
import java.io.*;
public class ProjectManager implements Chain {
private Chain nextChain = null;
private String responsibility = "Design";
public ProjectManager() {
}
public void addChain(Chain c) {
nextChain = c;
}
public Chain getChain() {
return nextChain;
}
public void sendToChain(String mesg) {
if(mesg.equals(responsibility)) {
System.out.println("A PM --> Design");
} else {
if(nextChain != null) {
nextChain.sendToChain(mesg);
}
}
}
}
package behaviour.chain;
/**
* A member of the chain
* The resposibility of QA is test
*/
import java.io.*;
public class QA implements Chain {
private Chain nextChain = null;
private String responsibility = "Test";
public QA() {
}
public void addChain(Chain c) {
nextChain = c;
}
public Chain getChain() {
return nextChain;
}
public void sendToChain(String mesg) {
if(mesg.equals(responsibility)) {
System.out.println("A QA --> Test");
} else {
if(nextChain != null) {
nextChain.sendToChain(mesg);
}
}
}
}
package behaviour.chain;
/**
* A client to test
*/
import java.io.*;
public class Test {
public static void main(String[] args) {
Manager aManager = new Manager();
ProjectManager aPM = new ProjectManager();
Programmer aProgrammer = new Programmer();
QA aQA = new QA();
Others others = new Others();
aManager.addChain(aPM);
aPM.addChain(aProgrammer);
aProgrammer.addChain(aQA);
aQA.addChain(others);
aManager.sendToChain("Get Project");
aManager.sendToChain("Design");
aManager.sendToChain("Coding");
aManager.sendToChain("Test");
aManager.sendToChain("Kill La Deng !");
}
}