乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java設(shè)計(jì)模式--狀態(tài)模式

       印度阿三17 2019-07-06

        狀態(tài)模式:當(dāng)一個(gè)對(duì)象的內(nèi)在狀態(tài)改變時(shí)允許改變其行為,這個(gè)對(duì)象像是改變了其類。

        乍一看狀態(tài)模式的解釋可能有點(diǎn)不知所以然,其實(shí)這個(gè)模式并不難理解,首先我們看一個(gè)例子

        我們定義了一個(gè)學(xué)習(xí)類,它的一個(gè)studyStatus()方法采用if-else來(lái)做具體的操作。

      public class Study {
      
          private int studyHours;
      
          public int getStudyHours() {
              return studyHours;
          }
      
          public void setStudyHours(int studyHours) {
              this.studyHours = studyHours;
          }
      
      
          public void studyStatus(){
              if(studyHours >0 && studyHours<2){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",剛開始學(xué)習(xí),精力充沛!");
              }else if(studyHours>=2 && studyHours<4){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",稍有疲憊");
              }else if(studyHours >=4 && studyHours<5){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",學(xué)習(xí)效率下降");
              }else if(studyHours >=5 && studyHours <7){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",學(xué)習(xí)效率低下");
              }else {
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",疲憊不堪");
              }
          }
      
      }
      public class Test {
          public static void main(String[] args) {
              Study study = new Study();
              study.setStudyHours(1);
              study.studyStatus();
              study.setStudyHours(3);
              study.studyStatus();
              study.setStudyHours(4);
              study.studyStatus();
              study.setStudyHours(6);
              study.studyStatus();
              study.setStudyHours(8);
              study.studyStatus();
          }
      
      }

      測(cè)試結(jié)果:

      學(xué)習(xí)時(shí)間:1,剛開始學(xué)習(xí),精力充沛!
      學(xué)習(xí)時(shí)間:3,稍有疲憊
      學(xué)習(xí)時(shí)間:4,學(xué)習(xí)效率下降
      學(xué)習(xí)時(shí)間:6,學(xué)習(xí)效率低下
      學(xué)習(xí)時(shí)間:8,疲憊不堪

      我們假設(shè)StudyStatus的study方法內(nèi)條件分支有很多(雖然我這里只有5種),當(dāng)我們把所有分支條件寫在一個(gè)的方法里時(shí),此時(shí)就違反了了“單一職責(zé)原則”,除此之外當(dāng)我們需要修改某一個(gè)分支時(shí),這個(gè)方法下的其他分支也有可能受到影響,這就違反了“開放-封閉原則”,所以我們做如下改動(dòng),首先定義一個(gè)狀態(tài)的接口,實(shí)現(xiàn)類中有一方法studyStatus(),當(dāng)滿足條件是執(zhí)行操作,否則跳入下一分支。

      public interface Status {
          void studyStatus(Study study);
      }
      
      //分支1
      public class Status1 implements Status {
      
          @Override
          public void studyStatus(Study study) {
              int studyHours = study.getStudyHours();
              if(studyHours >0 && studyHours < 2){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",剛開始學(xué)習(xí),精力充沛!");
              }else {
                  study.SetStatus(new Status2());
                  study.studyStatus();
              }
          }
      }
      
      //分支2
      public class Status2 implements Status {
          @Override
          public void studyStatus(Study study) {
              int studyHours = study.getStudyHours();
              if(studyHours >=2 && studyHours < 4){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",稍有疲憊");
              }else {
                  study.SetStatus(new Status3());
                  study.studyStatus();
              }
          }
      }
      
      //分支3
      public class Status3 implements Status {
          @Override
          public void studyStatus(Study study) {
              int studyHours = study.getStudyHours();
              if(studyHours >=4 && studyHours < 5){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",學(xué)習(xí)效率下降");
              }else {
                  study.SetStatus(new Status4());
                  study.studyStatus();
              }
          }
      }
      
      //分支4
      public class Status4 implements Status {
          @Override
          public void studyStatus(Study study) {
              int studyHours = study.getStudyHours();
              if(studyHours >=5 && studyHours < 7){
                  System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",學(xué)習(xí)效率低下");
              }else {
                  study.SetStatus(new Status5());
                  study.studyStatus();
              }
          }
      }
      
      //分支5
      public class Status5 implements Status {
          @Override
          public void studyStatus(Study study) {
              int studyHours = study.getStudyHours();
              System.out.println("學(xué)習(xí)時(shí)間:" studyHours ",疲憊不堪");
          }
      }

      去除if-else判斷的學(xué)習(xí)類,

      public class Study {
      
          //當(dāng)前狀態(tài)
          private Status current;
      
          private int studyHours;
      
          //初始化時(shí)從第一個(gè)分支開始
          public Study(){
              this.current = new Status1();
          }
      
          public void SetStatus(Status status){
              this.current = status;
          }
      
          public int getStudyHours() {
              return studyHours;
          }
      
          public void setStudyHours(int studyHours) {
              this.studyHours = studyHours;
          }
      
          //獲取當(dāng)前狀態(tài)
          public void studyStatus(){
              current.studyStatus(this);
          }
      
      }

       

      測(cè)試類

      public class Test {
          public static void main(String[] args) {
              Study study = new Study();
              study.setStudyHours(1);
              study.studyStatus();
              Study study2 = new Study();
              study2.setStudyHours(3);
              study2.studyStatus();
              Study study3 = new Study();
              study3.setStudyHours(4);
              study3.studyStatus();
              Study study4 = new Study();
              study4.setStudyHours(6);
              study4.studyStatus();
              Study study5 = new Study();
              study5.setStudyHours(8);
              study5.studyStatus();
          }
      
      }

      ?

        總結(jié):我們?nèi)コ薸f-else結(jié)構(gòu)使得代碼可維護(hù)性更強(qiáng),當(dāng)我們?cè)偬砑臃种r(shí)就會(huì)盡可能少的改動(dòng)原來(lái)的代碼,但是這種模式也有缺點(diǎn),會(huì)使類的數(shù)量增加,增加系統(tǒng)的復(fù)雜性,所以我們?cè)陂_發(fā)時(shí)需要考慮以后是否會(huì)有更多的分支添加進(jìn)來(lái)來(lái)決定是否使用該模式。

      來(lái)源:https://www./content-1-305601.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多