设计模式原则之:迪米特法则
不符合迪米特法则的案例
import java.util.ArrayList; import java.util.List; /** * @description: 不符合迪米特法则的案例 * @author: abel.he * @date: 2023-08-06 **/ public class Test { public static void main(String[] args) { CollegeEmployeeManager manager = new CollegeEmployeeManager(); EmployeeManager employeeManager = new EmployeeManager(); employeeManager.printAll(manager); } } /** * 学校总部员工类 */ class Employee { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } } /** * 学院档案的员工类 */ class CollegeEmployee { private String collegeEmployeeId; public String getCollegeEmployeeId() { return collegeEmployeeId; } public void setCollegeEmployeeId(String collegeEmployeeId) { this.collegeEmployeeId = collegeEmployeeId; } } /** * 员工档案管理类 */ class CollegeEmployeeManager { public List<CollegeEmployee> getAllList() { List<CollegeEmployee> result = new ArrayList<>(); for (int i = 0; i < 5; i++) { CollegeEmployee employee = new CollegeEmployee(); employee.setCollegeEmployeeId("我是学院档案员工:" + i); result.add(employee); } return result; } } /** * 员工管理类 * Employee 直接的朋友(返回值) * CollegeEmployee 不是直接朋友 陌生类 违反了迪米特法则 * */ class EmployeeManager { public List<Employee> getEmployee() { List<Employee> result = new ArrayList<>(); for (int i = 0; i < 10; i++) { Employee employee = new Employee(); employee.setId("我是学校总部员工 " + i); result.add(employee); } return result; } /** * 输出所有数据 学校总部员工+学院档案员工信息 * @param collegeEmployeeManager */ public void printAll(CollegeEmployeeManager collegeEmployeeManager) { List<CollegeEmployee> collegeEmployeeManagerAllList = collegeEmployeeManager.getAllList(); for (CollegeEmployee employee : collegeEmployeeManagerAllList) { System.out.println(employee.getCollegeEmployeeId()); } List<Employee> employeeList = this.getEmployee(); for (Employee employee : employeeList) { System.out.println(employee.getId()); } } }
改成迪米特法则实现
import java.util.ArrayList; import java.util.List; /** * @description: 迪米特法则的案例 * @author: abel.he * @date: 2023-08-06 **/ public class Test { public static void main(String[] args) { CollegeEmployeeManager manager = new CollegeEmployeeManager(); EmployeeManager employeeManager = new EmployeeManager(); employeeManager.printAll(manager); } } /** * 学校总部员工类 */ class Employee { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } } /** * 学院档案的员工类 */ class CollegeEmployee { private String collegeEmployeeId; public String getCollegeEmployeeId() { return collegeEmployeeId; } public void setCollegeEmployeeId(String collegeEmployeeId) { this.collegeEmployeeId = collegeEmployeeId; } } /** * 员工档案管理类 */ class CollegeEmployeeManager { public List<CollegeEmployee> getAllList() { List<CollegeEmployee> result = new ArrayList<>(); for (int i = 0; i < 5; i++) { CollegeEmployee employee = new CollegeEmployee(); employee.setCollegeEmployeeId("我是学院档案员工:" + i); result.add(employee); } return result; } public void printCollegeEmployeeManager() { List<CollegeEmployee> collegeEmployeeManagerAllList = getAllList(); for (CollegeEmployee employee : collegeEmployeeManagerAllList) { System.out.println(employee.getCollegeEmployeeId()); } } } /** * 员工管理类 * Employee 直接的朋友(返回值) * */ class EmployeeManager { public List<Employee> getEmployee() { List<Employee> result = new ArrayList<>(); for (int i = 0; i < 10; i++) { Employee employee = new Employee(); employee.setId("我是学校总部员工 " + i); result.add(employee); } return result; } /** * 输出所有数据 学校总部员工+学院档案员工信息 * @param collegeEmployeeManager */ public void printAll(CollegeEmployeeManager collegeEmployeeManager) { // 将输出的方法封装到CollegeEmployeeManager中 collegeEmployeeManager.printCollegeEmployeeManager(); List<Employee> employeeList = this.getEmployee(); for (Employee employee : employeeList) { System.out.println(employee.getId()); } } }