《重学Java计划模式》之 原型模式

[复制链接]
发表于 2025-12-29 17:55:06 | 显示全部楼层 |阅读模式

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

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

×
原型模式重要办理的题目就是创建重复对象,而这部门对象内容本身比力复杂,天生过程大概从库大概RPC接口中获取数据的耗时较长,因此接纳克隆的方式节省时间。

案例:上机考试抽题,要求打乱标题、答案数据
工厂布局


选择题实体类
  1. public class ChoiceQuestion {
  2.     private String name;                 // 题目
  3.     private Map<String, String> option;  // 选项;A、B、C、D
  4.     private String key;                  // 答案;B
  5. }
复制代码
简答题实体类
  1. public class AnswerQuestion {
  2.     private String name;  // 问题
  3.     private String key;   // 答案
  4. }
复制代码
选项重排工具类
  1. public class TopicRandomUtil {
  2.     /**
  3.      * 乱序Map元素,记录对应答案key
  4.      * @param option 题目
  5.      * @param key    答案
  6.      * @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
  7.      */
  8.     static public Topic random(Map<String, String> option, String key) {
  9.         Set<String> keySet = option.keySet();
  10.         ArrayList<String> keyList = new ArrayList<String>(keySet);
  11.         Collections.shuffle(keyList);
  12.         HashMap<String, String> optionNew = new HashMap<String, String>();
  13.         int idx = 0;
  14.         String keyNew = "";
  15.         for (String next : keySet) {
  16.             String randomKey = keyList.get(idx++);
  17.             if (key.equals(next)) {
  18.                 keyNew = randomKey;
  19.             }
  20.             optionNew.put(randomKey, option.get(next));
  21.         }
  22.         return new Topic(optionNew, keyNew);
  23.     }
  24. }
复制代码
克隆对象处置惩罚类
  1. public class QuestionBank implements Cloneable{
  2.     private String candidate; // 考生
  3.     private String number;    // 考号
  4.     private ArrayList<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
  5.     private ArrayList<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
  6.     public QuestionBank append(ChoiceQuestion choiceQuestion) {
  7.         choiceQuestionList.add(choiceQuestion);
  8.         return this;
  9.     }
  10.     public QuestionBank append(AnswerQuestion answerQuestion) {
  11.         answerQuestionList.add(answerQuestion);
  12.         return this;
  13.     }
  14.     @Override
  15.     public Object clone() throws CloneNotSupportedException {
  16.         QuestionBank questionBank = (QuestionBank) super.clone();
  17.         questionBank.choiceQuestionList = (ArrayList<ChoiceQuestion>) choiceQuestionList.clone();
  18.         questionBank.answerQuestionList = (ArrayList<AnswerQuestion>) answerQuestionList.clone();
  19.         // 题目乱序
  20.         Collections.shuffle(questionBank.choiceQuestionList);
  21.         Collections.shuffle(questionBank.answerQuestionList);
  22.         // 答案乱序
  23.         ArrayList<ChoiceQuestion> choiceQuestionList = questionBank.choiceQuestionList;
  24.         for (ChoiceQuestion question : choiceQuestionList) {
  25.             Topic random = TopicRandomUtil.random(question.getOption(), question.getKey());
  26.             question.setOption(random.getOption());
  27.             question.setKey(random.getKey());
  28.         }
  29.         return questionBank;
  30.     }
  31.     public void setCandidate(String candidate) {
  32.         this.candidate = candidate;
  33.     }
  34.     public void setNumber(String number) {
  35.         this.number = number;
  36.     }
  37.     @Override
  38.     public String toString() {
  39.         StringBuilder detail = new StringBuilder("考生:" + candidate + "\r\n" +
  40.                 "考号:" + number + "\r\n" +
  41.                 "--------------------------------------------\r\n" +
  42.                 "一、选择题" + "\r\n\n");
  43.         for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
  44.             detail.append("第").append(idx + 1).append("题:").append(choiceQuestionList.get(idx).getName()).append("\r\n");
  45.             Map<String, String> option = choiceQuestionList.get(idx).getOption();
  46.             for (String key : option.keySet()) {
  47.                 detail.append(key).append(":").append(option.get(key)).append("\r\n");;
  48.             }
  49.             detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
  50.         }
  51.         detail.append("二、问答题" + "\r\n\n");
  52.         for (int idx = 0; idx < answerQuestionList.size(); idx++) {
  53.             detail.append("第").append(idx + 1).append("题:").append(answerQuestionList.get(idx).getName()).append("\r\n");
  54.             detail.append("答案:").append(answerQuestionList.get(idx).getKey()).append("\r\n\n");
  55.         }
  56.         return detail.toString();
  57.     }
  58. }
复制代码
初始化试题试卷
  1. public class QuestionBankController {
  2.     private QuestionBank questionBank = new QuestionBank();
  3.     public QuestionBankController() {
  4.         Map<String, String> map01 = new HashMap<String, String>();
  5.         map01.put("A", "JAVA2 EE");
  6.         map01.put("B", "JAVA2 Card");
  7.         map01.put("C", "JAVA2 ME");
  8.         map01.put("D", "JAVA2 HE");
  9.         map01.put("E", "JAVA2 SE");
  10.         Map<String, String> map02 = new HashMap<String, String>();
  11.         map02.put("A", "JAVA程序的main方法必须写在类里面");
  12.         map02.put("B", "JAVA程序中可以有多个main方法");
  13.         map02.put("C", "JAVA程序中类名必须与文件名一样");
  14.         map02.put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");
  15.         Map<String, String> map03 = new HashMap<String, String>();
  16.         map03.put("A", "变量由字母、下划线、数字、$符号随意组成;");
  17.         map03.put("B", "变量不能以数字作为开头;");
  18.         map03.put("C", "A和a在java中是同一个变量;");
  19.         map03.put("D", "不同类型的变量,可以起相同的名字;");
  20.         Map<String, String> map04 = new HashMap<String, String>();
  21.         map04.put("A", "STRING");
  22.         map04.put("B", "x3x;");
  23.         map04.put("C", "void");
  24.         map04.put("D", "de$f");
  25.         Map<String, String> map05 = new HashMap<String, String>();
  26.         map05.put("A", "31");
  27.         map05.put("B", "0");
  28.         map05.put("C", "1");
  29.         map05.put("D", "2");
  30.         questionBank.append(new ChoiceQuestion("JAVA所定义的版本中不包括", map01, "D"))
  31.                 .append(new ChoiceQuestion("下列说法正确的是", map02, "A"))
  32.                 .append(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B"))
  33.                 .append(new ChoiceQuestion("以下()不是合法的标识符",map04, "C"))
  34.                 .append(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D"))
  35.                 .append(new AnswerQuestion("小红马和小黑马生的小马几条腿", "4条腿"))
  36.                 .append(new AnswerQuestion("铁棒打头疼还是木棒打头疼", "头最疼"))
  37.                 .append(new AnswerQuestion("什么床不能睡觉", "牙床"))
  38.                 .append(new AnswerQuestion("为什么好马不吃回头草", "后面的草没了"));
  39.     }
  40.     public String createPaper(String candidate, String number) throws CloneNotSupportedException {
  41.         QuestionBank questionBankClone = (QuestionBank) questionBank.clone();
  42.         questionBankClone.setCandidate(candidate);
  43.         questionBankClone.setNumber(number);
  44.         return questionBankClone.toString();
  45.     }
  46. }
复制代码
测试
  1.     @GetMapping("/questionBank")
  2.     public String questionBank() throws CloneNotSupportedException {
  3.         QuestionBankController questionBankController = new QuestionBankController();
  4.         System.out.println(questionBankController.createPaper("花花", "1000001921032"));
  5.         System.out.println(questionBankController.createPaper("豆豆", "1000001921051"));
  6.         System.out.println(questionBankController.createPaper("大宝", "1000001921987"));
  7.         return "奖品发放成功!";
  8.     }
复制代码

回复

使用道具 举报

登录后关闭弹窗

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