JAVA Transformation (xii) design pattern Chain of Responsibility design pattern

Foreword

     This chapter describes the design pattern Chain of Responsibility design pattern knowledge

method

1. Concept

Imagine the following scenario: A company approving financial funds to purchase equipment, goods of less than 10 million instructions from the director, greater than 100,000 and less than 100 million worth of goods instructed by the manager, cargo greater than one million instructions by the general manager. So how does this logic?

Then we first thought is to use traditional if ... else to deal with, but are quite complex and difficult to maintain for the business.

Financial aspects of the approval process in order to solve the problems mentioned above, we use the Chain of Responsibility pattern better.

Realization of ideas 2. Chain of Responsibility design pattern

1) creates a corresponding project

2) the preparation of the application form class

package chain;

/**
 * 申请表
 * @author jwang
 *
 */
public class Apply {
	private String name;
	private int money;//以万为单位
	public Apply(String name, int money) {
		super();
		this.name = name;
		this.money = money;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	
}

3) Create a chain of responsibility abstract class

package chain;

/**
 * 责任链抽象类
 * @author jwang
 *
 */
public abstract class Leader {
	protected String name;
	protected Leader nextLeader;//下一个领导
	
	public Leader(String name) {
		super();
		this.name = name;
	}

	public void setNextLeader(Leader nextLeader) {
		this.nextLeader = nextLeader;
	}
	//处理申请
	public abstract void handleRequest(Apply apply);
}

4) Create a chain of responsibility specific class leader (director, manager, general manager)

package chain;

/**
 * 主任
 * @author jwang
 *
 */
public class Director extends Leader {

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

	@Override
	public void handleRequest(Apply apply) {
		if(apply.getMoney()<10){
			System.out.println("审批金额小于10万,"+this.name+"审批通过!");
		}else {
			//交给下一个领导审批
			this.nextLeader.handleRequest(apply);
		}
	}

}
package chain;
/**
 * 经理
 * @author jwang
 *
 */
public class Manager extends Leader {

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

	@Override
	public void handleRequest(Apply apply) {
		if(apply.getMoney()<100){
			System.out.println("审批金额小于100万,"+this.name+"审批通过!");
		}else {
			//交给下一个领导审批
			this.nextLeader.handleRequest(apply);
		}
	}

}
package chain;
/**
 * 总经理
 * @author jwang
 *
 */
public class GeneralManager extends Leader {

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

	@Override
	public void handleRequest(Apply apply) {
		if(apply.getMoney()>100){
			System.out.println("审批金额大于100万,"+this.name+"审批通过!");
		}
	}

}

5) writing test code

package chain;

public class Test {

	public static void main(String[] args) {
		Apply apply1 = new Apply("材料费申请", 90);
        Apply apply2 = new Apply("材料费申请", 120);
		
		Leader a = new Director("张主任");
		Leader b = new Manager("李经理");
		Leader c = new GeneralManager("王总经理");
		
        //设置领导顺序
		a.setNextLeader(b);
		b.setNextLeader(c);
		
		a.handleRequest(apply1);
        a.handleRequest(apply2);
	}

}

As a result of the program:

Guess you like

Origin blog.csdn.net/qq_21046965/article/details/92401100