java 状态模式_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 状态模式

java 状态模式

 2019/4/27 3:45:34  xiaoshiyier  程序员俱乐部  我要评论(0)
  • 摘要:publicinterfaceTestState{voidhandle();}publicclassTestState1implementsTestState{privateContextcontext;publicTestState1(Contextcontext){this.context=context;}@Overridepublicvoidhandle(){System.out.println("赤");context.setState(newTestState2(context))
  • 标签:Java 模式

public interface TestState {
?? void handle();
}

public class TestState1 implements TestState {
??? private Context context;

??? public TestState1(Context context) {
??????? this.context = context;
??? }

??? @Override
??? public void handle() {
??????? System.out.println("赤");
??????? context.setState(new TestState2(context));
??? }
}

public class TestState2 implements TestState {
??? private Context context;

??? public TestState2(Context context) {
??????? this.context = context;
??? }

??? @Override
??? public void handle() {
??????? System.out.println("橙");
??????? context.setState(new TestState3(context));
??? }
}

?

public class TestState3 implements TestState {
??? private Context context;

??? public TestState3(Context context) {
??????? this.context = context;
??? }
??? @Override
??? public void handle() {
??????? System.out.println("黄");
??????? context.setState(new TestState1(context));
??? }
}

?

public class Client {
??? public static void main(String[] args) {
??????? Context context = new Context();
??????? context.setState(new TestState1(context));
??????? for (int i = 0; i < 100; i++) {
??????????? context.changeColor();
??????? }
??? }
}

?

神奇的输出:

?































上一篇: Learun FrameWork,基于.NET的智能化开发工具 下一篇: 没有下一篇了!
发表评论
用户名: 匿名