定义:是指一个对象类对于其他对象类来说,知道的越少越好。也就是说两个类之间不要有过多的耦合关系,保持最少关联性。
经典语录:只和朋友通信,不合陌生人说话。
也就是说有内在关联的类要内聚,没有直接关系的类要低耦合。
原始方法代码实现:
public class Principal {
private Teacher teacher = new Teacher("梨花","三年级一班");
//查询班级信息、总分、学生人数、平均分
public Map<String,Object> queryClassInfo(){
//获取班级信息:学生人数、总分、平均分
//学生人数
int stuCount = clazzStudentCount();
//班级总成绩
double totalScore = clazzTotalScore();
//班级平均成绩
double averageScore = clazzAgerageScore();
//组装对象,实际开发过程中会有对应的类
Map<String,Object> mapObj = new HashMap<>();
mapObj.put("班级", teacher.getClazz());
mapObj.put("老师姓名", teacher.getName());
mapObj.put("学生人数", stuCount);
mapObj.put("总分", totalScore);
mapObj.put("平均分", averageScore);
return mapObj;
}
private double clazzAgerageScore() {
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();
}
/**
* 以上实现方法虽然简单可用,但是违背了迪米特法则,因为校长需要了解到每个学生的情况,
* 如果所有班级都让校长统计的话,代码就会显得非常的臃肿,不利于维护和扩展
*/
}
public class Student {
private String name; //学生姓名
private int rank; //考试排名
private double grade; // 总分
public Student(String name, int rank, double grade) {
this.name = name;
this.rank = rank;
this.grade = grade;
}
public String getName() {
return name;
}
public Student setName(String name) {
this.name = name;
return this;
}
public int getRank() {
return rank;
}
public Student setRank(int rank) {
this.rank = rank;
return this;
}
public double getGrade() {
return grade;
}
public Student setGrade(double grade) {
this.grade = grade;
return this;
}
}
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,580));
studentList.add(new Student("豆豆",54 , 356));
studentList.add(new Student("秋雅",23 , 500));
studentList.add(new Student("屁屁",5 , 600));
studentList.add(new Student("吉吉",2 , 655));
}
public String getName() {
return name;
}
public Teacher setName(String name) {
this.name = name;
return this;
}
public String getClazz() {
return clazz;
}
public Teacher setClazz(String clazz) {
this.clazz = clazz;
return this;
}
public static List<Student> getStudentList() {
return studentList;
}
public static void setStudentList(List<Student> studentList) {
Teacher.studentList = studentList;
}
}
迪米特法则原理代码实现:
public class Principal {
private Teacher teacher = new Teacher("梨花","三年级一班");
//查询班级信息、总分、学生人数、平均分
public Map<String,Object> queryClassInfo(){
//获取班级信息:学生人数、总分、平均分
//学生人数
int stuCount = teacher.clazzStudentCount();
//班级总成绩
double totalScore = teacher.clazzTotalScore();
//班级平均成绩
double averageScore = teacher.clazzAgerageScore();
//组装对象,实际开发过程中会有对应的类
Map<String,Object> mapObj = new HashMap<>();
mapObj.put("班级", teacher.getClazz());
mapObj.put("老师姓名", teacher.getName());
mapObj.put("学生人数", stuCount);
mapObj.put("总分", totalScore);
mapObj.put("平均分", averageScore);
return mapObj;
}
/**
* 以上实现方式采用迪米特原则,校长直接调用老师类的接口,并获取相应的信息,整个逻辑功能非常清晰
*/
}
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,580));
studentList.add(new Student("豆豆",54 , 356));
studentList.add(new Student("秋雅",23 , 500));
studentList.add(new Student("屁屁",5 , 600));
studentList.add(new Student("吉吉",2 , 655));
}
//平均分
public double clazzAgerageScore() {
double totalScore = 0;
for (Student stu : com.yangtingjia.design_ruler.dimite.origin.Teacher.getStudentList()) {
totalScore += stu.getGrade();
}
return totalScore / com.yangtingjia.design_ruler.dimite.origin.Teacher.getStudentList().size();
}
//总分
public double clazzTotalScore() {
double totalScore = 0;
for (Student stu : com.yangtingjia.design_ruler.dimite.origin.Teacher.getStudentList()) {
totalScore += stu.getGrade();
}
return totalScore;
}
//学生人数
public int clazzStudentCount() {
return com.yangtingjia.design_ruler.dimite.origin.Teacher.getStudentList().size();
}
public String getName() {
return name;
}
public Teacher setName(String name) {
this.name = name;
return this;
}
public String getClazz() {
return clazz;
}
public Teacher setClazz(String clazz) {
this.clazz = clazz;
return this;
}
public static List<Student> getStudentList() {
return studentList;
}
public static void setStudentList(List<Student> studentList) {
Teacher.studentList = studentList;
}
}