您当前的位置: 首页 > 

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

设计模式 原型模式

java持续实践 发布时间:2022-03-20 13:03:54 ,浏览量:2

文章目录
      • 原型模式介绍
      • 原型模式实战
        • 不使用设计模式实现试卷
        • 使用原型模式实现不同考生的试卷

原型模式介绍

原型模式用来解决创建重复对象, 这些对象创建的内容比较复杂, 可能涉及到到远程服务调用, 采用克隆方式创建此对象, 节省时间.

原型模式实战

需求背景: 给每个人创建试卷, 试卷的题目是相同的, 但需要选项不同, 防止作弊.

创建tutorials-7.0-0模块, 创建选择题类和问答题类. pom如下




    4.0.0

    tutorials-7.0-0
    org.example
    1.0-SNAPSHOT


选择题类ChoiceQuestion

import java.util.Map;

/**单选题
 * 类名称:ChoiceQuestion
 * @author: https://javaweixin6.blog.csdn.net/
 * 创建时间:2022/3/18 7:10
 */
public class ChoiceQuestion {
    // 题目
    private String name;
    // 选项a b c d
    private Map option;
    // 答案
    private String key;

    public ChoiceQuestion() {
    }

    public ChoiceQuestion(String name, Map 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 getOption() {
        return option;
    }

    public void setOption(Map option) {
        this.option = option;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

问答题类

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;
    }
}
不使用设计模式实现试卷

创建tutorials-7.0-1 模块, pom如下




    4.0.0

    tutorials-7.0-1
    org.example
    1.0-SNAPSHOT

    
        
            junit
            junit
            4.12
            test
        

        
            tutorials-7.0-0
            org.example
            1.0-SNAPSHOT
        
    

生成问卷的类QuestionBankController 使用流水账式的代码生成试卷, 且没有实现选择题不同考生乱序.

public class QuestionBankController {

    public String createPaper(String candidate, String number) {
        List choiceQuestionList = new ArrayList();
        List answerQuestionList = new ArrayList();

        Map map01 = new HashMap();
        map01.put("A", "JAVA2 EE");
        map01.put("B", "JAVA2 CARD");
        map01.put("C", "JAVA2 ME");
        map01.put("D", "JAVA2 HE");

        Map map02 = new HashMap();
        map02.put("A", "JAVA程序的main方法必须写在类里面");
        map02.put("B", "JAVA程序中可以有多个main方法");
        map02.put("C", "JAVA程序中类名必须与文件名一样");
        map02.put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");

        Map map03 = new HashMap();
        map03.put("A", "变量由字母、下划线、数字、$符号随意组成;");
        map03.put("B", "变量不能以数字作为开头;");
        map03.put("C", "A和a在java中是同一个变量;");
        map03.put("D", "不同类型的变量,可以起相同的名字;");

        Map map04 = new HashMap();
        map04.put("A", "STRING");
        map04.put("B", "x3x;");
        map04.put("C", "void");
        map04.put("D", "de$f");

        Map map05 = new HashMap();
        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 
    
        mydesign-study
        org.example
        1.0-SNAPSHOT
    
    4.0.0
    tutorials-7.0-2

    
        
            junit
            junit
            4.12
            test
        

        
            tutorials-7.0-0
            org.example
            1.0-SNAPSHOT
        
    

创建Topic 用于存储每个考题

public class Topic {

    private Map option;
    private String key;

    public Topic() {
    }

    public Topic(Map option, String key) {
        this.option = option;
        this.key = key;
    }

    public Map getOption() {
        return option;
    }

    public void setOption(Map option) {
        this.option = option;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

创建TopicRandomUtil , 用于随机生成选择题选择的类

import java.util.*;
/**
 * 类名称:TopicRandomUtil  随机生成试卷的类
 * 创建时间:2022/3/18 7:43
 */
public class TopicRandomUtil {

    /**
     *  乱序map 元素  记录对应答案 key
     * @param option
     * @param key
     * @return
     */
    static public Topic random(Map option, String key) {
        Set keySet = option.keySet();
        ArrayList keyList = new ArrayList(keySet);
        Collections.shuffle(keyList);
        HashMap optionNew = new HashMap();
        int idx = 0;
        String keyNew = "";
        for (String currentSelect : keySet) {
            // 同时遍历新的乱序的key
            String randomKey = keyList.get(idx++);
            if (key.equals(currentSelect)) {
                // 比较当前遍历的key 是否为原始答案, 如果是, 则把当前新乱序的key作为答案.
                keyNew = randomKey;
            }
            optionNew.put(randomKey, option.get(currentSelect));
        }
        return new Topic(optionNew, keyNew);
    }
}

创建QuestionBank类, 该类实现了Cloneable, 用于实现原型模式

import com.thc.design.demo.AnswerQuestion;
import com.thc.design.demo.ChoiceQuestion;
import com.thc.design.util.Topic;
import com.thc.design.util.TopicRandomUtil;

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

/**
 * 类名称:QuestionBank
 * 类描述: 克隆对象处理类
 *
 * @author: https://javaweixin6.blog.csdn.net/
 * 创建时间:2022/3/18 7:53
 */
public class QuestionBank implements Cloneable {
    // 考生
    private String candidate;
    // 考号
    private String number;

    private ArrayList choiceQuestionList = new ArrayList();
    private ArrayList answerQuestionList = new ArrayList();
    // 给试卷添加选择题
    public QuestionBank append(ChoiceQuestion choiceQuestion) {
        choiceQuestionList.add(choiceQuestion);
        return this;
    }
    // 给试卷添加问答题
    public QuestionBank append(AnswerQuestion answerQuestion) {
        answerQuestionList.add(answerQuestion);
        return this;
    }

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

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

        // 选择题答案乱序
        ArrayList 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             
关注
打赏
1658054974
查看更多评论
0.0469s