您当前的位置: 首页 > 

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

设计模式 迪米特原则

java持续实践 发布时间:2022-03-12 16:37:00 ,浏览量:2

文章目录
      • 迪米特原则
      • 迪米特原则 实战
        • 迪米特原则改造.
      • 迪米特原则与单一职责的区别

迪米特原则

理解在实际项目中, 设计原则用于做什么, 比一定非得符合某个规范.

迪米特原则 : 最少知道 , 减少依赖. 降低类之间的耦合. 高内聚, 低耦合.

迪米特原则 实战

学生 老师 校长类 原有的写法是直接在校长类Principal 中计算学生相关的数据. 学生类

package com.thc.design;

/**
 * 类名称:Student
 * 创建时间:2022/3/11 6:51
 */
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;
    }
}

教师类

public class Teacher {

    private String name;
    private String clazz;
    private static List studentList;

    public Teacher() {
    }

    public Teacher(String name, String clazz) {
        this.name = name;
        this.clazz = clazz;
    }

    static {
        studentList = new ArrayList();
        studentList.add(new Student("花花", 10, 598));
        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 getStudentList() {
        return studentList;
    }

    public String getName() {
        return name;
    }

    public String getClazz() {
        return clazz;
    }
}

校长类. 在校长类中, 直接计算了学生相关的数据 .

public class Principal {

    private Teacher teacher = new Teacher("丽华","3年1班");

    public Map queryClazzInfo(String clazzId) {
        // 获取学生总人数 总分 平均分
        int stuCount = clazzStudentCount();
        double totalScore = clazzTotalScore();
        double averageScore = clazzAverageScore();

        // 组装返回对象
        Map mapObj  = new HashMap();
        mapObj.put("班级", teacher.getClazz());
        mapObj.put("老师", teacher.getName());
        mapObj.put("学生人数", stuCount);
        mapObj.put("班级总分数", totalScore);
        mapObj.put("班级平均分", averageScore);
        return mapObj;
    }

    // 获取学生总分
    public double clazzTotalScore() {
        double totalScore = 0;
        for (Student stu : Teacher.getStudentList()) {
            totalScore += stu.getGrade();
        }
        return totalScore;
    }

    /**
     * 获取平均分
     * @return
     */
    private double clazzAverageScore() {
        double totalScore = 0;
        for (Student stu : Teacher.getStudentList()) {
            totalScore += stu.getGrade();
        }
        return totalScore / Teacher.getStudentList().size();
    }

    /**
     * 获取班级人数
     * @return
     */
    public int clazzStudentCount() {
        return Teacher.getStudentList().size();
    }
}

测试类

public class ApiTest {
    private Logger logger = LoggerFactory.getLogger(ApiTest.class);

    @Test
    public void test_Principal() {
        Principal principal = new Principal();
        Map map = principal.queryClazzInfo("3年1班");
        logger.info("查询结果:{}", JSON.toJSONString(map));
    }
}

打印结果如下

查询结果:{“学生人数”:5,“班级平均分”:512.0,“班级”:“3年1班”,“老师”:“丽华”,“班级总分数”:2560.0}

迪米特原则改造.

校长不该直接关注学生, 解除与学生之间的耦合, 校长通过老师来获取学生的信息. 学生类不变, 教师类改造如下 , 在教师类中计算学生相关数据

public class Teacher {

    private String name;
    private String clazz;
    private static List studentList;

    public Teacher() {
    }

    public Teacher(String name, String clazz) {
        this.name = name;
        this.clazz = clazz;
    }

    static {
        studentList = new ArrayList();
        studentList.add(new Student("花花", 10, 598));
        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 clazzTotalScore() {
        double totalScore = 0;
        for (Student stu : Teacher.getStudentList()) {
            totalScore += stu.getGrade();
        }
        return totalScore;
    }

    /**
     * 获取平均分
     * @return
     */
    public double clazzAverageScore() {
        double totalScore = 0;
        for (Student stu : Teacher.getStudentList()) {
            totalScore += stu.getGrade();
        }
        return totalScore / Teacher.getStudentList().size();
    }

    /**
     * 获取班级人数
     * @return
     */
    public int clazzStudentCount() {
        return Teacher.getStudentList().size();
    }

    public static List getStudentList() {
        return studentList;
    }

    public String getName() {
        return name;
    }

    public String getClazz() {
        return clazz;
    }
}

校长类如下 , 通过教师获取学生数据. 那么校长解除了学生与教师之间的耦合

public class Principal {
    private Teacher teacher = new Teacher("丽华","3年1班");

    public Map queryClazzInfo(String clazzId) {
        // 获取学生总人数 总分 平均分
        int stuCount = teacher.clazzStudentCount();
        double totalScore = teacher.clazzTotalScore();
        double averageScore = teacher.clazzAverageScore();

        // 组装返回对象
        Map mapObj  = new HashMap();
        mapObj.put("班级", teacher.getClazz());
        mapObj.put("老师", teacher.getName());
        mapObj.put("学生人数", stuCount);
        mapObj.put("班级总分数", totalScore);
        mapObj.put("班级平均分", averageScore);
        return mapObj;
    }
}

测试类

public class ApiTest {
    private Logger logger = LoggerFactory.getLogger(ApiTest.class);

    @Test
    public void test_Principal() {
        Principal principal = new Principal();
        Map map = principal.queryClazzInfo("3年1班");
        logger.info("查询结果:{}", JSON.toJSONString(map));
    }

}

打印结果如下

查询结果:{“学生人数”:5,“班级平均分”:512.0,“班级”:“3年1班”,“老师”:“丽华”,“班级总分数”:2560.0}

与一开始的打印结果相同.

迪米特原则与单一职责的区别
  1. 单一职责讲的是一个类,只有一个要处理的事情
  2. 迪米特法则讲的是两个类之间,不要过多的耦合. 例如此例子中, 就是校长类与学生类不要进行耦合.
关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0416s