马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
媒介:
平常你也付出了很多的时间,但就是没有得到多少收益。就像偶然候很多小同伴问我,我是该怎么学一个我没打仗过的内容。我的个人履历非常发起,先不要学太多理论性的内容,而是实验实际使用下,把要学的内容做一些Demo案例出来。这有点像你买了个自行车是先拆了学学怎么个原理,还是先骑几圈呢?哪怕摔了跟头,但那都是必须履历后留下的履历。
原型模式先容
原型模式重要办理的题目就是创建重复对象,而这部门对象内容本身比力复杂,天生过程大概从库大概RPC接口中获取数据的耗时较长,因此接纳克隆的方式节省时间。
着实这种场景经常出如今我们的身边,只不外很少用到本身的开辟中,就像;
A:你经常Ctrl+C、Ctrl+V,复制粘贴代码。
B:Java多数类中提供的API方法;Object clone()。
C:细胞的有丝分裂。
案例场景模仿
从纸制版到上机答题,大巨微小也有几百场。而从前坐在讲堂里答题身边的人都是一套试卷,考试的时间还能偷摸大概别人给发信息抄一抄答案。
但从一部门可以上机考试的内容开始,在包管各人的公平性一样的标题下,开始出现试题混排更有做的好的答案选项也混排。如许大大的增长了抄的本钱,也更好的做到了考试的公平性。
由于须要实现一个上机考试抽题的服务,因此在这里制作一个题库标题的场景类信息,用于创建;选择题、问答题。
在这里模仿了两个试卷标题的类;ChoiceQuestion(选择题)、AnswerQuestion(问答题)。假如是实际的业务场景开辟中,会有更多的标题范例。
选择题- package com.lm.design;
- import java.util.Map;
- /**
- * 单选题
- */
- 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;
- }
- }
复制代码 问答题- package com.lm.design;
- /**
- * 解答题
- */
- 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;
- }
- }
复制代码 用一坨坨代码实现

一把梭哈代码- package com.lm.design;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class QuestionBankController {
- public String createPaper(String candidate, String number) {
- List<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
- List<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
- 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");
- choiceQuestionList.add(new ChoiceQuestion("JAVA所定义的版本中不包括", map01, "D"));
- choiceQuestionList.add(new ChoiceQuestion("下列说法正确的是", map02, "A"));
- choiceQuestionList.add(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B"));
- choiceQuestionList.add(new ChoiceQuestion("以下()不是合法的标识符", map04, "C"));
- choiceQuestionList.add(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D"));
- answerQuestionList.add(new AnswerQuestion("小红马和小黑马生的小马几条腿", "4条腿"));
- answerQuestionList.add(new AnswerQuestion("铁棒打头疼还是木棒打头疼", "头最疼"));
- answerQuestionList.add(new AnswerQuestion("什么床不能睡觉", "牙床"));
- answerQuestionList.add(new AnswerQuestion("为什么好马不吃回头草", "后面的草没了"));
- // 输出结果
- 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();
- }
- }
复制代码 如许的代码通常都非常易于明确,要什么步伐就给什么代码,不面向对象,只面向过程。不思量扩展性,能用就行。
测试- package com.lm.desgin.test;
- import com.lm.design.QuestionBankController;
- import org.junit.Test;
- public class ApiTest {
- @Test
- public void test_QuestionBankController() {
- QuestionBankController questionBankController = new QuestionBankController();
- System.out.println(questionBankController.createPaper("花花", "1000001921032"));
- System.out.println(questionBankController.createPaper("豆豆", "1000001921051"));
- System.out.println(questionBankController.createPaper("大宝", "1000001921987"));
- }
- }
复制代码 A:以上呢就是三位考试的试卷;花花、豆豆、大宝,每个人的试卷内容是一样的这没题目,但是三个人的标题以及选项次序都是一样,就没有到达我们说盼望的乱序要求。
B:而且以上如许的代码非常难扩展,随着标题的不停的增长以及乱序功能的增补,都会让这段代码变得越来越杂乱。
原型模式重构代码
原型模式重要办理的题目就是创建大量重复的类,而我们模仿的场景就须要给差异的用户都创建类似的试卷,但这些试卷的标题未便于每次都从库中获取,以致偶然候须要从长途的RPC中获取。如许都黑白常耗时的,而且随着创建对象的增多将严厉影响服从。
在原型模式中所须要的非常告急的本事就是克隆,在须要用到克隆的类中都须要实现 implements Cloneable 接口。
工程中包罗了焦点的题库类QuestionBank,题库中重要负责将各个的标题举行组装终极输出试卷。
针对每一个试卷都会使用克隆的方式举行复制,复制完成后将试卷中标题以及每个标题的答案举行乱序处理惩罚。这里提供了工具包;TopicRandomUtil
标题选项乱序使用工具包- package com.lm.design.util;
- import java.util.*;
- public class TopicRandomUtil {
- /**
- * 乱序Map元素,记录对应答案key
- * @param option 题目
- * @param key 答案
- * @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
- */
- static public Topic random(Map<String, String> option, String key) {
- Set<String> keySet = option.keySet();
- ArrayList<String> keyList = new ArrayList<String>(keySet);
- Collections.shuffle(keyList);
- HashMap<String, String> optionNew = new HashMap<String, String>();
- int idx = 0;
- String keyNew = "";
- for (String next : keySet) {
- String randomKey = keyList.get(idx++);
- if (key.equals(next)) {
- keyNew = randomKey;
- }
- optionNew.put(randomKey, option.get(next));
- }
- return new Topic(optionNew, keyNew);
- }
- }
复制代码 这个这个工具类的使用就是将原有Map中的选型乱序使用,也就是A的选项内容给B,B的大概给C,同时记载精确答案在处理惩罚后的位置信息。
克隆对象处理惩罚类- package com.lm.design;
- import com.lm.design.util.Topic;
- import com.lm.design.util.TopicRandomUtil;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Map;
- 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,可以将原有聚集的次序打乱,输出一个新的次序。在这里我们使用此方法对标题举行乱序使用。
初始化试卷数据- package com.lm.design;
- import java.util.HashMap;
- import java.util.Map;
- 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();
- }
- }
复制代码 测试验证- package com.lm.desgin.test;
- import com.lm.design.QuestionBankController;
- import org.junit.Test;
- public class ApiTest {
- @Test
- public void test_QuestionBank() throws CloneNotSupportedException {
- QuestionBankController questionBankController = new QuestionBankController();
- System.out.println(questionBankController.createPaper("花花", "1000001921032"));
- System.out.println(questionBankController.createPaper("豆豆", "1000001921051"));
- System.out.println(questionBankController.createPaper("大宝", "1000001921987"));
- }
- }
复制代码 总结
以上的实际场景模仿了原型模式在开辟中重构的作用,但是原型模式的使用频率确实不是很高。假如有一些特殊场景须要使用到,也可以按照此筹划模式举行优化。
别的原型筹划模式的优点包罗;便于通过克隆方式创建复杂对象、也可以克制重复做初始化使用、不须要与类中所属的其他类耦合等。但也有一些缺点假如对象中包罗了循环引用的克隆,以及类中深度使用对象的克隆,都会使此模式变得非常贫困。
好了 至此筹划模式之原型模式(上机考试多套试,每人标题和答案乱序分列场景) 学习竣事了 友友们 点点关注不迷路 老铁们!!!!!
|