<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      24.12.04

      實(shí)驗(yàn) 20:備忘錄模式

      本次實(shí)驗(yàn)屬于模仿型實(shí)驗(yàn),通過(guò)本次實(shí)驗(yàn)學(xué)生將掌握以下內(nèi)容:

      1、理解備忘錄模式的動(dòng)機(jī),掌握該模式的結(jié)構(gòu);

      2、能夠利用備忘錄模式解決實(shí)際問(wèn)題。

      [實(shí)驗(yàn)任務(wù)一]:多次撤銷

      改進(jìn)課堂上的“用戶信息操作撤銷”實(shí)例,使得系統(tǒng)可以實(shí)現(xiàn)多次撤銷(可以使用HashMap、ArrayList等集合數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn))。

      實(shí)驗(yàn)要求:

      1.      畫(huà)出對(duì)應(yīng)的類圖;

      2.      提交源代碼;

      3. 注意編程規(guī)范。

       

      1. 類圖

       

       

       

       

      1. 源代碼:

      import java.util.ArrayList;

      import java.util.List;

       

      // Memento類

      class Memento {

          private String account;

          private String password;

          private String telNo;

       

          public Memento(String account, String password, String telNo) {

              this.account = account;

              this.password = password;

              this.telNo = telNo;

          }

       

          public String getAccount() {

              return account;

          }

       

          public String getPassword() {

              return password;

          }

       

          public String getTelNo() {

              return telNo;

          }

      }

       

      // Caretaker類

      class Caretaker {

          private List<Memento> mementoList = new ArrayList<>();

          private int currentIndex = -1;  // 當(dāng)前狀態(tài)索引

       

          public void addMemento(Memento memento) {

              // 如果當(dāng)前狀態(tài)不是最新?tīng)顟B(tài),則移除當(dāng)前索引后的歷史記錄

              if (currentIndex != mementoList.size() - 1) {

                  mementoList = mementoList.subList(0, currentIndex + 1);

              }

              mementoList.add(memento);

              currentIndex++;

          }

       

          public Memento undo() {

              if (currentIndex <= 0) {

                  System.out.println("無(wú)法撤銷,已是初始狀態(tài)。");

                  return null;

              }

              currentIndex--;

              return mementoList.get(currentIndex);

          }

       

          public Memento redo() {

              if (currentIndex >= mementoList.size() - 1) {

                  System.out.println("無(wú)法恢復(fù),已是最新?tīng)顟B(tài)。");

                  return null;

              }

              currentIndex++;

              return mementoList.get(currentIndex);

          }

       

          public Memento getCurrentMemento() {

              if (currentIndex >= 0 && currentIndex < mementoList.size()) {

                  return mementoList.get(currentIndex);

              }

              return null;

          }

      }

       

      // UserInfoDTO類

      public class UserInfoDTO {

          private String account;

          private String password;

          private String telNo;

       

          public String getAccount() {

              return account;

          }

       

          public void setAccount(String account) {

              this.account = account;

          }

       

          public String getPassword() {

              return password;

          }

       

          public void setPassword(String password) {

              this.password = password;

          }

       

          public String getTelNo() {

              return telNo;

          }

       

          public void setTelNo(String telNo) {

              this.telNo = telNo;

          }

       

          public Memento saveMemento() {

              return new Memento(account, password, telNo);

          }

       

          public void restoreMemento(Memento memento) {

              if (memento != null) {

                  this.account = memento.getAccount();

                  this.password = memento.getPassword();

                  this.telNo = memento.getTelNo();

              }

          }

       

          public void show() {

              System.out.println("Account: " + account);

              System.out.println("Password: " + password);

              System.out.println("TelNo: " + telNo);

          }

      }

       

      // Client類

      public class Client {

          public static void main(String[] args) {

              UserInfoDTO user = new UserInfoDTO();

              Caretaker caretaker = new Caretaker();

       

              // 初始狀態(tài)

              user.setAccount("zhangsan");

              user.setPassword("123456");

              user.setTelNo("13000000000");

              caretaker.addMemento(user.saveMemento());

       

              System.out.println("狀態(tài)一:");

              user.show();

              System.out.println("---------------------------");

       

              // 修改狀態(tài)

              user.setPassword("111111");

              user.setTelNo("13100001111");

              caretaker.addMemento(user.saveMemento());

       

              System.out.println("狀態(tài)二:");

              user.show();

              System.out.println("---------------------------");

       

              // 再次修改狀態(tài)

              user.setPassword("222222");

              user.setTelNo("13200002222");

              caretaker.addMemento(user.saveMemento());

       

              System.out.println("狀態(tài)三:");

              user.show();

              System.out.println("---------------------------");

       

              // 多次撤銷

              user.restoreMemento(caretaker.undo());

              System.out.println("撤銷一次:");

              user.show();

              System.out.println("---------------------------");

       

              user.restoreMemento(caretaker.undo());

              System.out.println("撤銷兩次:");

              user.show();

              System.out.println("---------------------------");

       

              // 恢復(fù)狀態(tài)

              user.restoreMemento(caretaker.redo());

              System.out.println("恢復(fù)一次:");

              user.show();

              System.out.println("---------------------------");

       

              user.restoreMemento(caretaker.redo());

              System.out.println("恢復(fù)到最新?tīng)顟B(tài):");

              user.show();

              System.out.println("---------------------------");

          }

      }

      posted on 2024-12-04 21:36  Daniel350  閱讀(6)  評(píng)論(0)    收藏  舉報(bào)

      主站蜘蛛池模板: 国产精品国产精品一区精品| 无码日韩精品一区二区免费| 超碰自拍成人在线观看| 久久综合精品成人一本| 欧洲极品少妇| 色综合人人超人人超级国碰| 欧美牲交a欧美牲交aⅴ一| 国产三级精品三级在线观看| 国模一区二区三区私拍视频| 福利一区二区在线播放| 久久精品国产蜜臀av| 国产精品午夜福利精品| 亚洲A综合一区二区三区| 亚洲天堂网中文在线资源| 亚洲一区二区三区av链接| 国产女同一区二区在线| 久久国产免费直播| 国产福利永久在线视频无毒不卡 | 亚洲欧美综合人成在线| 久久一级精品久熟女人妻| 国产成人精品手机在线观看| 国产精品成人av电影不卡| 少妇性l交大片| 日韩一区二区三区不卡片| 亚洲男女羞羞无遮挡久久丫| 亚洲欧洲日韩国内高清| аⅴ天堂国产最新版在线中文| 做暖暖视频在线看片免费| 精品视频福利| 免费全部高h视频无码| 免费一区二三区三区蜜桃| 不卡一区二区国产在线| 丰满人妻被黑人连续中出| 日韩一区精品视频一区二区| 国产18禁一区二区三区| 国产成人一区二区三区免费| 人妻少妇无码精品专区| 超碰人人超碰人人| 夜夜高潮次次欢爽av女| 狠狠躁夜夜躁无码中文字幕| 欧美日韩中文国产一区|