• 设计模式-原型模式


    原型模式

    适用场景:

    原型模式主要解决的问题就是创建重复对象,⽽而这部分 对象 内容本身⽐比较复杂,生成过程可能从库或者
    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 接口。

    工程结构

    image

    针对每⼀一个试卷都会使⽤用克隆的⽅方式进行复制,复制完成后将试卷中题目以及每个题目的答案进行
    乱序处理

    • 克隆对象处理类

      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 + "
      " +
                      "考号:" + number + "
      " +
                      "--------------------------------------------
      " +
                      "一、选择题" + "
      
      ");
      
              for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
                  detail.append("第").append(idx + 1).append("题:").append(choiceQuestionList.get(idx).getName()).append("
      ");
                  Map<String, String> option = choiceQuestionList.get(idx).getOption();
                  for (String key : option.keySet()) {
                      detail.append(key).append(":").append(option.get(key)).append("
      ");;
                  }
                  detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("
      
      ");
              }
      
              detail.append("二、问答题" + "
      
      ");
      
              for (int idx = 0; idx < answerQuestionList.size(); idx++) {
                  detail.append("第").append(idx + 1).append("题:").append(answerQuestionList.get(idx).getName()).append("
      ");
                  detail.append("答案:").append(answerQuestionList.get(idx).getKey()).append("
      
      ");
              }
      
              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(); 并最终返回试卷信息。

    总结

    原型设计模式的优点包括;便于通过克隆方式创建复杂对象、也可以避免重复做初始化操作、
    不需要与类中所属的其他类耦合等。但也有一些缺点如果对象中包括了了循环引用的克隆,以及类中
    深度使⽤用对象的克隆,都会使此模式变得异常麻烦。

  • 相关阅读:
    Python面向对象编程
    Python模块
    Python函数式编程(把函数作为参数传入)
    Python函数高级特性
    Python函数基础
    连续数字或英文字符文本强制换行
    flex布局文本过长不显示省略号
    在div中放一个相同大小的svg,实际显示的位置svg偏下
    设置git push默认branch
    c# using
  • 原文地址:https://www.cnblogs.com/RealGang/p/14887614.html
Copyright © 2020-2023  润新知