总纲
- 设计原则:遵循迪米特
- 常用场景:一个请求的处理需要多个对象当中的一个或几个协作处理
- 使用概率:15%
- 复杂度:中
- 变化点:处理链的长度与次序
- 选择关键点:对于每一次请求是否每个处理的对象都需要一次处理机会
- 逆鳞:无
- 定义:为了避免请求的发送者和接收者之间的耦合关系,使多个接受对象都有机会处理请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止
责任链模式
//订单类(相当于request,其实就是封装一个请求)
public class Order {
private int x;
private int y;
private Map<String, Integer> order;
public Order(int x, int y, Map<String, Integer> order) {
super();
this.x = x;
this.y = y;
this.order = order;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Map<String, Integer> getOrder() {
return order;
}
public void setOrder(Map<String, Integer> order) {
this.order = order;
}
}
//分店接口(相当于Hanlder)
public interface Subbranch {
void setSuccessor(Subbranch subbranch);
boolean handleOrder(Order order);
}
//麦当劳分店
public class McSubbranch implements Subbranch{
private final static int MIN_DISTANCE = 500;//假设是500米以内送餐
private static int count;//类计数
private final int number;//分店号
private int x;//分店的横坐标,用于判断距离
private int y;//分店的纵坐标,用于判断距离
private Map<String, Integer> menu;//菜单
private Subbranch nextSubbranch;//下一家分店
public McSubbranch(int x, int y, Map<String, Integer> menu) {
super();
this.x = x;
this.y = y;
this.menu = menu;
number = ++count;
}
//设置下一家分店
public void setSuccessor(Subbranch subbranch) {
this.nextSubbranch = subbranch;
}
//按照职责链处理订单
public boolean handleOrder(Order order){
//如果距离小于500米并且订单中的食物不缺货,则订单成功,否则失败
if (CommonUtils.getDistance(order.getX(), order.getY(), this.x, this.y) < MIN_DISTANCE && !CommonUtils.outOfStock(menu, order.getOrder())) {
for (String name : order.getOrder().keySet()) {
menu.put(name, menu.get(name) - order.getOrder().get(name));
}
System.out.println("订餐成功,接受订单的分店是:" + this);
return true;
}
if (nextSubbranch == null) {
return false;
}
return nextSubbranch.handleOrder(order);
}
public Map<String, Integer> getMenu() {
return Collections.unmodifiableMap(menu);
}
public Subbranch getNextSubbranch() {
return nextSubbranch;
}
public String toString() {
return "麦当劳分店第" + number + "个";
}
}
public class Client {
public static void main(String[] args) {
//假设初始菜单都是以下这些东西
Map<String, Integer> menu = new HashMap<String, Integer>();
menu.put("汉堡", 5);
menu.put("薯条", 5);
menu.put("可乐", 5);
menu.put("雪碧", 5);
//假设有5个分店
Subbranch mcSubbranch1 = new McSubbranch(0, 0, new HashMap<String, Integer>(menu));
Subbranch mcSubbranch2 = new McSubbranch(100, 120, new HashMap<String, Integer>(menu));
Subbranch mcSubbranch3 = new McSubbranch(-100, -120, new HashMap<String, Integer>(menu));
Subbranch mcSubbranch4 = new McSubbranch(1000, 20, new HashMap<String, Integer>(menu));
Subbranch mcSubbranch5 = new McSubbranch(-500, 0, new HashMap<String, Integer>(menu));
//以下设置职责链
mcSubbranch4.setSuccessor(mcSubbranch5);
mcSubbranch3.setSuccessor(mcSubbranch4);
mcSubbranch2.setSuccessor(mcSubbranch3);
mcSubbranch1.setSuccessor(mcSubbranch2);
//小左开始订餐,假设小左的坐标是900,20
Map<String, Integer> order = new HashMap<String, Integer>();
order.put("汉堡", 2);
order.put("可乐", 1);
order.put("薯条", 1);
print(mcSubbranch1);
System.out.println("------------------------------------------");
//小左开始订餐,直接找mcSubbranch1的这一家分店订餐即可
mcSubbranch1.handleOrder(new Order(900, 20, order));
System.out.println("------------------------------------------");
print(mcSubbranch1);
}
public static void print(Subbranch subbranch){
if (subbranch == null ) {
return;
}
do {
if (subbranch instanceof McSubbranch) {
System.out.println("[" + subbranch + "]的菜单:" + ((McSubbranch) subbranch).getMenu());
}
} while ((subbranch = ((McSubbranch) subbranch).getNextSubbranch()) != null);
}
}
网友评论