2、接口隔离
- 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应建立在最小的接口上
案例Ⅰ
- 违反接口隔离原则
- 类A通过接口Inteface1依赖(使用)C类,但只用到1,2,3方法
- 类B通过接口Inteface1依赖(使用)D类,但只用到1,4,5方法
public class Segregation {
public static void main(String[] args) {
A a = new A();
a.depend1(new C());
a.depend2(new C());
B b = new B();
b.depend1(new D());
b.depend4(new D());
}
}
// 接口1
interface interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
// 类C实现接口1
class C implements Interface1 {
public void operation1() {
System.out.println("C 实现了 方法1");
}
public void operation2() {
System.out.println("C 实现了 方法2");
}
public void operation3() {
System.out.println("C 实现了 方法3");
}
public void operation4() {
System.out.println("C 实现了 方法4");
}
public void operation5() {
System.out.println("C 实现了 方法5");
}
}
// 类D实现接口1
class D implements Interface1 {
public void operation1() {
System.out.println("D 实现了 方法1");
}
public void operation2() {
System.out.println("D 实现了 方法2");
}
public void operation3() {
System.out.println("D 实现了 方法3");
}
public void operation4() {
System.out.println("D 实现了 方法4");
}
public void operation5() {
System.out.println("D 实现了 方法5");
}
}
// 类A通过Interface1依赖类C的方法1、方法2、方法3
class A {
public void depend1(Interface1 i1) {
i1.operation1();
}
public void depend2(Interface1 i1) {
i1.operation2();
}
public void depend3(Interface1 i1) {
i1.operation3();
}
}
// 类B通过Interface1依赖类D的方法1、方法4、方法5
class B {
public void depend1(Interface1 i1) {
i1.operation1();
}
public void depend4(Interface1 i1) {
i1.operation4();
}
public void depend5(Interface1 i1) {
i1.operation5();
}
}
案例Ⅱ
- 符合接口隔离原则,依赖最小接口
public class Segregation {
public static void main(String[] args) {
A a = new A();
a.depend1(new C());
a.depend2(new C());
B b = new B();
b.depend1(new D());
b.depend4(new D());
}
}
// 接口1
interface interface1 {
void operation1();
}
// 接口2
interface interface2 {
void operation2();
void operation3();
}
// 接口3
interface interface3 {
void operation4();
void operation5();
}
// 类C实现接口1和接口2
class C implements Interface1,interface2 {
public void operation1() {
System.out.println("C 实现了 方法1");
}
public void operation2() {
System.out.println("C 实现了 方法2");
}
public void operation3() {
System.out.println("C 实现了 方法3");
}
}
// 类D实现接口1和接口3
class D implements Interface1,interface3 {
public void operation1() {
System.out.println("D 实现了 方法1");
}
public void operation4() {
System.out.println("D 实现了 方法4");
}
public void operation5() {
System.out.println("D 实现了 方法5");
}
}
// 类A通过Interface1和接口2依赖类C的方法1、方法2、方法3
class A {
public void depend1(Interface1 i1) {
i1.operation1();
}
public void depend2(Interface2 i2) {
i2.operation2();
}
public void depend3(Interface2 i2) {
i2.operation3();
}
}
// 类B通过Interface1和接口3依赖类D的方法1、方法4、方法5
class B {
public void depend1(Interface1 i1) {
i1.operation1();
}
public void depend4(Interface3 i3) {
i3.operation4();
}
public void depend5(Interface3 i3) {
i3.operation5();
}
}
3、依赖倒转
- 高层模块不应该依赖低层模块,二者都应该依赖其抽象
- 抽象不应该依赖细节(java种的实现类),细节应该依赖抽象
- 面向接口编程
- 相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构稳定的多
- 使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,将展现细节的任务交给实现类完成
错误示例
- 直接依赖具体实现
/**
* 如果需要获取消息的对象变成微信、短信等其他方式,则不仅要新增具体的类,同时Person也要增加相应的信息接
* 收方法。
*/
class Person {
public void receive(Email email) {
System.out.println(email.getInfo());
}
}
class Email {
public String getInfo() {
return "电子邮件信息";
}
}
案例Ⅰ
- 通过接口传递实现依赖
- 低层模块尽量都要有抽象类或者接口,或者两者都有。这样程序稳定性更好
// 开关接口
interface IOpenAndClose {
void open(ITV tv);
}
// 电视 - 遥控的电视、手动控制的电视
interface ITV {
void play();
}
class OpenAndClose implements IOpenAndClose {
// 不关心具体电视怎么开关,传进来什么具体电视调用其具体实现
public void open(ITV tv) {
tv.play();
}
}
案例Ⅱ
- 通过构造方法依实现赖传递
- 变量的声明类型尽量是抽象类或者接口,这样变量引用和实际对应间,就存在一个缓冲层,利于程序扩展和优化
// 开关接口
interface IOpenAndClose {
void open();
}
// 电视 - 遥控的电视、手动控制的电视
interface ITV {
void play();
}
class OpenAndClose implements IOpenAndClose {
private ITV tv;
public OpenAndClose(ITV tv) {
this.tv = tv;
}
// 通过初始化成员变量的方式
public void open() {
this.tv.play();
}
}
案例Ⅲ
- 通过setter方法实现依赖传递
// 开关接口
interface IOpenAndClose {
void open();
void setTv(ITV tv);
}
// 电视 - 遥控的电视、手动控制的电视
interface ITV {
void play();
}
class OpenAndClose implements IOpenAndClose {
private ITV tv;
public void setTv(ITV tv) {
this.tv = tv;
}
// 不关心具体电视怎么开关,传进来具体电视调用其具体开关实现
public void open() {
this.tv.play();
}
}
4、里氏替换
- 如果对每个类型为T1的对象O1,都有类型为T2的对象O2,使得以T1定义的所有程序P在所有的对象O1都代换成O2时,程序P的行为没有发生变化,那么类型T2是类型T1的子类型。
- 所有引用基类的地方必须能透明地使用其子类的对象
- 使用继承时,遵循里氏替换原则,在子类中尽量不要重写父类的方法
- 里氏替换原则告诉我们,继承实际上让两个类耦合性增强了,在适当的情况下,可以通过聚合、组合、依赖来解决耦合问题
案例Ⅰ
- 原来运行正常的求差功能发生了错误,原因就是类B无意中重写了父类的方法,造成原有功能的出错。在实际编码中,我们常常会通过重写父类的方法完成新的功能,这样写起来虽然简单,但整个继承体系的复用性会比较差。特别是运行多态比较频繁的时候
public class Liskov {
public static void main(String[] args) {
A a = new A();
// 结果为:11 - 3 = 8
a.func1(11,3);
B b = new B();
// 结果为:11 + 3 = 14,可能为无意识的重写父类求差方法,调用时还误以为是调用父类求差方法
b.func1(11,3);
b.func2(11,3)
}
}
class A {
// 返回两数之差
public int func1(int num1,int num2) {
return num1 - num2;
}
}
// B类重写了父类A中的func1方法
class B extends A {
@Override
public int func1(int num1,int num2) {
return num1 + num2;
}
public int func2(int num1,int num2) {
return func1(num1 , num2) + 9;
}
}
案例Ⅱ
- 通用的解决方法:使原来的父类和子类都继承一个更基础的基类,原有的继承关系去掉,采用依赖、聚合、组合、关联等关系代替
public class Liskov {
public static void main(String[] args) {
A a = new A();
// 结果为:11 - 3 = 8
a.func1(11,3);
B b = new B();
// 结果为:11 + 3 = 14,因为B类和A类不再是继承关系,调用者就不会再误以为func1是求差的
b.func1(11,3);
// 结果为:11 - 3 = 8
b.func3(11,3)
}
}
// 将更加基础的方法和成员抽离到Base类(子类不再重写的方法,不再改变的成员)
class Base {
}
class A extends Base {
// 返回两数之差
public int func1(int num1,int num2) {
return num1 - num2;
}
}
// 使B和A组合
class B extends Base {
private A a = new A();
public int func1(int num1,int num2) {
return num1 + num2;
}
public int func2(int num1,int num2) {
return func1(num1 , num2) + 9;
}
// 使用a的求差方法
public int func3(int num1,int num2) {
return this.a.func1(num1 , num2);
}
}
5、开闭原则
- 编程中最基础、最重要的设计原则
- 一个软件实体,如类、模块、函数应对扩展开放,对修改关闭。用抽象构建框架,用实现扩展细节
- 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化
- 编程中遵循其他原则,以及使用设计模式的目的就是为了遵循开闭原则
案例Ⅰ
- 优点代码好理解,简单易操作
- 缺点违反了开闭原则,即对扩展开放(提供方),对修改关闭(调用者)
- 当给类增加新功能得时候,尽量不修改原代码,或者尽可能得少修改原代码
- 如果现在需求要新增一个绘制三角形得方法,修改地方将较多
public class Liskov {
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Circle());
}
}
class GraphicEditor {
public void drawShape(Shape s) {
if (s.type == 1) {
drawRectangle(s);
} else if (s.type == 2) {
drawCircle(s);
}
}
public void drawRectangle(Shape s) {
System.out.println("绘制矩形");
}
public void drawCircle(Shape s) {
System.out.println("绘制圆形");
}
}
class Shape {
int type;
}
class Rectangle extends Shape {
Rectangle() {
super.type = 1;
}
}
class Circle extends Shape {
Rectangle() {
super.type = 2;
}
}
案例Ⅱ
- 新增功能改动比较多
- 违反开闭原则
public class Liskov {
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Triangle());
}
}
class GraphicEditor {
public void drawShape(Shape s) {
if (s.type == 1) {
drawRectangle(s);
} else if (s.type == 2) {
drawCircle(s);
} else if (s.type == 3) {
drawTriangle(s);
}
}
public void drawRectangle(Shape s) {
System.out.println("绘制矩形");
}
public void drawCircle(Shape s) {
System.out.println("绘制圆形");
}
public void drawTriangle(Shape s) {
System.out.println("绘制三角形");
}
}
class Shape {
int type;
}
class Rectangle extends Shape {
Rectangle() {
super.type = 1;
}
}
class Circle extends Shape {
Circle() {
super.type = 2;
}
}
class Triangle extends Shape {
Triangle() {
super.type = 3;
}
}
案例Ⅲ
- 代码更简洁、新增需求后只需要写具体实现即可
- 符合开闭原则
public class Liskov {
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawRectangle(new Rectangle());
graphicEditor.drawRectangle(new Circle());
graphicEditor.drawRectangle(new Triangle());
}
}
class GraphicEditor {
public void drawShape(Shape s) {
s.draw();
}
}
abstract class Shape {
// 抽象方法
public abstract void draw();
}
class Rectangle extends Shape {
@Override
public vid draw() {
System.out.println("绘制矩形");
}
}
class Circle extends Shape {
@Override
public vid draw() {
System.out.println("绘制圆形");
}
}
class Triangle extends Shape {
@Override
public vid draw() {
System.out.println("绘制三角形");
}
}
6、迪米特法则
- 一个对象应该对其他对象保持最少的了解
- 类与类 关系越密切,耦合度就越大
- 迪米特法则,又叫最少知道原则,即一个类对自己依赖的类 知道的越少越好
- 对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部,对外除了提供的public方法,不对外泄露任何信息
- 只与直接的朋友通信
直接朋友
- 每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就说这两个对象之间是朋友关系。耦合的方式有很多,依赖、关联、组合、聚合等。其中,我们成出现成员变量、方法参数、发放返回值中的类为直接的朋友,而出现在局部变量中的类不是直接的朋友。也就是说,陌生的类最好不要以局部变量的形式出现在类的内部。
案例Ⅰ
- 陌生类出现在局部变量违反迪米特法则
public class Demeter {
public static void main(String[] args) {
EmployeeManager em = new EmployeeManager();
em.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 void setId (String id) {
this.id = id;
}
public String getId() {
return id;
}
}
class CollegeManager {
public List<CollegeEmployee> getAllCollege() {
List<CollegeEmployee> list = new ArrayList<>();
for(int i = 0; i< 10; i++) {
CollegeEmployee ce = new CollegeEmployee();
ce.setId("学院员工id=" + i);
list.add(ce);
}
return list;
}
}
class EmployeeManager {
public List<Employee> getAllEmployee() {
List<Employee> list = new ArrayList<>();
for(int i = 0; i < 5; i++) {
Employee e = new Employee();
e.setId("学校总部员工id=" + i);
list.add(e);
}
return list;
}
void printAllEmployee(CollegeManager cm) {
// 此返回值违反迪米特法则:不是EmployeeManager类的成员、方法入参或出参,即陌生类
List<CollegeEmployee> list1 = cm.getAllCollege();
System.out.println("-----学院员工------");
for (CollegeEmployee ce : list1) {
System.out.println(ce.getId());
}
List<Employee> list2 = this.getAllEmployee();
System.out.println("-----学校总部员工------");
for (Employee e : list2) {
System.out.println(e.getId());
}
}
}
案例Ⅱ
- 不要将类自身方法的具体实现写到别的类里实现
- 类与类之间需要符合最少知道原则,即迪米特法则
public class Demeter {
public static void main(String[] args) {
EmployeeManager em = new EmployeeManager();
em.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 void setId (String id) {
this.id = id;
}
public String getId() {
return id;
}
}
class CollegeManager {
public List<CollegeEmployee> getAllCollege() {
List<CollegeEmployee> list = new ArrayList<>();
for(int i = 0; i< 10; i++) {
CollegeEmployee ce = new CollegeEmployee();
ce.setId("学院员工id=" + i);
list.add(ce);
}
return list;
}
/**
* 不要将自身类的方法具体实现写到别的类里实现
*/
void printAllCollege() {
List<CollegeEmployee> list1 = this.getAllCollege();
System.out.println("-----学院员工------");
for (CollegeEmployee ce : list1) {
System.out.println(ce.getId());
}
}
}
class EmployeeManager {
public List<Employee> getAllEmployee() {
List<Employee> list = new ArrayList<>();
for(int i = 0; i < 5; i++) {
Employee e = new Employee();
e.setId("学校总部员工id=" + i);
list.add(e);
}
return list;
}
void printAllEmployee(CollegeManager cm) {
// 通过public方法调用修改后的方法
cm.printAllCollege();
List<Employee> list2 = this.getAllEmployee();
System.out.println("-----学校总部员工------");
for (Employee e : list2) {
System.out.println(e.getId());
}
}
}
7、合成复用原则
-
尽量使用合成/聚合的方式,而不是使用继承
-
如果只是让一个类使用另一个类中的部分方法,使用继承就会让两者之间耦合度增强
- 依赖:类A作为类B方法的入参
- 聚合:类A作为类B的成员变量,并通过setter方法进行赋值
- 组合:类A作为类B的成员变量,并直接new创建A对象
设计原则的核心思想
- 找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起
- 针对接口编程,而不是针对实现编程
- 为了交互对象之间的松耦合设计而努力
类图
依赖关系
- 类的成员属性(也是聚合)
- 类的方法入参
- 类的方法出参
- 类方法中的局部变量
泛化 - 依赖关系的特列
- 泛化实际上就是继承关系
实现关系 - 依赖关系的特列
- 类实现接口
关联关系 - 依赖关系的特列
- 关联具有导航行:单向关系、双向关系、
- 一个类是另一个类的成员属性,单向关系
- 两个类互相为成员属性,双向关系
聚合 - 关联关系的特列
- 表示整体和部分的关系,整体与部分可以分开
- 整体和部分可以分开为聚合,不可分开为组合
组合 - 关联关系的特列
- 一个类为另一个类的成员变量,且通过new直接创建
设计模式类型
- 创建型模式:单例、抽象工厂、原型、建造者、工厂
- 结构型模式:适配器、桥接、装饰、组合、外观、享元、代理
- 行为型模式:模板方法、命令、访问者、迭代器、观察者、中介者、备忘录、解释器、状态、策略、责任链