state?状态设计模式

发表时间:2017-02-27 22:12:02浏览:78次

在上一篇文章中,小编为您详细介绍了关于《新版2.5.7水印功能升级体验?美图秀秀》相关知识。 本篇中小编将再为您讲解标题state?状态设计模式。

  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)+①).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)+①).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)+①).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()-①?⓪:commands.indexOf(this)+①).handle();

  }

  }

  测试类Main代码

  package org.javaer.code.pattern.state;

  public class Main {

  @SuppressWarnings(“unused”)

  public static void main(String[] args) {

  State state① = new Create();

  State state② = new Start();

  State state③ = new End();

  State state④ = new Destroy();

  state①.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_⑧.updatePositions(Unknown Source)

编后语:关于《state?状态设计模式》关于知识就介绍到这里,希望本站内容能让您有所收获,如有疑问可跟帖留言,值班小编第一时间回复。 下一篇内容是有关《OS上网本或将6月上市?传谷歌Chrome》,感兴趣的同学可以点击进去看看。

相关推荐

玩家点评

条评论

热门下载

热点资讯