| package com.atguigu.state.money; |
| |
| |
| |
| public interface State { |
| |
| |
| |
| |
| void checkEvent(Context context); |
| |
| |
| |
| |
| void checkFailEvent(Context context); |
| |
| |
| |
| |
| void makePriceEvent(Context context); |
| |
| |
| |
| |
| void acceptOrderEvent(Context context); |
| |
| |
| |
| |
| void notPeopleAcceptEvent(Context context); |
| |
| |
| |
| |
| void payOrderEvent(Context context); |
| |
| |
| |
| |
| void orderFailureEvent(Context context); |
| |
| |
| |
| |
| void feedBackEvent(Context context); |
| |
| String getCurrentState(); |
| } |
| |
| package com.atguigu.state.money; |
| public abstract class AbstractState implements State { |
| |
| protected static final RuntimeException EXCEPTION = new RuntimeException("操作流程不允许"); |
| |
| |
| |
| |
| @Override |
| public void checkEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void checkFailEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void makePriceEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void acceptOrderEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void notPeopleAcceptEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void payOrderEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void orderFailureEvent(Context context) { |
| throw EXCEPTION; |
| } |
| |
| @Override |
| public void feedBackEvent(Context context) { |
| throw EXCEPTION; |
| } |
| } |
| |
| package com.atguigu.state.money; |
| |
| class FeedBackState extends AbstractState { |
| @Override |
| public String getCurrentState() { |
| return StateEnum.FEED_BACKED.getValue(); |
| } |
| } |
| |
| class GenerateState extends AbstractState { |
| |
| @Override |
| public void checkEvent(Context context) { |
| context.setState(new ReviewState()); |
| } |
| |
| @Override |
| public void checkFailEvent(Context context) { |
| context.setState(new FeedBackState()); |
| } |
| |
| @Override |
| public String getCurrentState() { |
| return StateEnum.GENERATE.getValue(); |
| } |
| } |
| |
| class NotPayState extends AbstractState { |
| |
| @Override |
| public void payOrderEvent(Context context) { |
| context.setState(new PaidState()); |
| } |
| |
| @Override |
| public void feedBackEvent(Context context) { |
| context.setState(new FeedBackState()); |
| } |
| |
| @Override |
| public String getCurrentState() { |
| return StateEnum.NOT_PAY.getValue(); |
| } |
| } |
| |
| class PaidState extends AbstractState { |
| |
| @Override |
| public void feedBackEvent(Context context) { |
| context.setState(new FeedBackState()); |
| } |
| |
| @Override |
| public String getCurrentState() { |
| return StateEnum.PAID.getValue(); |
| } |
| } |
| |
| class PublishState extends AbstractState { |
| |
| @Override |
| public void acceptOrderEvent(Context context) { |
| |
| |
| context.setState(new NotPayState()); |
| } |
| |
| @Override |
| public void notPeopleAcceptEvent(Context context) { |
| context.setState(new FeedBackState()); |
| } |
| |
| @Override |
| public String getCurrentState() { |
| return StateEnum.PUBLISHED.getValue(); |
| } |
| } |
| |
| class ReviewState extends AbstractState { |
| |
| @Override |
| public void makePriceEvent(Context context) { |
| context.setState(new PublishState()); |
| } |
| |
| @Override |
| public String getCurrentState() { |
| return StateEnum.REVIEWED.getValue(); |
| } |
| |
| } |
| |
| package com.atguigu.state.money; |
| |
| public class Context extends AbstractState{ |
| |
| private State state; |
| |
| @Override |
| public void checkEvent(Context context) { |
| state.checkEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void checkFailEvent(Context context) { |
| state.checkFailEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void makePriceEvent(Context context) { |
| state.makePriceEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void acceptOrderEvent(Context context) { |
| state.acceptOrderEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void notPeopleAcceptEvent(Context context) { |
| state.notPeopleAcceptEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void payOrderEvent(Context context) { |
| state.payOrderEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void orderFailureEvent(Context context) { |
| state.orderFailureEvent(this); |
| getCurrentState(); |
| } |
| |
| @Override |
| public void feedBackEvent(Context context) { |
| state.feedBackEvent(this); |
| getCurrentState(); |
| } |
| |
| public State getState() { |
| return state; |
| } |
| |
| public void setState(State state) { |
| this.state = state; |
| } |
| |
| @Override |
| public String getCurrentState() { |
| System.out.println("当前状态 : " + state.getCurrentState()); |
| return state.getCurrentState(); |
| } |
| } |
| |
| package com.atguigu.state.money; |
| |
| public class ClientTest { |
| |
| public static void main(String[] args) { |
| |
| Context context = new Context(); |
| |
| context.setState(new PublishState()); |
| System.out.println(context.getCurrentState()); |
| |
| |
| context.acceptOrderEvent(context); |
| |
| context.payOrderEvent(context); |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?