Least Knowledge Principle,LKP
。
design-1.4-0
|——src
|——main
|--java
|--com.lino.design
|--Principal.java
|--Student.java
|--Teacher.java
|——test
|--java
|--com.lino.design.test
|--ApiTest.java
Student.java
package com.lino.design;
/**
* @description: 学生
*/
public class Student {
/**
* 姓名
*/
private String name;
/**
* 考试排名
*/
private int rank;
/**
* 考试分数
*/
private double grade;
public Student() {
}
public Student(String name, int rank, double grade) {
this.name = name;
this.rank = rank;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
Teacher.java
package com.lino.design;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 老师
*/
public class Teacher {
/**
* 老师名称
*/
private String name;
/**
* 班级
*/
private String clazz;
/**
* 学生列表
*/
private static List<Student> studentList;
public Teacher() {
}
public Teacher(String name, String clazz) {
this.name = name;
this.clazz = clazz;
}
static {
studentList = new ArrayList<>();
studentList.add(new Student("花花", 10, 589));
studentList.add(new Student("豆豆", 54, 356));
studentList.add(new Student("秋雅", 23, 439));
studentList.add(new Student("皮皮", 2, 665));
studentList.add(new Student("蛋蛋", 19, 502));
}
public static List<Student> getStudentList() {
return studentList;
}
public String getName() {
return name;
}
public String getClazz() {
return clazz;
}
}
Principal.java
package com.lino.design;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 校长
*/
public class Principal {
private Teacher teacher = new Teacher("丽华", "3年1班");
public Map<String, Object> queryClazzInfo(String clazzId) {
// 获取班级信息,学生总人数、总分、平均分
int stuCount = clazzStudentCount();
double totalScore = clazzTotalScore();
double averageScore = clazzAverageScore();
// 组装对象,实际业务开发会有对应的类
Map<String, Object> mapObj = new HashMap<>(16);
mapObj.put("班级", teacher.getClazz());
mapObj.put("老师", teacher.getName());
mapObj.put("学生人数", stuCount);
mapObj.put("班级总分数", totalScore);
mapObj.put("班级平均分", averageScore);
return mapObj;
}
/**
* 平均分
*/
private double clazzAverageScore() {
double totalScore = 0;
for (Student stu : Teacher.getStudentList()) {
totalScore += stu.getGrade();
}
return totalScore / Teacher.getStudentList().size();
}
/**
* 总分
*/
private double clazzTotalScore() {
double totalScore = 0;
for (Student stu : Teacher.getStudentList()) {
totalScore += stu.getGrade();
}
return totalScore;
}
/**
* 总人数
*/
private int clazzStudentCount() {
return Teacher.getStudentList().size();
}
}
ApiTest.java
@Test
public void test_Principal() {
Principal principal = new Principal();
Map<String, Object> map = principal.queryClazzInfo("3年1班");
logger.info("查询结果:{}", JSON.toJSONString(map));
}
测试结果
13:49:37.477 [main] INFO com.lino.design.test.ApiTest - 查询结果:{"学生人数":5,"班级平均分":510.2,"班级":"3年1班","老师":"丽华","班级总分数":2551.0}
design-1.4-1
|——src
|——main
|--java
|--com.lino.design
|--Principal.java
|--Student.java
|--Teacher.java
|——test
|--java
|--com.lino.design.test
|--ApiTest.java
Student.java
package com.lino.design;
/**
* @description: 学生
*/
public class Student {
/**
* 姓名
*/
private String name;
/**
* 考试排名
*/
private int rank;
/**
* 考试分数
*/
private double grade;
public Student() {
}
public Student(String name, int rank, double grade) {
this.name = name;
this.rank = rank;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
Teacher.java
package com.lino.design;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 老师
*/
public class Teacher {
/**
* 老师名称
*/
private String name;
/**
* 班级
*/
private String clazz;
/**
* 学生列表
*/
private static List<Student> studentList;
public Teacher() {
}
public Teacher(String name, String clazz) {
this.name = name;
this.clazz = clazz;
}
static {
studentList = new ArrayList<>();
studentList.add(new Student("花花", 10, 589));
studentList.add(new Student("豆豆", 54, 356));
studentList.add(new Student("秋雅", 23, 439));
studentList.add(new Student("皮皮", 2, 665));
studentList.add(new Student("蛋蛋", 19, 502));
}
/**
* 平均分
*/
public double clazzAverageScore() {
double totalScore = 0;
for (Student stu : studentList) {
totalScore += stu.getGrade();
}
return totalScore / studentList.size();
}
/**
* 总分
*/
public double clazzTotalScore() {
double totalScore = 0;
for (Student stu : studentList) {
totalScore += stu.getGrade();
}
return totalScore;
}
/**
* 总人数
*/
public int clazzStudentCount() {
return studentList.size();
}
public static List<Student> getStudentList() {
return studentList;
}
public String getName() {
return name;
}
public String getClazz() {
return clazz;
}
}
Principal.java
package com.lino.design;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 校长
*/
public class Principal {
private Teacher teacher = new Teacher("丽华", "3年1班");
public Map<String, Object> queryClazzInfo(String clazzId) {
// 获取班级信息,学生总人数、总分、平均分
int stuCount = teacher.clazzStudentCount();
double totalScore = teacher.clazzTotalScore();
double averageScore = teacher.clazzAverageScore();
// 组装对象,实际业务开发会有对应的类
Map<String, Object> mapObj = new HashMap<>(16);
mapObj.put("班级", teacher.getClazz());
mapObj.put("老师", teacher.getName());
mapObj.put("学生人数", stuCount);
mapObj.put("班级总分数", totalScore);
mapObj.put("班级平均分", averageScore);
return mapObj;
}
}
ApiTest.java
@Test
public void test_Principal() {
Principal principal = new Principal();
Map<String, Object> map = principal.queryClazzInfo("3年1班");
logger.info("查询结果:{}", JSON.toJSONString(map));
}
测试结果
13:49:37.477 [main] INFO com.lino.design.test.ApiTest - 查询结果:{"学生人数":5,"班级平均分":510.2,"班级":"3年1班","老师":"丽华","班级总分数":2551.0}