赞
踩
有一个学校,有各个学院和总部,现要求打印出学校总部员工ID和学院员工的ID
SchoolManager类的直接朋友类有Employee、CollegeManager
CollegeEmployee不是直接朋友,而是一个陌生类,这样违背了迪米特法则
package com.weirdo.principle.demeter; import java.util.ArrayList; import java.util.List; public class Demeter1 { public static void main(String[] args) { //创建一个SchoolManager对象 SchoolManager schoolManager = new SchoolManager(); //输出学院的员工ID和学校总部的员工信息 schoolManager.PRINTaLLEmployee(new CollegeManager()); } } //学校总部员工类 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 CollegeManager{ //返回学院的所有员工 public List<CollegeEmployee> getAllEmployee(){ List<CollegeEmployee> list = new ArrayList<CollegeEmployee>(); //增加了10个员工到list for (int i = 0; i < 10; i++) { CollegeEmployee emp = new CollegeEmployee(); emp.setId("学院员工ID="+i); list.add(emp); } return list; } } //学校管理类 class SchoolManager{ //返回学校总部的员工 public List<Employee> getAllEmployee(){ List<Employee>list = new ArrayList<Employee>(); for (int i = 0; i < 5; i++) { Employee emp = new Employee(); emp.setId("学校总部员工ID="+i); list.add(emp); } return list; } //该方法完成输出学校总部和学院员工信息(ID) void PRINTaLLEmployee(CollegeManager sub){ //分析问题 //1、这两的CollegeEmployee不是SchoolManager的直接朋友 //2、CollegeEmployee是以局部变量方式出现在SchoolManager //3、违反了迪米特法则 //获取学院员工 List<CollegeEmployee>list1 = sub.getAllEmployee(); System.out.println("===========学院员工=============="); for (CollegeEmployee e :list1) { System.out.println(e.getId()); } //获取学院总部员工 List<Employee>list2 = this.getAllEmployee(); System.out.println("===========学院总部员工=============="); for (Employee e :list2) { System.out.println(e.getId()); } } }
package com.weirdo.principle.demeter.improve; import java.util.ArrayList; import java.util.List; public class Demeter1 { public static void main(String[] args) { SchoolManager schoolManager = new SchoolManager(); schoolManager.printAllEmployee(new CollegeManager()); } } //学校总部员工类 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 CollegeManager{ //返回学院的所有员工 public List<CollegeEmployee> getAllEmployee(){ List<CollegeEmployee> list = new ArrayList<CollegeEmployee>(); //增加了10个员工到list for (int i = 0; i < 10; i++) { CollegeEmployee emp = new CollegeEmployee(); emp.setId("学院员工ID="+i); list.add(emp); } return list; } //输出学院员工的信息 public void printEmployee(){ //获取到学院员工 List<CollegeEmployee>list1 = getAllEmployee(); System.out.println("===========学院员工=============="); for (CollegeEmployee e :list1) { System.out.println(e.getId()); } } } //学校管理类 class SchoolManager{ //返回学校总部的员工 public List<Employee> getAllEmployee(){ List<Employee>list = new ArrayList<Employee>(); for (int i = 0; i < 5; i++) { Employee emp = new Employee(); emp.setId("学校总部员工ID="+i); list.add(emp); } return list; } //该方法完成输出学校总部和学院员工信息(ID) void printAllEmployee(CollegeManager sub){ //分析问题 //1、将输出学院员工方法,封装到CollegeManager sub.printEmployee(); //获取学院总部员工 List<Employee>list2 = this.getAllEmployee(); System.out.println("===========学院总部员工=============="); for (Employee e :list2) { System.out.println(e.getId()); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。