筹划模式之原型模式(上机考试多套试,每人标题和答案乱序分列场景)

[复制链接]
发表于 2025-12-28 20:15:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
媒介:
平常你也付出了很多的时间,但就是没有得到多少收益。就像偶然候很多小同伴问我,我是该怎么学一个我没打仗过的内容。我的个人履历非常发起,先不要学太多理论性的内容,而是实验实际使用下,把要学的内容做一些Demo案例出来。这有点像你买了个自行车是先拆了学学怎么个原理,还是先骑几圈呢?哪怕摔了跟头,但那都是必须履历后留下的履历。
原型模式先容

原型模式重要办理的题目就是创建重复对象,而这部门对象内容本身比力复杂,天生过程大概从库大概RPC接口中获取数据的耗时较长,因此接纳克隆的方式节省时间。
着实这种场景经常出如今我们的身边,只不外很少用到本身的开辟中,就像;
A:你经常Ctrl+C、Ctrl+V,复制粘贴代码。
B:Java多数类中提供的API方法;Object clone()。
C:细胞的有丝分裂。
案例场景模仿
从纸制版到上机答题,大巨微小也有几百场。而从前坐在讲堂里答题身边的人都是一套试卷,考试的时间还能偷摸大概别人给发信息抄一抄答案。
但从一部门可以上机考试的内容开始,在包管各人的公平性一样的标题下,开始出现试题混排更有做的好的答案选项也混排。如许大大的增长了抄的本钱,也更好的做到了考试的公平性。
由于须要实现一个上机考试抽题的服务,因此在这里制作一个题库标题的场景类信息,用于创建;选择题、问答题。

在这里模仿了两个试卷标题的类;ChoiceQuestion(选择题)、AnswerQuestion(问答题)。假如是实际的业务场景开辟中,会有更多的标题范例。
选择题
  1. package com.lm.design;
  2. import java.util.Map;
  3. /**
  4. * 单选题
  5. */
  6. public class ChoiceQuestion {
  7.     private String name;                 // 题目
  8.     private Map<String, String> option;  // 选项;A、B、C、D
  9.     private String key;                  // 答案;B
  10.     public ChoiceQuestion() {
  11.     }
  12.     public ChoiceQuestion(String name, Map<String, String> option, String key) {
  13.         this.name = name;
  14.         this.option = option;
  15.         this.key = key;
  16.     }
  17.     public String getName() {
  18.         return name;
  19.     }
  20.     public void setName(String name) {
  21.         this.name = name;
  22.     }
  23.     public Map<String, String> getOption() {
  24.         return option;
  25.     }
  26.     public void setOption(Map<String, String> option) {
  27.         this.option = option;
  28.     }
  29.     public String getKey() {
  30.         return key;
  31.     }
  32.     public void setKey(String key) {
  33.         this.key = key;
  34.     }
  35. }
复制代码
问答题
  1. package com.lm.design;
  2. /**
  3. * 解答题
  4. */
  5. public class AnswerQuestion {
  6.     private String name;  // 问题
  7.     private String key;   // 答案
  8.     public AnswerQuestion() {
  9.     }
  10.     public AnswerQuestion(String name, String key) {
  11.         this.name = name;
  12.         this.key = key;
  13.     }
  14.     public String getName() {
  15.         return name;
  16.     }
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.     public String getKey() {
  21.         return key;
  22.     }
  23.     public void setKey(String key) {
  24.         this.key = key;
  25.     }
  26. }
复制代码
用一坨坨代码实现

一把梭哈代码
  1. package com.lm.design;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class QuestionBankController {
  7.     public String createPaper(String candidate, String number) {
  8.         List<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
  9.         List<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
  10.         Map<String, String> map01 = new HashMap<String, String>();
  11.         map01.put("A", "JAVA2 EE");
  12.         map01.put("B", "JAVA2 Card");
  13.         map01.put("C", "JAVA2 ME");
  14.         map01.put("D", "JAVA2 HE");
  15.         map01.put("E", "JAVA2 SE");
  16.         Map<String, String> map02 = new HashMap<String, String>();
  17.         map02.put("A", "JAVA程序的main方法必须写在类里面");
  18.         map02.put("B", "JAVA程序中可以有多个main方法");
  19.         map02.put("C", "JAVA程序中类名必须与文件名一样");
  20.         map02.put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");
  21.         Map<String, String> map03 = new HashMap<String, String>();
  22.         map03.put("A", "变量由字母、下划线、数字、$符号随意组成;");
  23.         map03.put("B", "变量不能以数字作为开头;");
  24.         map03.put("C", "A和a在java中是同一个变量;");
  25.         map03.put("D", "不同类型的变量,可以起相同的名字;");
  26.         Map<String, String> map04 = new HashMap<String, String>();
  27.         map04.put("A", "STRING");
  28.         map04.put("B", "x3x;");
  29.         map04.put("C", "void");
  30.         map04.put("D", "de$f");
  31.         Map<String, String> map05 = new HashMap<String, String>();
  32.         map05.put("A", "31");
  33.         map05.put("B", "0");
  34.         map05.put("C", "1");
  35.         map05.put("D", "2");
  36.         choiceQuestionList.add(new ChoiceQuestion("JAVA所定义的版本中不包括", map01, "D"));
  37.         choiceQuestionList.add(new ChoiceQuestion("下列说法正确的是", map02, "A"));
  38.         choiceQuestionList.add(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B"));
  39.         choiceQuestionList.add(new ChoiceQuestion("以下()不是合法的标识符", map04, "C"));
  40.         choiceQuestionList.add(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D"));
  41.         answerQuestionList.add(new AnswerQuestion("小红马和小黑马生的小马几条腿", "4条腿"));
  42.         answerQuestionList.add(new AnswerQuestion("铁棒打头疼还是木棒打头疼", "头最疼"));
  43.         answerQuestionList.add(new AnswerQuestion("什么床不能睡觉", "牙床"));
  44.         answerQuestionList.add(new AnswerQuestion("为什么好马不吃回头草", "后面的草没了"));
  45.         // 输出结果
  46.         StringBuilder detail = new StringBuilder("考生:" + candidate + "\r\n" +
  47.                 "考号:" + number + "\r\n" +
  48.                 "--------------------------------------------\r\n" +
  49.                 "一、选择题" + "\r\n\n");
  50.         for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
  51.             detail.append("第").append(idx + 1).append("题:").append(choiceQuestionList.get(idx).getName()).append("\r\n");
  52.             Map<String, String> option = choiceQuestionList.get(idx).getOption();
  53.             for (String key : option.keySet()) {
  54.                 detail.append(key).append(":").append(option.get(key)).append("\r\n");
  55.                 ;
  56.             }
  57.             detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
  58.         }
  59.         detail.append("二、问答题" + "\r\n\n");
  60.         for (int idx = 0; idx < answerQuestionList.size(); idx++) {
  61.             detail.append("第").append(idx + 1).append("题:").append(answerQuestionList.get(idx).getName()).append("\r\n");
  62.             detail.append("答案:").append(answerQuestionList.get(idx).getKey()).append("\r\n\n");
  63.         }
  64.         return detail.toString();
  65.     }
  66. }
复制代码
如许的代码通常都非常易于明确,要什么步伐就给什么代码,不面向对象,只面向过程。不思量扩展性,能用就行。
测试
  1. package com.lm.desgin.test;
  2. import com.lm.design.QuestionBankController;
  3. import org.junit.Test;
  4. public class ApiTest {
  5.     @Test
  6.     public void test_QuestionBankController() {
  7.         QuestionBankController questionBankController = new QuestionBankController();
  8.         System.out.println(questionBankController.createPaper("花花", "1000001921032"));
  9.         System.out.println(questionBankController.createPaper("豆豆", "1000001921051"));
  10.         System.out.println(questionBankController.createPaper("大宝", "1000001921987"));
  11.     }
  12. }
复制代码
A:以上呢就是三位考试的试卷;花花、豆豆、大宝,每个人的试卷内容是一样的这没题目,但是三个人的标题以及选项次序都是一样,就没有到达我们说盼望的乱序要求。
B:而且以上如许的代码非常难扩展,随着标题的不停的增长以及乱序功能的增补,都会让这段代码变得越来越杂乱。
原型模式重构代码
原型模式重要办理的题目就是创建大量重复的类,而我们模仿的场景就须要给差异的用户都创建类似的试卷,但这些试卷的标题未便于每次都从库中获取,以致偶然候须要从长途的RPC中获取。如许都黑白常耗时的,而且随着创建对象的增多将严厉影响服从。
在原型模式中所须要的非常告急的本事就是克隆,在须要用到克隆的类中都须要实现 implements Cloneable 接口。

工程中包罗了焦点的题库类QuestionBank,题库中重要负责将各个的标题举行组装终极输出试卷。
针对每一个试卷都会使用克隆的方式举行复制,复制完成后将试卷中标题以及每个标题的答案举行乱序处理惩罚。这里提供了工具包;TopicRandomUtil
标题选项乱序使用工具包
  1. package com.lm.design.util;
  2. import java.util.*;
  3. public class TopicRandomUtil {
  4.     /**
  5.      * 乱序Map元素,记录对应答案key
  6.      * @param option 题目
  7.      * @param key    答案
  8.      * @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
  9.      */
  10.     static public Topic random(Map<String, String> option, String key) {
  11.         Set<String> keySet = option.keySet();
  12.         ArrayList<String> keyList = new ArrayList<String>(keySet);
  13.         Collections.shuffle(keyList);
  14.         HashMap<String, String> optionNew = new HashMap<String, String>();
  15.         int idx = 0;
  16.         String keyNew = "";
  17.         for (String next : keySet) {
  18.             String randomKey = keyList.get(idx++);
  19.             if (key.equals(next)) {
  20.                 keyNew = randomKey;
  21.             }
  22.             optionNew.put(randomKey, option.get(next));
  23.         }
  24.         return new Topic(optionNew, keyNew);
  25.     }
  26. }
复制代码
这个这个工具类的使用就是将原有Map中的选型乱序使用,也就是A的选项内容给B,B的大概给C,同时记载精确答案在处理惩罚后的位置信息。
克隆对象处理惩罚类
  1. package com.lm.design;
  2. import com.lm.design.util.Topic;
  3. import com.lm.design.util.TopicRandomUtil;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Map;
  7. public class QuestionBank implements Cloneable {
  8.     private String candidate; // 考生
  9.     private String number;    // 考号
  10.     private ArrayList<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
  11.     private ArrayList<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
  12.     public QuestionBank append(ChoiceQuestion choiceQuestion) {
  13.         choiceQuestionList.add(choiceQuestion);
  14.         return this;
  15.     }
  16.     public QuestionBank append(AnswerQuestion answerQuestion) {
  17.         answerQuestionList.add(answerQuestion);
  18.         return this;
  19.     }
  20.     @Override
  21.     public Object clone() throws CloneNotSupportedException {
  22.         QuestionBank questionBank = (QuestionBank) super.clone();
  23.         questionBank.choiceQuestionList = (ArrayList<ChoiceQuestion>) choiceQuestionList.clone();
  24.         questionBank.answerQuestionList = (ArrayList<AnswerQuestion>) answerQuestionList.clone();
  25.         // 题目乱序
  26.         Collections.shuffle(questionBank.choiceQuestionList);
  27.         Collections.shuffle(questionBank.answerQuestionList);
  28.         // 答案乱序
  29.         ArrayList<ChoiceQuestion> choiceQuestionList = questionBank.choiceQuestionList;
  30.         for (ChoiceQuestion question : choiceQuestionList) {
  31.             Topic random = TopicRandomUtil.random(question.getOption(), question.getKey());
  32.             question.setOption(random.getOption());
  33.             question.setKey(random.getKey());
  34.         }
  35.         return questionBank;
  36.     }
  37.     public void setCandidate(String candidate) {
  38.         this.candidate = candidate;
  39.     }
  40.     public void setNumber(String number) {
  41.         this.number = number;
  42.     }
  43.     @Override
  44.     public String toString() {
  45.         StringBuilder detail = new StringBuilder("考生:" + candidate + "\r\n" +
  46.                 "考号:" + number + "\r\n" +
  47.                 "--------------------------------------------\r\n" +
  48.                 "一、选择题" + "\r\n\n");
  49.         for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
  50.             detail.append("第").append(idx + 1).append("题:").append(choiceQuestionList.get(idx).getName()).append("\r\n");
  51.             Map<String, String> option = choiceQuestionList.get(idx).getOption();
  52.             for (String key : option.keySet()) {
  53.                 detail.append(key).append(":").append(option.get(key)).append("\r\n");;
  54.             }
  55.             detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
  56.         }
  57.         detail.append("二、问答题" + "\r\n\n");
  58.         for (int idx = 0; idx < answerQuestionList.size(); idx++) {
  59.             detail.append("第").append(idx + 1).append("题:").append(answerQuestionList.get(idx).getName()).append("\r\n");
  60.             detail.append("答案:").append(answerQuestionList.get(idx).getKey()).append("\r\n\n");
  61.         }
  62.         return detail.toString();
  63.     }
  64. }
复制代码
两个append(),对各项标题的添加,有点像我们在制作者模式中使用的方式,添加装修物料。
clone(),这里的焦点使用就是对对象的复制,这里的复制不但是包罗了本身,同时对两个聚集也做了复制。只有如许的拷贝才气确保在使用克隆对象的时间不影响原对象。
乱序使用,在list聚集中有一个方法,Collections.shuffle,可以将原有聚集的次序打乱,输出一个新的次序。在这里我们使用此方法对标题举行乱序使用。
初始化试卷数据
  1. package com.lm.design;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class QuestionBankController {
  5.     private QuestionBank questionBank = new QuestionBank();
  6.     public QuestionBankController() {
  7.         Map<String, String> map01 = new HashMap<String, String>();
  8.         map01.put("A", "JAVA2 EE");
  9.         map01.put("B", "JAVA2 Card");
  10.         map01.put("C", "JAVA2 ME");
  11.         map01.put("D", "JAVA2 HE");
  12.         map01.put("E", "JAVA2 SE");
  13.         Map<String, String> map02 = new HashMap<String, String>();
  14.         map02.put("A", "JAVA程序的main方法必须写在类里面");
  15.         map02.put("B", "JAVA程序中可以有多个main方法");
  16.         map02.put("C", "JAVA程序中类名必须与文件名一样");
  17.         map02.put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");
  18.         Map<String, String> map03 = new HashMap<String, String>();
  19.         map03.put("A", "变量由字母、下划线、数字、$符号随意组成;");
  20.         map03.put("B", "变量不能以数字作为开头;");
  21.         map03.put("C", "A和a在java中是同一个变量;");
  22.         map03.put("D", "不同类型的变量,可以起相同的名字;");
  23.         Map<String, String> map04 = new HashMap<String, String>();
  24.         map04.put("A", "STRING");
  25.         map04.put("B", "x3x;");
  26.         map04.put("C", "void");
  27.         map04.put("D", "de$f");
  28.         Map<String, String> map05 = new HashMap<String, String>();
  29.         map05.put("A", "31");
  30.         map05.put("B", "0");
  31.         map05.put("C", "1");
  32.         map05.put("D", "2");
  33.         questionBank.append(new ChoiceQuestion("JAVA所定义的版本中不包括", map01, "D"))
  34.                 .append(new ChoiceQuestion("下列说法正确的是", map02, "A"))
  35.                 .append(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B"))
  36.                 .append(new ChoiceQuestion("以下()不是合法的标识符",map04, "C"))
  37.                 .append(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D"))
  38.                 .append(new AnswerQuestion("小红马和小黑马生的小马几条腿", "4条腿"))
  39.                 .append(new AnswerQuestion("铁棒打头疼还是木棒打头疼", "头最疼"))
  40.                 .append(new AnswerQuestion("什么床不能睡觉", "牙床"))
  41.                 .append(new AnswerQuestion("为什么好马不吃回头草", "后面的草没了"));
  42.     }
  43.     public String createPaper(String candidate, String number) throws CloneNotSupportedException {
  44.         QuestionBank questionBankClone = (QuestionBank) questionBank.clone();
  45.         questionBankClone.setCandidate(candidate);
  46.         questionBankClone.setNumber(number);
  47.         return questionBankClone.toString();
  48.     }
  49. }
复制代码
测试验证
  1. package com.lm.desgin.test;
  2. import com.lm.design.QuestionBankController;
  3. import org.junit.Test;
  4. public class ApiTest {
  5.     @Test
  6.     public void test_QuestionBank() throws CloneNotSupportedException {
  7.         QuestionBankController questionBankController = new QuestionBankController();
  8.         System.out.println(questionBankController.createPaper("花花", "1000001921032"));
  9.         System.out.println(questionBankController.createPaper("豆豆", "1000001921051"));
  10.         System.out.println(questionBankController.createPaper("大宝", "1000001921987"));
  11.     }
  12. }
复制代码
总结
以上的实际场景模仿了原型模式在开辟中重构的作用,但是原型模式的使用频率确实不是很高。假如有一些特殊场景须要使用到,也可以按照此筹划模式举行优化。
别的原型筹划模式的优点包罗;便于通过克隆方式创建复杂对象、也可以克制重复做初始化使用、不须要与类中所属的其他类耦合等。但也有一些缺点假如对象中包罗了循环引用的克隆,以及类中深度使用对象的克隆,都会使此模式变得非常贫困。
好了 至此筹划模式之原型模式(上机考试多套试,每人标题和答案乱序分列场景)     学习竣事了 友友们 点点关注不迷路 老铁们!!!!!
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表