设计模式之职责链(ChainOfResponsible)

public class ChainOfResponsible {
	//请求
	class Request{
		private String content;

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}
	}
	//职责链抽象类
	abstract class Persion{
		protected Persion persion;
		//设置上层链接
		public void setSuperior(Persion persion){
			this.persion = persion;
		}
		public abstract void check(Request request);
	}
	//主管
	class Director extends Persion{
		@Override
		public void check(Request request) {
			if("1".equals(request.getContent())){
				System.out.println("director check...");
			}else{
				persion.check(request);
			}
		}
	}
	//经理
	class Manager extends Persion{
		@Override
		public void check(Request request) {
			if("2".equals(request.getContent())){
				System.out.println("manager check...");
			}else{
				persion.check(request);
			}
		}
	}
	//总监 
	class Majordomo extends Persion{
		@Override
		public void check(Request request) {
			System.out.println("majordomo check...");
		}
	}
	public static void main(String[] args){
		ChainOfResponsible cor = new ChainOfResponsible();
		Majordomo majordomo = cor.new Majordomo();
		Manager manager = cor.new Manager();
		Director director = cor.new Director();
		director.setSuperior(manager);
		manager.setSuperior(majordomo);
		Request request = cor.new Request();
		request.setContent("2");
		director.check(request);
	}
}
 

你可能感兴趣的:(职责链模式)