(十一)状态模式

在业务中,常常有那种状态巨多的东西,比如一个立项,可能有新建、已提交、已审批等多种状态
一个活动可能有新建、已提交、审核通过、执行中、结束等多种状态
因此,这个设计模式,指的是面向一个立项、一个订单这个"对象"的逻辑

public interface State {
	void execute();
}

public class NewState implements State {
	public void execute() {
		System.out.println("执行新建的逻辑")
	}
}
public class ApprovingState implements State {
	public void execute() {
		System.out.println("执行待审批的逻辑")
	}
}
public class ApprovedState implements State {
	public void execute() {
		System.out.println("执行已审批的逻辑")
	}
}
public class FinishState implements State {
	public void execute() {
		System.out.println("执行已完成的逻辑")
	}
}

然后将状态的流转和下一步操作都抽出来到上下文类中统一管理,流转

public class Context {
	private State state;
	public Context(State state) {
		this.state = state;
	}
	public void execute(int stateType) {
		if (stateType == 1) {
			this.state = new ApprovingState();
			this.state.execute();
		} else if (stateType == 2) {
			this.state = new ApprovedState();
			this.state.execute();
		} else if (stateType == 3) {
			this.state = new FinishState();
			this.state.extcute();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值