程序员这份工作里有两种人;一类是热爱喜欢的、一类是仅当成工作的。而喜欢代码编程的这部分人会 极其主动学习去丰富自己的羽翼,也非常喜欢对技术探索力求将学到的知识能到平时的业务需求开发 中。对于这部分小伙伴来说上班写代码还能赚钱真的是幸福!
无论做哪行那业你都喜欢,往往来自从中持续不断都获取成就感。就开发编程而言因为你的一行代码影
响到了千千万万的人、因为你的一行代码整个系统更加稳定、因为你的一行代码扛过了了所有秒杀等等,
这样一行行的代码都是你日积月累学习到的经验。那如果你也想成为这样有成就感的程序员就需要不断
的学习,不断的用更多的技能知识把自己编写的代码运用到更核心的系统。
平常你也付出了很多的时间,但就是没有得到多少收益。就像有时候很多小伙伴问我,我是该怎么学一
个我没接触过的内容。我的个人经验非常建议,先不不要学太多理论性的内容,而是尝试实际操作下,把
要学的内容做一些Demo案例例出来。这有点像你买了个自行车是先拆了学怎么个原理,还是先骑几圈
呢?哪怕摔了跟头,但那都是必须经历后留下的经验。
同样我也知道很多人看了设计模式收获不大,这主要新人对没有案例或者案例不贴近实际场景没有学习
方向导致。太空、太虚、太玄,让人没有抓手!
所以我开始编写以实际案例为着手的方式,讲解设计模式的文章,帮助大家成长的同时也让我自己有所
沉淀!
一、开发环境
-
二、原型模式介绍
原型模式主要解决的问题就是创建重复对象,⽽这部分对象内容本身比较复杂,生成过程可能从库或者 RPC接口中获取数据的耗时较长,因此采用克隆的方式节省时间。
其实这种场景经常出现在我们的身边,只不过很少用到自己的开发中,就像;
1. 你经常 Ctrl+C 、 Ctrl+V ,复制粘贴代码。
2. Java多数类中提供的API⽅方法; Object clone() 。
3. 细胞的有丝分裂。
类似以上的场景并不少,但如果让你去思考平时的代码开发中,有用到这样的设计模式吗?确实不那么 容易找到,甚至有时候是忽略了这个设计模式的方式。在没有阅读下文之前,也可以思考下哪些场景可 以用到。三、案例场景模拟
每个人都经历过考试,从纸制版到上机答题,⼤大⼩小也有几百场。而以前坐在教室里答题身边的人都 是一套试卷,考试的时候还能偷摸或者别人给发信息抄一抄答案。
但从一部分可以上机考试的内容开始,在保证大家的公平性一样的题目下,开始出现试题混排更有做的 好的答案选项也混排。这样⼤大的增加了抄的成本,也更好的做到了考试的公平性。 但如果这个公平性的考试需求交给你来完成,你会怎么做?
因为需要实现一个上机考试抽题的服务,因此在这里建造一个题库题目的场景类信息,用于创建 选择 题 、 问答题 。1. 场景模拟工程
在这里模拟了两个试卷题目的类; ChoiceQuestion ( 选择题 )、 AnswerQuestion ( 问答题 )。如果 是实际的业务场景开发中,会有更多的题目类型,可以回忆一下你的高考试卷。2. 场景简述
2.1 选择题
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;
}
}
2.2 问答题
```java 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;
} }
以上两个类就是我们场景中需要的物料内容,相对来说比较简单。如果你在测试的时候想扩充学
习,可以继续添加一些其他物料( 题目类型 )。
<a name="AZvn9"></a>
# 四、用一坨坨代码实现
今天的实现方式没有ifelse了,但是没有一个类解决不了的业务,只要你胆大!在以下的例子中我们会按照每一个用户创建试卷的题目,并返回给调用方。
<a name="VpBER"></a>
## 1. 工程结构
![image.png](https://cdn.nlark.com/yuque/0/2021/png/22376009/1631790128225-f3315a4c-3890-4add-9b2e-782d15fd1170.png#clientId=uf03b5fac-5cd9-4&from=paste&height=144&id=ufe4c8e90&margin=%5Bobject%20Object%5D&name=image.png&originHeight=144&originWidth=393&originalType=binary&ratio=1&size=7513&status=done&style=none&taskId=uc6d06200-26b7-4506-a1ac-6d7f4d5071b&width=393)
<a name="gw0b5"></a>
## 2. 一把梭实现需求
```java
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();
}
}
这样的代码往往都非常易于理解,要什么程序就给什么代码,不面向对象,只面向过程。不考虑扩
展性,能用就行。
以上的代码主要就三部分内容;首先创建选择题和问答题到集合中、定义详情字符串包装结果、返
回结果内容。
但以上的代码有一个没有实现的地方就是不能乱序,所有人的试卷顺序都是一样的。如果需要加乱
序也是可以的,但复杂度又会增加。 这里不展示具体过多实现,只为后文对比重构
3. 测试验证
接下来我们通过junit单元测试的方式验证接口服务,强调日常编写好单测可以更好的提高系统的健壮
度。
编写测试类:
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"));
}
}
结果:
以上呢就是三位考试的试卷; 花花 、 ⾖豆 、 大宝 ,每个人的试卷内容是一样的这没问题,但是三
个人的题⽬目以及选项顺序都是一样,就没有达到我们说希望的乱序要求。
而且以上这样的代码非常难扩展,随着题目的不断的增加以及乱序功能的补充,都会让这段代码变
得越来越混乱。
五、原型模式重构代码
原型模式主要解决的问题就是创建大量重复的类,而我们模拟的场景就需要给不同的用户都创建相同的
试卷,但这些试卷的题目不便于每次都从库中获取,甚至有时候需要从远程的RPC中获取。这样都是非
常耗时的,而且随着创建对象的增多将严重影响效率。
在原型模式中所需要的非常重要的手段就是克隆,在需要用到克隆的类中都需要实现 implements
Cloneable 接口。
1. 工程结构
原型模式模型结构
工程中包括了核心的题库类 QuestionBank ,题库中主要负责将各个的题目进行组装最终输出试
卷。
针对每一个试卷都会使用克隆的方式进行复制,复制完成后将试卷中题目以及每个题目的答案进行
乱序处理理。这里提供了工具包;TopicRandomUtil
2. 代码实现
2.1 题目选项乱序操作工具包
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存储题目选项,同时key的属性存放答案。 如果忘记可以往上翻翻
这个这个⼯具类的操作就是将原有Map中的选型乱序操作, 也就是A的选项内容给B , B的可能给
C ,同时记录正确答案在处理后的位置信息。
2.2 克隆对象处理类
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 ,可以将原有集合的顺序打乱,
输出一个新的顺序。在这里我们使用此方法对题目进行乱序操作。
2.3初始化试卷数据
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();
}
}
3. 测试验证
编写测试类:
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"));
}
}
结果:
从以上的输出结果可以看到,每个人的题目和答案都是差异化的乱序的
六、总结
以上的实际场景模拟了原型模式在开发中重构的作用,但是原型模式的使用频率确实不是很高。如
果有一些特殊场景需要使用到,也可以按照此设计模式进行优化。
另外原型设计模式的优点包括;便于通过克隆方式创建复杂对象、也可以避免重复做初始化操作、
不需要与类中所属的其他类耦合等。但也有一些缺点如果对象中包括了循环引用的克隆,以及类中
深度使用对象的克隆,都会使此模式变得异常麻烦。
终究设计模式是一整套的思想,在不同的场景合理的运用可以提升整体的架构的质量。永远不要想着去硬凑设计模式,否则将会引起过渡设计,以及在承接业务反复变化的需求时造成浪费的开发和
维护成本。
初期是代码的优化,中期是设计模式的使用,后期是把控全局服务的搭建。不断的加强自己对全局
能力的把控,也加深自己对细节的处理。可上可下才是一个程序员最佳处理方式,选取做合适的才
是最好的选择。