国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

java state 狀態(tài)設(shè)計模式

2024-07-15 02:01:44
字體:
供稿:網(wǎng)友

  網(wǎng)上有很多java設(shè)計模式之state狀態(tài)模式的教程,今天小編為大家提供的是一位java從業(yè)者的一些經(jīng)驗。state借口有四個狀態(tài)類,分別是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狀態(tài)類代碼
  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狀態(tài)類代碼
  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狀態(tài)類代碼
  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狀態(tài)類代碼
  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”);
  //我這里加了這一句,就是想讓它循環(huán)的轉(zhuǎn)換狀態(tài),就會導(dǎo)致內(nèi)存溢出
  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();

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 政和县| 汝城县| 康定县| 唐河县| 阆中市| 平阳县| 新乡县| 天全县| 清远市| 平罗县| 荥经县| 巧家县| 乌兰浩特市| 江安县| 关岭| 屯留县| 阳信县| 临桂县| 白沙| 仁怀市| 临湘市| 山阳县| 泽库县| 宜昌市| 讷河市| 高尔夫| 汝南县| 湖口县| 遵义县| 区。| 天镇县| 科尔| 海阳市| 云浮市| 永州市| 吴桥县| 遂宁市| 巨鹿县| 四会市| 崇仁县| 临高县|