一、定义
原型模式主要解决的是创建重复对象的问题,而这部分对象内容本身比较复杂,从数据库或者RPC接口中获取相关对象数据的耗时较长,因此需要采用复制的方式节省时间,例如图所示的批量复制和生产机器人。
这种场景也经常出现在我们身边,只不过很少有人提炼出这种设计思想,并运用到自己的系统开发中,就像
- 经常使用Ctrl+C、Ctrl+V组合键复制和粘贴代码。
- 细胞的有丝分裂。
类似的场景并不少,但在平时的代码开发中并不容易找到这样的设计模式,甚至有时即使遇到了也会忽略。在没有阅读下文之前,可以思考有哪些场景可以用到这种设计模式。
二、问题背景
每个人都经历过考试,大部分情况都是在纸质的试卷上答题,随着互联网的兴起,也有一些考试改为上机考试。
从时间效率和成本上看,上机考试可以大大降低判卷的人工成本,提高判分效率。上机考试还可以提高考试的公平性,将同样的题目混排,可以更好地降低抄袭的可能性,在这种情况下对应的答案也是混排的。
同样的试卷题目,如果是人工判卷,很难实现题目混排,但放在计算机上,无论是生成试卷还是判卷都能轻而易举地实现。下面就来实现这样的功能:同样一张试卷、同样的题目、同样的答案,把题目和答案全部混排。
在模拟工程中,提供了试卷中两类题目:选择题类(ChoiceQuestion)和问答题类(AnswerQuestion)。
如果是实际的业务开发,还会有更多的考试题目类型,读者在练习时可以自行添加。
(1)选择题
在选择题类(ChoiceQuestion)中,提供了题目名称、题目选项和题目答案三种属性。
/**
* 单选题
*/
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)问答题类
在问答题类(AnswerQuestion)中,提供了问题和答案两种属性。
/**
* 解答题
*/
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;
}
}
三、违背设计模式的设计实现
按照通常的需求迭代过程,往往最开始都是非常简单的,也是非常容易实现的。
需求最初的模样,只是给每位考生创建出一张试卷即可,对于这样简单的需求,如果不仔细思考,可能会把所有代码写到一个类里。
这个工程的结构只有一个用于生成试卷的控制类QuestionBankController,接下来看这样的类是如何实现的。
public class QuestionBankController {
public String createPaper(String candidate, String number) {
List<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
choiceQuestionList.add(new ChoiceQuestion("JAVA所定义的版本中不包括", new HashMap<String, String>() {{
put("A", "JAVA2 EE");
put("B", "JAVA2 Card");
put("C", "JAVA2 ME");
put("D", "JAVA2 HE");
put("E", "JAVA2 SE");
}}, "D"));
choiceQuestionList.add(new ChoiceQuestion("下列说法正确的是", new HashMap<String, String>() {{
put("A", "JAVA程序的main方法必须写在类里面");
put("B", "JAVA程序中可以有多个main方法");
put("C", "JAVA程序中类名必须与文件名一样");
put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");
}}, "A"));
choiceQuestionList.add(new ChoiceQuestion("变量命名规范说法正确的是", new HashMap<String, String>() {{
put("A", "变量由字母、下划线、数字、$符号随意组成;");
put("B", "变量不能以数字作为开头;");
put("C", "A和a在java中是同一个变量;");
put("D", "不同类型的变量,可以起相同的名字;");
}}, "B"));
choiceQuestionList.add(new ChoiceQuestion("以下()不是合法的标识符", new HashMap<String, String>() {{
put("A", "STRING");
put("B", "x3x;");
put("C", "void");
put("D", "de$f");
}}, "C"));
choiceQuestionList.add(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", new HashMap<String, String>() {{
put("A", "31");
put("B", "0");
put("C", "1");
put("D", "2");
}}, "D"));
List<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
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();
}
}
以上的代码主要包括三部分内容:将选择题和问答题创建到集合中,定义详情字符串包装结果,返回结果内容。
单从代码阅读角度来看,这样的代码并不复杂,且更易于理解。
因为它的编程方式不面向对象,只面向程序过程,业务逻辑需要什么就直接写什么。
不考虑扩展性,能运行即可。
但这段代码没有实现题目和答案乱序的功能,最终所有人的试卷题目的顺序都是一样的。如果需要增加混排题目功能,则代码实现就会非常混乱。
四、问题改进
原型模式主要解决的问题是创建大量的重复对象,而这里模拟的场景同样是需要给不同的考生创建相同的试卷,但在创建过程中,这些试卷的题目不应该每次都从数据库或者远程 RPC 接口中获取。
这些操作都是非常耗时的,而且随着创建对象的增多,将严重降低创建效率。
另外,在解决获取相同试卷题目的问题后,还需要将试卷的题目与答案混排。
而这种混排的过程就可以使用原型模式。
在原型模式中,需要的重要技术手段是复制,而在需要用到复制的类中需要实现 implements Cloneable
接口。
重构后的整个工程结构并不复杂,主要包括如下内容:
- 题目类ChoiceQuestion、AnswerQuestion被用在题库创建中;
- 针对每一张试卷,都会复制。复制完成后,将试卷的题目及相应的答案混排。这里提供了工具包TopicRandomUtil。
- 核心的题库类QuestionBank主要负责将各个题目进行组装,最终输出试卷。
(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);
}
}
考题答案混排的工具包提供了实现混排的random方法。其核心逻辑如下:
- 在混排操作方法中,首先把题目选项使用 Java 中Collections 工具包里的shuffle方法进行混排操作;
- 记录混排后正确答案的位置key.equals(next),最终返回新的题目选项单Topic;
- 混排的过程也就是把A的选项内容给B、把B的选项内容给 C,同时把正确答案位置标记出来。
(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,可以将原有集合的顺序打乱,输出一个新的顺序。这里使用此方法对题目进行混排操作。
(3)初始化试卷数据
public class QuestionBankController {
private QuestionBank questionBank = new QuestionBank();
public QuestionBankController() {
questionBank.append(new ChoiceQuestion("JAVA所定义的版本中不包括", new HashMap<String, String>() {{
put("A", "JAVA2 EE");
put("B", "JAVA2 Card");
put("C", "JAVA2 ME");
put("D", "JAVA2 HE");
put("E", "JAVA2 SE");
}}, "D")).append(new ChoiceQuestion("下列说法正确的是", new HashMap<String, String>() {{
put("A", "JAVA程序的main方法必须写在类里面");
put("B", "JAVA程序中可以有多个main方法");
put("C", "JAVA程序中类名必须与文件名一样");
put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");
}}, "A")).append(new ChoiceQuestion("变量命名规范说法正确的是", new HashMap<String, String>() {{
put("A", "变量由字母、下划线、数字、$符号随意组成;");
put("B", "变量不能以数字作为开头;");
put("C", "A和a在java中是同一个变量;");
put("D", "不同类型的变量,可以起相同的名字;");
}}, "B")).append(new ChoiceQuestion("以下()不是合法的标识符", new HashMap<String, String>() {{
put("A", "STRING");
put("B", "x3x;");
put("C", "void");
put("D", "de$f");
}}, "C")).append(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", new HashMap<String, String>() {{
put("A", "31");
put("B", "0");
put("C", "1");
put("D", "2");
}}, "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();
,并最终返回试卷信息。
五、总结
以上的实际业务场景模拟了原型模式在开发中的作用。因为原型模式的使用频率不是很高,所以如果有一些特殊场景需要使用,可以按照此设计模式优化。
另外,原型设计模式的优点包括:便于通过克隆方式创建复杂对象,也可以避免重复初始化,不需要与类中所属的其他类耦合等。
但也有一些缺点,如果对象中包括了循环引用的复制,以及类中深度使用对象的复制,都会使此模式变得非常麻烦。
设计模式终究是一种设计思想,只有在不同的场景中合理地运用才能提升整体架构的质量。
永远不要想着生硬地套用设计模式,否则将会导致过渡设计,并在满足业务反复变化的需求时造成开发浪费,增加维护成本。
另外,初期是代码的优化,中期是设计模式的使用,后期是把控全局服务的搭建。
只有不断地加强自己对全局能力的把控,才能加深自己对细节处理的理解。