-
合成/聚合复用原则:尽量使用合成/聚合 的方式,而不是使用继承 合成/聚合示例:
* 合成(Composition,也称组合)和聚合(Aggregation), 都是关联的特殊种类。 聚合表示一种弱的“拥有”关系,体现的是A对象可以包含
* B对象,但B对象不是A对象的一部分; 组合(合成)是一种强的“拥有”关系,体现了严格的部分 与整体的关系,部分和整体的生命周期是一样的。
* 接口隔离原则基本介绍: 不应该依赖不需要的接口,即一个类对另一个类的依赖应该 建立在最小的接口上。* 场景: 1.类B实现接口Interface1 , * 类A通过接口Interface1依赖(使 用)类B,但是只会用到1,2,3方法 2. 类D实现接口Interface1 , * 类C通过接口Interface1依赖(使 用)类D,但是只会用到1,4,5方法
public class Main {
public static void main(String[] args) {
A a = new A();
C c = new C();
// 类A 通过接口 依赖类B
a.depend1(new B());
a.depend2(new B());
a.depend3(new B());
// 类C 通过接口 依赖类D
c.depend1(new D());
c.depend4(new D());
c.depend5(new D());
}
}
//接口
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
//类B实现接口Interface1
class B implements Interface1 {
public void operation1() {
System.out.println("B实现 了operation1");
}
public void operation2() {
System.out.println("B实现 了operation2");
}
public void operation3() {
System.out.println("B实现 了operation3");
}
public void operation4() {
System.out.println("B实现 了operation4");
}
public void operation5() {
System.out.println("B实现 了operation5");
}
}
//类D实现接口Interface1
class D implements Interface1 {
public void operation1() {
System.out.println("D实现 了operation1");
}
public void operation2() {
System.out.println("D实现 了operation2");
}
public void operation3() {
System.out.println("D实现 了operation3");
}
public void operation4() {
System.out.println("D实现 了operation4");
}
public void operation5() {
System.out.println("D实现 了operation5");
}
}
//类A通过接口Interface1依赖(使用) 类B,但是只会用到1,2,3方法
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
//类C通过接口Interface1依赖(使用) 类D,但是只会用到1,4,5方法
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
解决:
接口Interface1拆分为独立的几个接口,类A和类C分别与它 们需要的接口建立依赖关系。也就是采用接口隔离原则。
接口Interface1 中出现的方法,根据实际情况拆分为三个接 口:
public class Main {
public static void main(String[] args) {
A a = new A();
C c = new C();
// 类A 通过接口 依赖类B
a.depend1(new B());
a.depend2(new B());
a.depend3(new B());
// 类C通过接口 依赖类D
c.depend1(new D());
c.depend4(new D());
c.depend5(new D());
}
}
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
interface Interface3 {
void operation4();
void operation5();
}
//类B 实现接口Interface1 ,Interface2的所有方法
class B implements Interface1, Interface2 {
public void operation1() {
System.out.println("B 实现 operation1");
}
public void operation2() {
System.out.println("B 实现 operation2");
}
public void operation3() {
System.out.println("B 实现 operation3");
}
}
//类A 通过接口 Interface1,Interface2 依赖 (使用)类B 只会 用到方法1,2,3
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
//类D实现接口Interface1,Interface3 的所有方法
class D implements Interface1, Interface3 {
public void operation1() {
System.out.println("D实现 operation1");
}
public void operation4() {
System.out.println("D实现 operation4");
}
public void operation5() {
System.out.println("D实现 operation5");
}
}
//类C 通过接口 Interface1,Interface3 依赖 (使用)类D 只会 用到方法1,4,5
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
- 迪米特法则(LoD) 1)一个对象应该对其他对象保持最少的了解 2)类与类关系越密切,耦合度越大
- 3)迪米特法则(DemeterPrinciple)又叫最少知道原则,即一 个类对自己依赖的类知道的越少越好。也就是说,对于被
- 依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。 对外除了提供的public方法,不泄露任何信息。 4)迪米特法则还有个更简单的定义:只与直接的朋友通信
- 5)直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之 间有耦合关系,我们就说这两个对象之间是朋友关系。耦合的方式很
- 多,依赖,关联,组合,聚合等。其中,称出现成员变量,方法参数, 方法返回值中的类为直接的朋友,而出现在局部变量中的类不是直接
- 的朋友。也就是说,陌生的类最好不要以局部变量的形式出现在类的 内部。
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建了一个SchoolManager对象
SchoolManager schoolManager = new SchoolManager();
// 输出学院的员工id 和 学校总部的员工信息
schoolManager.printAllEmployee(new CollegeManager());
}
}
//学校总部员工类
class Employee {
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return 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();
for (int i = 0; i < 10; i++) {
CollegeEmployee emp = new CollegeEmployee();
emp.setId("学院员工 id = " + i);
list.add(emp);
}
return list;
}
}
//学校管理类
//分析SchoolMangager 类的直接朋友有哪些Employee,CollegeManager
//CollegeEmployee 不是直接朋友而是一个陌生类,这样违背了迪米特法则
class SchoolManager {
public List<Employee> getAllEmployee() {
List<Employee> list = new ArrayList();
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 employee : list1) {
System.out.println(employee.getId());
}
// 获取到学校总部的员工
List<Employee> list2 = this.getAllEmployee();
System.out.println("-------学校总部员工---------");
for (Employee employee : list2) {
System.out.println(employee.getId());
}
}
}
//改进方法:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建了一个SchoolManager对象
SchoolManager schoolManager = new SchoolManager();
// 输出学院的员工id 和 学校总部的员工信息
schoolManager.printAllEmployee(new CollegeManager());
}
}
//学校总部员工类
class Employee {
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return 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();
for (int i = 0; i < 10; i++) {
CollegeEmployee emp = new CollegeEmployee();
emp.setId("学院员工 id = " + i);
list.add(emp);
}
return list;
}
void printEmployee() {
List<CollegeEmployee> list1 = getAllEmployee();
System.out.println("-------学院员工---------");
for (CollegeEmployee employee : list1) {
System.out.println(employee.getId());
}
}
}
//学校管理类
//分析SchoolMangager 类的直接朋友有哪些Employee,CollegeManager
//CollegeEmployee 不是直接朋友而是一个陌生类,这样违背了迪米特法则
class SchoolManager {
public List<Employee> getAllEmployee() {
List<Employee> list = new ArrayList();
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 employee : list2) {
System.out.println(employee.getId());
}
}
}
- 迪米特法则(Law of Demeter,简写LoD )又叫做最少知识 原则(Least Knowledge Principle或简写为LKP)
- 如果两个类不必彼此直接通信,那么这两个类就不应当发 生直接的相互作用。如果其中一个类需要调用另一个类的 某一个方法的话,可以通过第三者转发这个调用。
- 也就是说,一个对象应当对其它对象有尽可能少的了解。 迪米特法则的目的在于降低类与类之间的耦合。由于每 个类尽量减少对其他类的依赖,因此,很容易使得系统
- 的功能模块功能独立,使得相互间存在尽可能少的依赖 关系。信息的隐藏促进了软件的复用。 但是注意: 由于每个类都减少了不必要的依赖,因此迪米特法则
- 只是要求降低类间(对象间)耦合关系, 并不是要求完 全没有依赖关系。