文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页资讯编程开发 → java state 状态设计模式

java state 状态设计模式

时间:2011/9/1 8:54:42来源:本站原创作者:lff我要评论(1)

  网上有很多java设计模式之state状态模式的教程,今天小编为大家提供的是一位java从业者的一些经验。state借口有四个状态类,分别是create、start、end和destroy。
  State接口代码
  package org.javaer.code.pattern.state;

  import java.util.ArrayList;
  import java.util.List;

  public interface State {
  List commands = new ArrayList();
  public void handle();
  }

  Create状态类代码
  package org.javaer.code.pattern.state;

  public class Create implements State {

  public Create() {
  commands.add(this);
  }
  @Override
  public void handle() {
  execute(this);
  }

  public void execute(State command){
  System.out.println(“create”);
  commands.get(commands.indexOf(this)+1).handle();
  }
  }

  Start状态类代码
  package org.javaer.code.pattern.state;

  public class Start implements State{

  public Start() {
  commands.add(this);
  }
  @Override
  public void handle() {
  execute(this);
  }

  public void execute(State command){
  System.out.println(“start”);
  commands.get(commands.indexOf(this)+1).handle();
  }
  }

  End状态类代码
  package org.javaer.code.pattern.state;

  public class End implements State {

  public End() {
  commands.add(this);
  }
  @Override
  public void handle() {
  execute(this);
  }

  public void execute(State command){
  System.out.println(“end”);
  commands.get(commands.indexOf(this)+1).handle();
  }
  }

  Destroy状态类代码
  package org.javaer.code.pattern.state;

  public class Destroy implements State {

  public Destroy() {
  commands.add(this);
  }
  @Override
  public void handle() {
  execute(this);
  }

  public void execute(State command){
  System.out.println(“destory”);
  //我这里加了这一句,就是想让它循环的转换状态,就会导致内存溢出
  commands.get(commands.indexOf(this)>=commands.size()-1?0:commands.indexOf(this)+1).handle();
  }
  }

  测试类Main代码
  package org.javaer.code.pattern.state;

  public class Main {
  @SuppressWarnings(“unused”)
  public static void main(String[] args) {
  State state1 = new Create();
  State state2 = new Start();
  State state3 = new End();
  State state4 = new Destroy();
  state1.handle();
  }
  }
 
  输出:
  create
  start
  end
  destory
  create
  start
  end
  destory
  create
  start
  end
  destory

  Exception in thread “main” java.lang.StackOverflowError
  at sun.nio.cs.UTF_8.updatePositions(Unknown Source)
  at sun.nio.cs.UTF_8$Encoder.encodeArrayLoop(Unknown Source)

相关视频

    没有数据

相关阅读 jdk不是有效的win32程序怎么办 jdk不是有效的win32程序解决方法java设置cookie教程 java怎么设置cookiejava怎么设置随机数 java设置随机数详细教程java怎么设置光标位置 java设置光标位置教程如何在Mac上清除Java高速缓存?如何在Mac上卸载Java?Mac上怎么卸载Java?OSX 10.11 java 6不兼容问题解决办法如何为Mac更新Java?java mac版更新教程

文章评论
第 1 楼 湖南长沙联通 客人 发表于: 2011/9/1 12:16:27
看看还不错哦。。

支持( 0 ) 盖楼(回复)

查看所有1条评论>>

发表评论

热门文章 没有查询到任何记录。

最新文章 编程语言排行榜2020年android studio怎么删 eclipse怎么设置编码格式 eclipse设置编码格andriod studio如何使用真机测试 andriod sandroid studio怎么生成apk android studioandroid studio如何导入jar包 android stud

人气排行 安卓模拟器BlueStacks安装使用教程编程语言排行榜2020年9月 TIOBE编程语言排行eclipse字体大小怎么设置 eclipse字体大小plsql developer怎么连接数据库 plsql deveTomcat9.0安装教程 Tomcat9.0环境变量配置方plsql developer怎么使用 plsql developerVisual Studio 2015环境搭建教程Eclipse优化设置教程 Eclipse优化设置技巧