Browse Source

feat: 🎱 原型模式

master
niushuai233 1 year ago
parent
commit
ae6249ad35
  1. 7
      pom.xml
  2. 179
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/QuestionService.java
  3. 94
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/Student.java
  4. 61
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/Answer.java
  5. 40
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/Option.java
  6. 44
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/Question.java
  7. 28
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/QuestionTypeEnum.java
  8. 28
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/SelectAnswerEnum.java
  9. 24
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/package-info.java
  10. 43
      src/test/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/QuestionTest.java

7
pom.xml

@ -15,6 +15,13 @@
</properties> </properties>
<dependencies> <dependencies>
<!--google guava-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.0.0-jre</version>
</dependency>
<!--lombok--> <!--lombok-->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

179
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/QuestionService.java

@ -0,0 +1,179 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01;
import cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity.*;
import cn.hutool.core.util.SerializeUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* 问题
*
* @author niushuai233
* @date 2023/12/22 9:36
* @since 0.0.1
*/
public class QuestionService {
private static List<Question> questions = new ArrayList<Question>();
static {
initSelectQuestions();
initJudgeQuestions();
initAnswerQuestions();
}
private static void initSelectQuestions() {
questions.add(createSelectQuestion("下列说法正确的是",
"JAVA程序中可以有多个main⽅法",
"JAVA程序的main⽅法必须写在类⾥⾯",
"JAVA程序的main⽅法中如果只有⼀条语句,可以不⽤{}(⼤括号)括起来",
"JAVA程序中类名必须与⽂件名⼀样", SelectAnswerEnum.B));
questions.add(createSelectQuestion("表达式(11+3*8)/4%3的值是", "2", "1", "31", "0", SelectAnswerEnum.A));
questions.add(createSelectQuestion("以下()不是合法的标识符", "void", "de$f", "x3x", "STRING", SelectAnswerEnum.A));
questions.add(createSelectQuestion("1+1=?", "4", "2", "3", "1", SelectAnswerEnum.B));
questions.add(createSelectQuestion("变量命名规范说法正确的是",
"变量不能以数字作为开头",
"A和a在java中是同⼀个变量",
"不同类型的变量,可以起相同的名字",
"变量由字⺟、下划线、数字、$符号随意组成", SelectAnswerEnum.D));
}
private static Question createSelectQuestion(String title, String optionA, String optionB, String optionC, String optionD, SelectAnswerEnum answer) {
Question question = new Question();
question.setType(QuestionTypeEnum.SELECT);
question.setTitle(title);
ArrayList<Option> options = new ArrayList<>();
options.add(new Option(SelectAnswerEnum.A, optionA));
options.add(new Option(SelectAnswerEnum.B, optionB));
options.add(new Option(SelectAnswerEnum.C, optionC));
options.add(new Option(SelectAnswerEnum.D, optionD));
question.setAnswer(new Answer(answer));
question.setOptions(options);
return question;
}
private static void initJudgeQuestions() {
questions.add(createJudgeQuestion("5比4多25%, 4比5少20%", true));
questions.add(createJudgeQuestion("1+1>2", false));
questions.add(createJudgeQuestion("1斤铁比1斤棉花重", false));
questions.add(createJudgeQuestion("明天一定会下雨", false));
questions.add(createJudgeQuestion("一天有24小时", true));
}
private static Question createJudgeQuestion(String title, Boolean answer) {
Question question = new Question();
question.setType(QuestionTypeEnum.JUDGE);
question.setTitle(title);
question.setAnswer(new Answer(answer));
return question;
}
private static void initAnswerQuestions() {
questions.add(createAnswerQuestion("接口一定要有返回值", "不一定"));
questions.add(createAnswerQuestion("什么床不能睡觉", "⽛床"));
questions.add(createAnswerQuestion("铁棒打头疼还是⽊棒打头疼", "头最疼"));
questions.add(createAnswerQuestion("为什么好⻢不吃回头草", "后⾯的草没了"));
questions.add(createAnswerQuestion("⼩红⻢和⼩⿊⻢⽣的⼩⻢⼏条腿", "4条腿"));
}
private static Question createAnswerQuestion(String title, String answer) {
Question question = new Question();
question.setType(QuestionTypeEnum.ANSWER);
question.setTitle(title);
question.setAnswer(new Answer(answer));
return question;
}
public static List<Question> getQuestions() {
return disturbQuestionOrder(cloneQuestions());
}
private static List<Question> cloneQuestions() {
List<Question> list = new ArrayList<>(questions.size());
for (Question question : questions) {
Question cloneQuestion = SerializeUtil.clone(question);
list.add(cloneQuestion);
}
return list;
}
private static List<Question> disturbQuestionOrder(List<Question> questions) {
List<Question> list = new ArrayList<>(questions.size());
List<Question> selectQuestions = questions.stream().filter(item -> QuestionTypeEnum.SELECT.equals(item.getType())).collect(Collectors.toList());
// 打乱答案顺序
selectQuestions.forEach(QuestionService::disturbAnswerOrder);
// 选择题乱序
Collections.shuffle(selectQuestions);
// 判断题乱序
List<Question> judgeQuestions = questions.stream().filter(item -> QuestionTypeEnum.JUDGE.equals(item.getType())).collect(Collectors.toList());
Collections.shuffle(judgeQuestions);
// 问答题乱序
List<Question> answerQuestions = questions.stream().filter(item -> QuestionTypeEnum.ANSWER.equals(item.getType())).collect(Collectors.toList());
Collections.shuffle(answerQuestions);
list.addAll(selectQuestions);
list.addAll(judgeQuestions);
list.addAll(answerQuestions);
return list;
}
private static void disturbAnswerOrder(Question question) {
if (!QuestionTypeEnum.SELECT.equals(question.getType())) {
return;
}
// 只有选择题需要乱序
List<Option> options = question.getOptions();
// 记录乱序前的正确答案
String answerValue = null;
for (int i = 0; i < options.size(); i++) {
Option option = options.get(i);
if (option.getKey().equals(question.getAnswer().getKey())) {
answerValue = option.getValue();
break;
}
}
// 打乱顺序
Collections.shuffle(options);
// 找出乱序后的正确答案
SelectAnswerEnum[] values = SelectAnswerEnum.values();
for (int i = 0; i < options.size(); i++) {
Option option = options.get(i);
// 设置新key
option.setKey(values[i]);
// 如果新key是答案 记录新key
if (option.getValue().equals(answerValue)) {
question.setAnswer(new Answer(option.getKey()));
}
}
}
}

94
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/Student.java

@ -0,0 +1,94 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01;
import cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity.Option;
import cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity.Question;
import cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity.QuestionTypeEnum;
import lombok.Data;
import java.util.List;
import java.util.stream.Collectors;
/**
* 学生
*
* @author niushuai233
* @date 2023/12/28 16:40
* @since 0.0.1
*/
@Data
public class Student {
private String stuNo;
private String name;
private List<Question> questions;
public Student(String stuNo, String name, List<Question> questions) {
this.stuNo = stuNo;
this.name = name;
this.questions = questions;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("======================================================================================").append(System.lineSeparator());
sb.append("学号: ").append(stuNo).append(System.lineSeparator());
sb.append("姓名: ").append(name).append(System.lineSeparator());
sb.append("---------------------------------------------------------------------------------------").append(System.lineSeparator());
sb.append("一、选择题").append(System.lineSeparator());
List<Question> selectQuestions = questions.stream().filter(item -> QuestionTypeEnum.SELECT.equals(item.getType())).collect(Collectors.toList());
for (int i = 0; i < selectQuestions.size(); i++) {
Question question = selectQuestions.get(i);
List<Option> options = question.getOptions();
sb.append((i + 1) + "、\t").append(question.getTitle()).append(System.lineSeparator());
for (Option option : options) {
sb.append("\t").append(option.getKey()).append(": ").append(option.getValue()).append(System.lineSeparator());
}
sb.append("答案: ").append(question.getAnswer().getKey()).append(System.lineSeparator());
}
sb.append(System.lineSeparator());
sb.append("二、判断题").append(System.lineSeparator());
List<Question> judgeQuestions = questions.stream().filter(item -> QuestionTypeEnum.JUDGE.equals(item.getType())).collect(Collectors.toList());
for (int i = 0; i < judgeQuestions.size(); i++) {
Question question = judgeQuestions.get(i);
sb.append((i + 1) + "、\t").append(question.getTitle()).append(" ( )").append(System.lineSeparator());
sb.append("答案: ").append(question.getAnswer().getCorrect()).append(System.lineSeparator());
}
sb.append(System.lineSeparator());
sb.append("二、判断题").append(System.lineSeparator());
List<Question> answerQuestions = questions.stream().filter(item -> QuestionTypeEnum.ANSWER.equals(item.getType())).collect(Collectors.toList());
for (int i = 0; i < answerQuestions.size(); i++) {
Question question = answerQuestions.get(i);
sb.append((i + 1) + "、\t").append(question.getTitle()).append(" ( )").append(System.lineSeparator());
sb.append("答案: ").append(question.getAnswer().getAnswer()).append(System.lineSeparator());
}
sb.append("======================================================================================").append(System.lineSeparator());
sb.append(System.lineSeparator());
return sb.toString();
}
}

61
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/Answer.java

@ -0,0 +1,61 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity;
import lombok.Data;
import java.io.Serializable;
/**
* 答案
*
* @author niushuai233
* @date 2023/12/20 17:06
* @since 0.0.1
*/
@Data
public class Answer implements Serializable {
private static final long serialVersionUID = 7089356236006618280L;
/**
* 问答题正确答案
*/
private String answer;
/**
* 选择题正确答案
*/
private SelectAnswerEnum key;
/**
* 判断题正确答案
*/
private Boolean correct;
public Answer(String answer) {
this.answer = answer;
}
public Answer(SelectAnswerEnum key) {
this.key = key;
}
public Answer(Boolean correct) {
this.correct = correct;
}
}

40
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/Option.java

@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
/**
* 选项
*
* @author niushuai233
* @date 2023/12/22 9:28
* @since 0.0.1
*/
@Data
@AllArgsConstructor
public class Option implements Serializable {
private static final long serialVersionUID = -4732258966292936442L;
private SelectAnswerEnum key;
private String value;
}

44
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/Question.java

@ -0,0 +1,44 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 问题
*
* @author niushuai233
* @date 2023/12/20 17:06
* @since 0.0.1
*/
@Data
public class Question implements Serializable {
private static final long serialVersionUID = -860553158276002337L;
private String title;
private QuestionTypeEnum type;
private List<Option> options;
private Answer answer;
}

28
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/QuestionTypeEnum.java

@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity;
/**
* 问题类型
*
* @author niushuai233
* @date 2023/12/22 9:29
* @since 0.0.1
*/
public enum QuestionTypeEnum {
SELECT, ANSWER, JUDGE
}

28
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/entity/SelectAnswerEnum.java

@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01.entity;
/**
* 选择题答案枚举
*
* @author niushuai233
* @date 2023/12/22 9:34
* @since 0.0.1
*/
public enum SelectAnswerEnum {
A, B, C, D, E, F, G, H
}

24
src/main/java/cc/niushuai/demo/designpattern/creatormodel/prototype/package-info.java

@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 原型设计模式
*
* @author niushuai233
* @date 2023/12/20 17:05
* @since 0.0.1
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype;

43
src/test/java/cc/niushuai/demo/designpattern/creatormodel/prototype/_01/QuestionTest.java

@ -0,0 +1,43 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.prototype._01;
import org.junit.jupiter.api.Test;
/**
* question test
*
* @author niushuai233
* @date 2023/12/29 10:33
* @since 0.0.1
*/
public class QuestionTest {
@Test
public void questionTest() {
Student zhangsan = new Student("STU001", "张三", QuestionService.getQuestions());
Student lisi = new Student("STU001", "李四", QuestionService.getQuestions());
Student wanger = new Student("STU001", "王二", QuestionService.getQuestions());
System.out.println(zhangsan);
System.out.println(lisi);
System.out.println(wanger);
}
}
Loading…
Cancel
Save