JAVA【设计模式】原型模式

一、定义

在这里插入图片描述

原型模式是用于创建复杂的对象,保证了性能,同时能复制出对象私有的属性,它提供了一种创建对象的绝佳方式(clone

二、示例:

模拟场景:
例如:1、细胞的分裂过程,有丝分裂会产生一对完全相同的细胞。 原始细胞就是一个原型, 它在复制体的生成过程中起到了推动作用。

例如:2、每个⼈都经历过考试,从纸制版到上机答题,⼤⼤⼩⼩也有⼏百场。⽽以前坐在教室⾥答题身边的⼈都是⼀套试卷,考试的时候还能偷摸或者别⼈给发信息抄⼀抄答案。

但从⼀部分可以上机考试的内容开始,在保证⼤家的公平性⼀样的题⽬下,开始出现试题混排更有做的好的答案选项也混排。这样⼤⼤的增加了抄的成本,也更好的做到了考试的公平性。

传统编码方式

代码会变的非常的冗长,可扩展差

package com.qf.design.create.prototype.tradition;

import com.qf.design.create.prototype.entity.AnswerQuestion;
import com.qf.design.create.prototype.entity.ChoiceQuestion;

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();
    }

}

原型模式(clone)

package com.qf.design.create.prototype.design;

import com.qf.design.create.prototype.entity.AnswerQuestion;
import com.qf.design.create.prototype.entity.ChoiceQuestion;

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"));
        questionBank.append(new ChoiceQuestion("下列说法正确的是", map02, "A"));
        questionBank.append(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B"));
        questionBank.append(new ChoiceQuestion("以下()不是合法的标识符", map04, "C"));
        questionBank.append(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D"));
        questionBank.append(new AnswerQuestion("小红马和小黑马生的小马几条腿", "4条腿"));
        questionBank.append(new AnswerQuestion("铁棒打头疼还是木棒打头疼", "头最疼"));
        questionBank.append(new AnswerQuestion("什么床不能睡觉", "牙床"));
        questionBank.append(new AnswerQuestion("为什么好马不吃回头草", "后面的草没了"));
    }

    public String createPaper(String candidate, String number) throws CloneNotSupportedException {
        QuestionBank clone = (QuestionBank) questionBank.clone();
        clone.setCandidate(candidate);
        clone.setNumber(number);
        String toString = clone.toString();
        return toString;
    }
}

实现了clone方法,复制对象里面的所有内容。然后重新定义规则:QuestionBank

package com.qf.design.create.prototype.design;

import com.qf.design.create.prototype.design.util.Topic;
import com.qf.design.create.prototype.design.util.TopicRandomUtil;
import com.qf.design.create.prototype.entity.AnswerQuestion;
import com.qf.design.create.prototype.entity.ChoiceQuestion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class QuestionBank implements Cloneable{
    private List<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
    private List<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();
    private String candidate;
    private String number;

    public void append(ChoiceQuestion choiceQuestion){
        choiceQuestionList.add(choiceQuestion);
    }

    public void append(AnswerQuestion answerQuestion){
        answerQuestionList.add(answerQuestion);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        QuestionBank questionBank= (QuestionBank) super.clone();
        questionBank.choiceQuestionList = this.choiceQuestionList;
        questionBank.answerQuestionList = this.answerQuestionList;

        //题目乱序
        Collections.shuffle(choiceQuestionList);
        Collections.shuffle(answerQuestionList);

        //答案乱序
        List<ChoiceQuestion> choiceQuestionList=questionBank.choiceQuestionList;
        for (ChoiceQuestion choiceQuestion : choiceQuestionList) {
            Topic random = TopicRandomUtil.random(choiceQuestion.getOption(), choiceQuestion.getKey());
            choiceQuestion.setKey(random.getKey());
            choiceQuestion.setOption(random.getOption());
        }
        return questionBank;
    }

    public void setCandidate(String candidate){
        this.candidate=candidate;
    }

    public void setNumber(String number){
        this.number=number;
    }

    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();
    }
}

总结:
以上的实际场景模拟了原型模式在开发中᯿构的作⽤,但是原型模式的使⽤频率确实不是很⾼。如
果有⼀些特殊场景需要使⽤到,也可以按照此设计模式进⾏优化。

另外原型设计模式的优点包括;便于通过克隆⽅式创建复杂对象、也可以避免重复做初始化操作、
不需要与类中所属的其他类耦合等。但也有⼀些缺点如果对象中包括了循环引⽤的克隆,以及类中
深度使⽤对象的克隆,都会使此模式变得异常麻烦

posted @ 2022-08-30 22:40  雾托邦  阅读(39)  评论(0编辑  收藏  举报