0
点赞
收藏
分享

微信扫一扫

AndroidStudio安装Android模拟器AVD及遇到的问题解决

木樨点点 03-07 06:00 阅读 2

在这里插入图片描述

基本介绍

  1. 一个对象应该对其他对象保持最少的了解
  2. 类与类关系越密切,耦合度越大
  3. 迪米特法则(Demeter Principle)又叫最少知道法则,即一个类对自己依赖的类知道的越少越好。也就是说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。对外除了提供的public方法,不对外泄露任何信息
  4. 迪米特法则更简单的定义:只与直接的朋友通信
  5. 直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就可以说这两个对象之间是朋友关系。耦合的方式很多,依赖,关联,组合,聚合等。其中,我们称出现成员变量方法参数方法返回值中的类为直接的朋友。也就是说,陌生的类最好不要以局部变量的形式出现在类的内部

应用实例

  1. 有一个学校,下属有各个学院和总部,现要求打印出学校总部员工ID和学院员工ID
/**
 * Created with IntelliJ IDEA.
 * User: Mingda
 * Time: 2024/2/28 15:22
 * File: Demeter1
 * Description: 迪米特法则
 */
public class Demeter1 {

    public static void main(String[] args) {
        SchoolManager schoolManager = new SchoolManager();
        // 输出学校总部和学院员工信息
        schoolManager.printAllEmployee(new CollegeEmployeeManager());
    }
}

// 学校总部员工类
class Employee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学院员工类
class CollegeEmployee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学院员工管理类
class CollegeEmployeeManager {
    public List<CollegeEmployee> getAllEmployee() {
        // 返回学院的所有员工
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        for (int i = 0; i < 10; i++) {
            CollegeEmployee collegeEmployee = new CollegeEmployee();
            collegeEmployee.setId("学院员工ID:" + i);
            list.add(collegeEmployee);
        }
        return list;
    }
}

// 学校员工管理类
// 分析:
// 直接朋友:Employee、CollegeEmployeeManager
// CollegeEmployee不是直接朋友,违反了迪米特法则(以局部变量的形式出现在SchoolManager类)
class SchoolManager {
    // 返回学校总部的所有员工
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<Employee>();
        for (int i = 0; i < 10; i++) {
            Employee employee = new Employee();
            employee.setId("学校总部员工ID:" + i);
            list.add(employee);
        }
        return list;
    }

    // 输出学校总部和学院员工信息
    void printAllEmployee(CollegeEmployeeManager sub) {
        // 获取到学院员工
        List<CollegeEmployee> list1 = sub.getAllEmployee();
        System.out.println("学院员工信息:" + list1);
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }

        // 获取到学校总部员工
        List<Employee> list2 = this.getAllEmployee();
        System.out.println("学校总部员工信息:" + list2);
        for (Employee e : list2) {
            System.out.println(e.getId());
        }
    }
}

改进:

/**
 * Created with IntelliJ IDEA.
 * User: Mingda
 * Time: 2024/2/28 15:22
 * File: Demeter1
 * Description: 迪米特法则(改进)
 */
public class Demeter1 {

    public static void main(String[] args) {
        SchoolManager schoolManager = new SchoolManager();
        // 输出学校总部和学院员工信息
        schoolManager.printAllEmployee(new CollegeEmployeeManager());
    }
}

// 学校总部员工类
class Employee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学院员工类
class CollegeEmployee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学院员工管理类
class CollegeEmployeeManager {
    public List<CollegeEmployee> getAllEmployee() {
        // 返回学院的所有员工
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        for (int i = 0; i < 10; i++) {
            CollegeEmployee collegeEmployee = new CollegeEmployee();
            collegeEmployee.setId("学院员工ID:" + i);
            list.add(collegeEmployee);
        }
        return list;
    }

    // 输出学院员工信息
    public void printEmployee() {
        List<CollegeEmployee> list1 = getAllEmployee();
        System.out.println("学院员工信息:" + list1);
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }
    }
}

// 学校员工管理类
// 分析:
// 直接朋友:Employee、CollegeEmployeeManager
// CollegeEmployee不是直接朋友,违反了迪米特法则(以局部变量的形式出现在SchoolManager类)
class SchoolManager {
    // 返回学校总部的所有员工
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<Employee>();
        for (int i = 0; i < 10; i++) {
            Employee employee = new Employee();
            employee.setId("学校总部员工ID:" + i);
            list.add(employee);
        }
        return list;
    }

    // 输出学校总部和学院员工信息
    void printAllEmployee(CollegeEmployeeManager sub) {
        /*// 获取到学院员工
        List<CollegeEmployee> list1 = sub.getAllEmployee();
        System.out.println("学院员工信息:" + list1);
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }*/

        // 分析问题:将输出学院员工方法,封装到CollegeEmployeeManager类中
        sub.printEmployee();

        // 获取到学校总部员工
        List<Employee> list2 = this.getAllEmployee();
        System.out.println("学校总部员工信息:" + list2);
        for (Employee e : list2) {
            System.out.println(e.getId());
        }
    }
}

注意事项和细节

  1. 迪米特法则的核心是降低类之间的耦合
  2. 注意:由于每个类都减少了不必要的依赖,因此迪米特法则只是要求降低类间(对象间)的耦合关系,并不是要求完全没有依赖

github笔记

举报

相关推荐

0 条评论