坏了,我写不出来面向过程的代码了 -- 面向对象之订单状态改变的设计

class Order {

	@Autowire
	OrderStatusActionRegister orderStatusActionRegister;
		
	void changeStatus(toStatus) {
		
		foreach(Action action : orderStatusActionRegister.getActions(toStatus)) {
			action.execute();
		}
	}
}

interface Action {
	
	void execute();
}

class OrderStatusActionRegister {

	Map<String, List> statusActionsMap = new HashMap<String, ArrayList>();	
	
	public void regitstr(status, action) {
		
		if(statusActionsMap.get(status) == null) {
			statusActionsMap.put(status, new ArrayList());
		}
		statusActionsMap.get(status).add(action);
	}	

	public void deregitstr() {
	
		...
	}

	public List getActions(status) {
	
		return statusActionsMap.get(status);
	}
}

// 新增发货动作
class SendAction implement Action {
	
	void execute() {
		
		syso('业务逻辑-皓哥,我发货了,请嫂子查收');
	}
}

class SendActionRegister {

	@Autowire
	OrderStatusActionRegister register;

	register.regitstr("发货", new SendAction());
}

虽然我不是高手,但我愿和你方便的交流。 QQ:370298543

  

原文地址:https://www.cnblogs.com/MorZe/p/5993077.html