大话设计模式_责任链模式

以小菜向公司请假,申请加薪为例子;

需要过的审批:经理,总监,总经理;

package com.wzs.design;

/**
 * 大话设计模式--page252 责任链模式
 * 
 * @author Administrator
 * 
 */
public class DutyChainMode {
	public static void main(String[] args) {
		CommonManager jinli = new CommonManager("金利");
		Majordomo zongjian = new Majordomo("宗剑");
		GeneralManager zhongjingli = new GeneralManager("钟精励");
		jinli.setSuperior(zongjian);
		zongjian.setSuperior(zhongjingli);

		Request request = new Request();
		request.setType("请假");
		request.setContent("小菜请假");
		request.setNumber(1);
		jinli.requestApplications(request);

		request = new Request();
		request.setType("请假");
		request.setContent("小菜请假");
		request.setNumber(4);
		jinli.requestApplications(request);

		request = new Request();
		request.setType("加薪");
		request.setContent("小菜请求加薪");
		request.setNumber(500);
		jinli.requestApplications(request);

		request = new Request();
		request.setType("加薪");
		request.setContent("小菜请求加薪");
		request.setNumber(1000);
		jinli.requestApplications(request);
	}
}

// 管理者抽象类
abstract class Manager {
	public String name;
	public Manager superior;

	public Manager(String name) {
		this.name = name;
	}

	// 设置管理者的上级
	public void setSuperior(Manager superior) {
		this.superior = superior;
	}

	// 申请请求
	public abstract void requestApplications(Request request);
}

// 经理
class CommonManager extends Manager {

	public CommonManager(String name) {
		super(name);
	}

	@Override
	public void requestApplications(Request request) {
		if ("请假" == request.getType() && request.getNumber() <= 2) {
			System.out.println(name + " : " + request.getContent() + " 数量 " + request.getNumber() + " 被批准.");
		} else {
			if (null != superior) {
				superior.requestApplications(request);
			}
		}
	}
}

// 总监
class Majordomo extends Manager {

	public Majordomo(String name) {
		super(name);
	}

	@Override
	public void requestApplications(Request request) {
		if ("请假" == request.getType() && request.getNumber() <= 5) {
			System.out.println(name + " : " + request.getContent() + " 数量 " + request.getNumber() + " 被批准.");
		} else {
			if (null != superior) {
				superior.requestApplications(request);
			}
		}
	}
}

// 总经理
class GeneralManager extends Manager {

	public GeneralManager(String name) {
		super(name);
	}

	@Override
	public void requestApplications(Request request) {
		if ("请假" == request.getType()) {
			System.out.println(name + " : " + request.getContent() + " 数量 " + request.getNumber() + " 被批准.");
		} else if ("加薪" == request.getType() && request.getNumber() <= 500) {
			System.out.println(name + " : " + request.getContent() + " 数量 " + request.getNumber() + " 被批准.");
		} else if ("加薪" == request.getType() && request.getNumber() > 500) {
			System.out.println(name + " : " + request.getContent() + " 数量 " + request.getNumber() + " 再说吧.");
		}
	}
}

// 请求:请假,加薪
class Request {
	private String type;
	private String content;
	private int number;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}
}


你可能感兴趣的:(java基础)