1. 程式人生 > 實用技巧 >虛擬資料中心儲存延遲如何分析-雲安

虛擬資料中心儲存延遲如何分析-雲安

原型模式

適用場景:

原型模式主要解決的問題就是建立重複物件,⽽而這部分 物件 內容本身⽐比較複雜,生成過程可能從庫或者
RPC介面中獲取資料的耗時較長,因此採⽤用克隆的方式節省時間。

原型模式主要解決的問題就是建立⼤大量量重複的類,

案例:實現一個上機考試抽題的服務,因此在這里建造一個題庫題目的場景類資訊,用於建立; 選擇
題 、 問答題 。

場景描述
  • 選擇題

    /**
     * 單選題
     */
    public class ChoiceQuestion {
    
        private String name;                 // 題目
        private Map<String, String> option;  // 選項;A、B、C、D
        private String key;                  // 答案;B
    
        public ChoiceQuestion() {
        }
    
        public ChoiceQuestion(String name, Map<String, String> option, String key) {
            this.name = name;
            this.option = option;
            this.key = key;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Map<String, String> getOption() {
            return option;
        }
    
        public void setOption(Map<String, String> option) {
            this.option = option;
        }
    
        public String getKey() {
            return key;
        }
    
        public void setKey(String key) {
            this.key = key;
        }
    }
    
    • 問答題

      /**
       * 解答題
       */
      public class AnswerQuestion {
      
          private String name;  // 問題
          private String key;   // 答案
      
          public AnswerQuestion() {
          }
      
          public AnswerQuestion(String name, String key) {
              this.name = name;
              this.key = key;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public String getKey() {
              return key;
          }
      
          public void setKey(String key) {
              this.key = key;
          }
      }
      

利用原型模式設計同一套試卷亂序的題目

在原型模式中所需要的非常重要的手段就是克隆,在需要用到克隆的類中都需要實現 implements
Cloneable 介面。

工程結構

針對每⼀一個試卷都會使⽤用克隆的⽅方式進行復制,複製完成後將試卷中題目以及每個題目的答案進行
亂序處理

  • 克隆物件處理類

    public class QuestionBank implements Cloneable {
    
        private String candidate; // 考生
        private String number;    // 考號
    
        private ArrayList<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
        private ArrayList<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
    
        public QuestionBank append(ChoiceQuestion choiceQuestion) {
            choiceQuestionList.add(choiceQuestion);
            return this;
        }
    
        public QuestionBank append(AnswerQuestion answerQuestion) {
            answerQuestionList.add(answerQuestion);
            return this;
        }
    
        @Override
        public Object clone() throws CloneNotSupportedException {
            QuestionBank questionBank = (QuestionBank) super.clone();
            questionBank.choiceQuestionList = (ArrayList<ChoiceQuestion>) choiceQuestionList.clone();
            questionBank.answerQuestionList = (ArrayList<AnswerQuestion>) answerQuestionList.clone();
    
            // 題目亂序
            Collections.shuffle(questionBank.choiceQuestionList);
            Collections.shuffle(questionBank.answerQuestionList);
            // 答案亂序
            ArrayList<ChoiceQuestion> choiceQuestionList = questionBank.choiceQuestionList;
            for (ChoiceQuestion question : choiceQuestionList) {
                Topic random = TopicRandomUtil.random(question.getOption(), question.getKey());
                question.setOption(random.getOption());
                question.setKey(random.getKey());
            }
            return questionBank;
        }
    
        public void setCandidate(String candidate) {
            this.candidate = candidate;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        @Override
        public String toString() {
    
            StringBuilder detail = new StringBuilder("考生:" + candidate + "\r\n" +
                    "考號:" + number + "\r\n" +
                    "--------------------------------------------\r\n" +
                    "一、選擇題" + "\r\n\n");
    
            for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
                detail.append("第").append(idx + 1).append("題:").append(choiceQuestionList.get(idx).getName()).append("\r\n");
                Map<String, String> option = choiceQuestionList.get(idx).getOption();
                for (String key : option.keySet()) {
                    detail.append(key).append(":").append(option.get(key)).append("\r\n");;
                }
                detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
            }
    
            detail.append("二、問答題" + "\r\n\n");
    
            for (int idx = 0; idx < answerQuestionList.size(); idx++) {
                detail.append("第").append(idx + 1).append("題:").append(answerQuestionList.get(idx).getName()).append("\r\n");
                detail.append("答案:").append(answerQuestionList.get(idx).getKey()).append("\r\n\n");
            }
    
            return detail.toString();
        }
    
    }
    

    這里的主要操作內容有三個,分別是:

    • 兩個 append() ,對各項題目的新增,有點像我們在建造者模式中使用的方式,新增裝修物料。
    • clone() ,這里的核心操作就是對物件的複製,這里的複製不只是包括了本身,同時對兩個集合
      也做了複製。只有這樣的拷貝才能確保在操作克隆物件的時候不影響原物件。
    • 亂序操作,在 list 集合中有一個⽅方法, Collections.shuffle ,可以將原有集合的順序打亂,
      輸出一個新的順序
  • 初始化試卷

    public class QuestionBankController {
    
        private QuestionBank questionBank = new QuestionBank();
    
        public QuestionBankController() {
    
            Map<String, String> map01 = new HashMap<String, String>();
            map01.put("A", "JAVA2 EE");
            map01.put("B", "JAVA2 Card");
            map01.put("C", "JAVA2 ME");
            map01.put("D", "JAVA2 HE");
            map01.put("E", "JAVA2 SE");
    
            Map<String, String> map02 = new HashMap<String, String>();
            map02.put("A", "JAVA程式的main方法必須寫在類裡面");
            map02.put("B", "JAVA程式中可以有多個main方法");
            map02.put("C", "JAVA程式中類名必須與檔名一樣");
            map02.put("D", "JAVA程式的main方法中如果只有一條語句,可以不用{}(大括號)括起來");
    
            Map<String, String> map03 = new HashMap<String, String>();
            map03.put("A", "變數由字母、下劃線、數字、$符號隨意組成;");
            map03.put("B", "變數不能以數字作為開頭;");
            map03.put("C", "A和a在java中是同一個變數;");
            map03.put("D", "不同型別的變數,可以起相同的名字;");
    
            Map<String, String> map04 = new HashMap<String, String>();
            map04.put("A", "STRING");
            map04.put("B", "x3x;");
            map04.put("C", "void");
            map04.put("D", "de$f");
    
            Map<String, String> map05 = new HashMap<String, String>();
            map05.put("A", "31");
            map05.put("B", "0");
            map05.put("C", "1");
            map05.put("D", "2");
    
            questionBank.append(new ChoiceQuestion("JAVA所定義的版本中不包括", map01, "D"))
                    .append(new ChoiceQuestion("下列說法正確的是", map02, "A"))
                    .append(new ChoiceQuestion("變數命名規範說法正確的是", map03, "B"))
                    .append(new ChoiceQuestion("以下()不是合法的識別符號",map04, "C"))
                    .append(new ChoiceQuestion("表示式(11+3*8)/4%3的值是", map05, "D"))
                    .append(new AnswerQuestion("小紅馬和小黑馬生的小馬幾條腿", "4條腿"))
                    .append(new AnswerQuestion("鐵棒打頭疼還是木棒打頭疼", "頭最疼"))
                    .append(new AnswerQuestion("什麼床不能睡覺", "牙床"))
                    .append(new AnswerQuestion("為什麼好馬不吃回頭草", "後面的草沒了"));
        }
    
        public String createPaper(String candidate, String number) throws CloneNotSupportedException {
            QuestionBank questionBankClone = (QuestionBank) questionBank.clone();
            questionBankClone.setCandidate(candidate);
            questionBankClone.setNumber(number);
            return questionBankClone.toString();
        }
    
    }
    
    • 主要提供對試卷內容的模式初始化操作(所有考⽣生試卷一樣,題目順
      序不一致)。
    • 對外部提供建立試卷的方法,在建立的過程中使用的是克隆的方式; (QuestionBank)
      questionBank.clone(); 並最終返回試卷資訊。

總結

原型設計模式的優點包括;便於通過克隆方式建立複雜物件、也可以避免重複做初始化操作、
不需要與類中所屬的其他類耦合等。但也有一些缺點如果物件中包括了了迴圈引用的克隆,以及類中
深度使⽤用物件的克隆,都會使此模式變得異常麻煩。