# 第02章 软件设计七大原则
# 2.1 开闭原则
定义:一个软件实体如类、模块和函数应该对扩展开放,对修改关闭;
用抽象构建框架,用实现扩展细节;(实现开闭原则的方式:面向抽象编程)
优点:提高软件系统的可复用性及可维护性;
Coding:
# 2.1.1 第一版
Icourse.java
package com.wangchao.design.principle.openclose;
public interface ICourse {
Integer getId();
String getName();
Double getPrice();
}
2
3
4
5
6
JavaCourse.java
package com.wangchao.design.principle.openclose;
public class JavaCourse implements ICourse {
private Integer id;
private String name;
private Double price;
public JavaCourse(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Double getPrice() {
return this.price;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Test.java
package com.wangchao.design.principle.openclose;
public class Test {
public static void main(String[] args) {
ICourse javaCourse = new JavaCourse(96, "Java从零到企业级电商开发", 348d);
//实际项目中System.out.println()尽量不用(禁止),因为System.out.println()中是有锁的
System.out.println("课程ID:" + javaCourse.getId() + " 课程名称:" + javaCourse.getName() + " 课程价格:" + javaCourse.getPrice());
}
}
2
3
4
5
6
7
8
结果:
类结构图:
# 2.1.2 第二版
增加一个打折接口
package com.wangchao.design.principle.openclose;
public interface ICourse {
Integer getId();
String getName();
Double getPrice();
Double getDiscountPrice();
}
2
3
4
5
6
7
8
修改实现类
package com.wangchao.design.principle.openclose;
public class JavaCourse implements ICourse {
private Integer id;
private String name;
private Double price;
public JavaCourse(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Double getPrice() {
return this.price;
}
public Double getDiscountPrice() {
return this.price*0.8;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
功能是实现了,但是不但改动了接口,还改动了实现类,如果实现类较多,那么改动的地方就更多了。
接口是不应该经常改动的,应该是稳定且可靠的
# 2.1.3 第三版
由继承实现增加获取打折后价格功能,不修改ICourse\JavaCourse类
增加JavaDiscountCourse.java类
package com.wangchao.design.principle.openclose;
public class JavaDiscountCourse extends JavaCourse {
public JavaDiscountCourse(Integer id, String name, Double price) {
super(id, name, price);
}
public Double getOriginPrice(){
return super.getPrice();
}
public Double getDiscountPrice(){
return super.getPrice()*0.8;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Test.java
package com.wangchao.design.principle.openclose;
public class Test {
public static void main(String[] args) {
ICourse iCourse = new JavaDiscountCourse(96, "Java从零到企业级电商开发", 348d);
JavaDiscountCourse javaCourse = (JavaDiscountCourse) iCourse;
//实际项目中System.out.println()尽量不用(禁止),因为System.out.println()中是有锁的
System.out.println("课程ID:" + javaCourse.getId() + " 课程名称:" + javaCourse.getName() + " 课程原价:" + javaCourse.getOriginPrice() + " 课程折后价格:" + javaCourse.getDiscountPrice() + "元");
}
}
2
3
4
5
6
7
8
9
10
# 2.2 依赖倒置原则
**定义:**高层模块不应该依赖低层模块,二者都应该依赖其抽象;
抽象不应该依赖细节;细节应该依赖抽象;
针对接口编程,不要针对实现编程;
**优点:**可以减少类间的耦合性、提高系统稳定性、提高代码可读性和可维护性、可降低修改程序所造成的风险;
# 2.2.1 第一版
面向实现编程:Clare目前正在学习两门课程Java和FE
Clare.java
package com.wangchao.design.principle.dependenceinversion;
public class Clare {
public void studyJavaCourse() {
System.out.println("Clare在学习Java课程");
}
public void studyFECourse() {
System.out.println("Clare在学习FE课程");
}
}
2
3
4
5
6
7
8
9
10
Test.java
package com.wangchao.design.principle.dependenceinversion;
public class Test {
public static void main(String[] args) {
Clare clare = new Clare();
clare.studyJavaCourse();
clare.studyFECourse();
}
}
2
3
4
5
6
7
8
9
现在Clare需要再增加一门Python的学习,面向实现编程改变如下:
Clare.java中增加方法:
package com.wangchao.design.principle.dependenceinversion;
public class Clare {
public void studyJavaCourse() {
System.out.println("Clare在学习Java课程");
}
public void studyFECourse() {
System.out.println("Clare在学习FE课程");
}
public void studyPythonCourse() {
System.out.println("Clare在学习Python课程");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
Test.java
package com.wangchao.design.principle.dependenceinversion;
public class Test {
public static void main(String[] args) {
Clare clare = new Clare();
clare.studyJavaCourse();
clare.studyFECourse();
clare.studyPythonCourse();
}
}
2
3
4
5
6
7
8
9
10
这种面向实现编程会导致Clare中经常需要修改代码,Clare属于低层模块,Test属于高层模块,Test依赖低层的模块,即依赖于具体实现,Test中扩展什么都需要去Clare中扩展进行补充
# 2.2.2 第二版
面向接口编程:增加ICourse接口
ICourse.java
package com.wangchao.design.principle.dependenceinversion;
public interface ICourse {
void studyCourse();
}
2
3
4
5
创建实现类:(需要学习新的课程可以扩展实现即可)
JavaCourse.java
package com.wangchao.design.principle.dependenceinversion;
public class JavaCourse implements ICourse {
public void studyCourse() {
System.out.println("Clare在学习Java课程");
}
}
2
3
4
5
6
7
FECourse.java
package com.wangchao.design.principle.dependenceinversion;
public class FECourse implements ICourse {
public void studyCourse() {
System.out.println("Clare在学习FE课程");
}
}
2
3
4
5
6
7
PythonCourse.java
package com.wangchao.design.principle.dependenceinversion;
public class PythonCourse implements ICourse {
public void studyCourse() {
System.out.println("Clare在学习Python课程");
}
}
2
3
4
5
6
7
Clare.java 第一种实现方式 不使用注入的方式
package com.wangchao.design.principle.dependenceinversion;
public class Clare {
public void studyITCourse(ICourse iCourse){
iCourse.studyCourse();
}
}
2
3
4
5
6
此时 Test.java为:
package com.wangchao.design.principle.dependenceinversion;
public class Test {
public static void main(String[] args) {
Clare clare = new Clare();
clare.studyITCourse(new JavaCourse());
clare.studyITCourse(new FECourse());
clare.studyITCourse(new PythonCourse());
}
}
2
3
4
5
6
7
8
9
Clare.java 第二种实现方式 使用构造器注入
package com.wangchao.design.principle.dependenceinversion;
public class Clare {
private ICourse iCourse;
public Clare(ICourse iCourse) {
this.iCourse = iCourse;
}
public void studyITCourse(){
iCourse.studyCourse();
}
}
2
3
4
5
6
7
8
9
10
11
12
此时Test.java变为:
package com.wangchao.design.principle.dependenceinversion;
public class Test {
public static void main(String[] args) {
Clare clare = new Clare(new JavaCourse());
clare.studyITCourse();
}
}
2
3
4
5
6
7
如果注入的是DAO,那么可以这样做,具体依赖于DAO的方法,但本处不适用,不能学习多种课程
Clare.java 第三种实现方式 使用setter注入
package com.wangchao.design.principle.dependenceinversion;
public class Clare {
private ICourse iCourse;
public void setiCourse(ICourse iCourse) {
this.iCourse = iCourse;
}
public void studyITCourse(){
iCourse.studyCourse();
}
}
2
3
4
5
6
7
8
9
10
11
此时Test.java变为:
package com.wangchao.design.principle.dependenceinversion;
public class Test {
public static void main(String[] args) {
Clare clare = new Clare();
clare.setiCourse(new JavaCourse());
clare.studyITCourse();
clare.setiCourse(new PythonCourse());
clare.studyITCourse();
}
}
2
3
4
5
6
7
8
9
10
11
# 2.3 单一职责原则
定义:不要存在多于一个导致类变更的原因
一个类/接口/方法只负责一项职责
优点:降低类的复杂度、提高类的可读性;提高系统的可维护性、降低变更引起的风险
# 2.3.1 类 的单一职责
不符合单一职责原则示例:
package com.wangchao.design.principle.singleresponsibility;
public class Bird {
public void mainMoveMode(String birdName) {
if ("鸵鸟".equals(birdName)) {
System.out.println(birdName + "用脚走");
} else {
System.out.println(birdName+"用翅膀飞");
}
}
}
2
3
4
5
6
7
8
9
10
11
Test.java
package com.wangchao.design.principle.singleresponsibility;
public class Test {
public static void main(String[] args) {
Bird bird = new Bird();
bird.mainMoveMode("大雁");
bird.mainMoveMode("鸵鸟");
}
}
2
3
4
5
6
7
8
9
修改如下:
FlyBird.java
package com.wangchao.design.principle.singleresponsibility;
public class FlyBird {
public void mainMoveMode(String birdName) {
System.out.println(birdName + "用翅膀飞");
}
}
2
3
4
5
6
7
WalkBird.java
package com.wangchao.design.principle.singleresponsibility;
public class WalkBird {
public void mainMoveMode(String birdName) {
System.out.println(birdName + "用脚走");
}
}
2
3
4
5
6
7
Test.java
package com.wangchao.design.principle.singleresponsibility;
public class Test {
public static void main(String[] args) {
FlyBird flyBird = new FlyBird();
flyBird.mainMoveMode("大雁");
WalkBird walkBird = new WalkBird();
walkBird.mainMoveMode("鸵鸟");
}
}
2
3
4
5
6
7
8
9
10
11
# 2.3.2 接口 的单一职责
package com.wangchao.design.principle.singleresponsibility;
public interface ICourse {
// 课程内容相关接口
String getCourseName();
byte[] getCourseVideo();
// 课程管理相关接口
void studyCourse();
void refundCourse();
}
2
3
4
5
6
7
8
9
10
11
按照单一职责原则可以调整为:
ICourseContent.java
package com.wangchao.design.principle.singleresponsibility;
public interface ICourseContent {
String getCourseName();
byte[] getCourseVideo();
}
2
3
4
5
6
ICourseManager.java
package com.wangchao.design.principle.singleresponsibility;
public interface ICourseManager {
void studyCourse();
void refundCourse();
}
2
3
4
5
6
CourseImpl.java
package com.wangchao.design.principle.singleresponsibility;
public class CourseImpl implements ICourseContent,ICourseManager {
public String getCourseName() {
return null;
}
public byte[] getCourseVideo() {
return new byte[0];
}
public void studyCourse() {
}
public void refundCourse() {
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
类图:
# 2.3.3 方法 的单一职责
package com.wangchao.design.principle.singleresponsibility;
public class Method {
private void updateUserInfo(String userName,String address){
userName = "clare";
address = "beijing";
}
private void updateUserInfo(String userName,String... properties){
userName = "clare";
// address = "beijing";
}
private void updateUsername(String userName){
userName = "clare";
}
private void updateUserAddress(String address){
address = "beijing";
}
private void updateUserInfo(String userName,String address,boolean bool){
if(bool){
//todo something1
}else{
//todo something2
}
//这种情况通常需要拆分为两个方法
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
实际开发中为了避免类的爆炸性增长,通知只会使用了接口和方法的单一职责
# 2.4 接口隔离原则
定义:用多个专门的接口,而不使用单一的总接口,客户端不应该依赖它不需要的接口;
注意:
- 一个类对一个类的依赖应该建立在最小的接口上;
- 建立单一接口,不要建立庞大臃肿的接口;
- 尽量细化接口,接口中的方法尽量少;
- 注意适度原则,一定要适度;
优点:符合我们常说的高内聚低耦合的设计思想,从而使得类具有很好的可读性、可扩展性和可维护性。
原始设计:
package com.wangchao.design.principle.interfacesegregation;
public interface IAnimalAction {
void eat();
void fly();
void swim();
}
package com.wangchao.design.principle.interfacesegregation;
public class Bird implements IAnimalAction {
public void eat() {
}
public void fly() {
}
public void swim() {
}
}
package com.wangchao.design.principle.interfacesegregation;
public class Dog implements IAnimalAction {
public void eat() {
}
public void fly() {
}
public void swim() {
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
使用接口隔离原则修改:
package com.wangchao.design.principle.interfacesegregation;
public interface IAnimalAction {
void eat();
}
package com.wangchao.design.principle.interfacesegregation;
public interface IFlyAnimalAction extends IAnimalAction {
void fly();
}
package com.wangchao.design.principle.interfacesegregation;
public interface ISwimAnimalAction extends IAnimalAction {
void swim();
}
package com.wangchao.design.principle.interfacesegregation;
public class Bird implements IFlyAnimalAction {
public void fly() {
}
public void eat() {
}
}
package com.wangchao.design.principle.interfacesegregation;
public class Dog implements ISwimAnimalAction {
public void swim() {
}
public void eat() {
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 2.5 迪米特法则(最少知道原则)
定义:一个对象应该对其他对象保持最少的了解,又叫最少知道原则;
尽量降低类与类之间的耦合;
优点:降低类之间的耦合;
注意:在使用迪米特法则的时候,需要反复权衡,既要做到结构清晰,又要做到低耦合、高内聚
强调只和朋友交流,不和陌生人说话
朋友:出现在成员变量、方法的输入、输出参数中的类称为成员朋友类,而出现在方法体内部的类不属于朋友类;
# 2.5.1 不符合迪米特法则示例
package com.wangchao.design.principle.demeter;
public class Course {
}
2
3
4
package com.wangchao.design.principle.demeter;
import java.util.ArrayList;
import java.util.List;
public class Boss {
public void commandCheckNumber(TeamLeader teamLeader){
List<Course> courseList = new ArrayList<Course>();
for(int i = 0 ;i < 20;i++){
courseList.add(new Course());
}
teamLeader.checkNumberOfCourses(courseList);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.wangchao.design.principle.demeter;
import java.util.List;
public class TeamLeader {
public void checkNumberOfCourses(List<Course> courseList){
System.out.println("在线课程的数量是:"+courseList.size());
}
}
2
3
4
5
6
7
8
9
10
package com.wangchao.design.principle.demeter;
public class Test {
public static void main(String[] args) {
Boss boss = new Boss();
TeamLeader teamLeader = new TeamLeader();
boss.commandCheckNumber(teamLeader);
}
}
2
3
4
5
6
7
8
9
# 2.5.2 修改为符合迪米特法则
上面的Course不应该由Boss来创建,Boss只需要关注TeamLeader,而Course由TeamLeader来创建
package com.wangchao.design.principle.demeter;
public class Boss {
public void commandCheckNumber(TeamLeader teamLeader){
teamLeader.checkNumberOfCourses();
}
}
2
3
4
5
6
7
package com.wangchao.design.principle.demeter;
import java.util.ArrayList;
import java.util.List;
public class TeamLeader {
public void checkNumberOfCourses(){
List<Course> courseList = new ArrayList<Course>();
for(int i = 0 ;i < 20;i++){
courseList.add(new Course());
}
System.out.println("在线课程的数量是:"+courseList.size());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.wangchao.design.principle.demeter;
public class Course {
}
2
3
4
package com.wangchao.design.principle.demeter;
public class Test {
public static void main(String[] args) {
Boss boss = new Boss();
TeamLeader teamLeader = new TeamLeader();
boss.commandCheckNumber(teamLeader);
}
}
2
3
4
5
6
7
8
9
# 2.6 里式替换原则
定义:如果对每一个类型为T1的对象o1,都有类型为T2的对象o2,使得以T1定义的所有程序P在所有的对象o1都替换成o2时,程序P的行为没有发生变化,那么类型T2是类型T1的子类型;
定义扩展:一个软件实体如果适用一个父类的话,那一定适用于其子类,所有引用父类的地方必须能透明地适用其子类的对象,子类对象能够替换父类对象,而程序逻辑不变;
引申意义:子类可以扩展父类的功能,但不能改变父类原有的功能;
- 含义01:子类可以实现父类的抽象方法,但不能覆盖父类的非抽象方法;
- 含义02:子类中可以增加自己特有的方法;
- 含义03:当子类的方法重载父类的方法时,方法的前置条件(即方法的入参\输入)要比父类方法的输入参数更宽松;
- 含义04:当子类的方法实现父类的方法式(重写/重载或实现抽象方法)方法的后置条件(即方法的输出\返回值)要比父类更严格或相等;
对前面的开闭原则中的类进行修改,使之符合里式替换原则
package com.wangchao.design.principle.openclose;
public class JavaDiscountCourse extends JavaCourse {
public JavaDiscountCourse(Integer id, String name, Double price) {
super(id, name, price);
}
/* public Double getOriginPrice(){
return super.getPrice();
}*/
public Double getDiscountPrice(){
return super.getPrice()*0.8;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Test.java
package com.wangchao.design.principle.openclose;
public class Test {
public static void main(String[] args) {
ICourse iCourse = new JavaDiscountCourse(96, "Java从零到企业级电商开发", 348d);
JavaDiscountCourse javaCourse = (JavaDiscountCourse) iCourse;
//实际项目中System.out.println()尽量不用(禁止),因为System.out.println()中是有锁的
System.out.println("课程ID:" + javaCourse.getId() + " 课程名称:" + javaCourse.getName() + " 课程原价:" + javaCourse.getPrice() + " 课程折后价格:" + javaCourse.getDiscountPrice() + "元");
}
}
2
3
4
5
6
7
8
9
10
- 优点01:约束继承泛滥,开闭原则的一种体现;
- 优点02:加强程序的健壮性,同时变更时也可以做到非常好的兼容性,提供程序的维护性、扩展性,降低需求变更时引入的风险;
# 2.6.1 不符合里式替换原则示例
假设有一个长方形和一个正方形的类,正方形继承于长方形,应用类中有一个resize长方形的边的方法
长方形:
package com.wangchao.design.principle.liskovsubstitution;
public class Rectangle {
private long length;
private long width;
public long getWidth() {
return width;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public void setWidth(long width) {
this.width = width;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
正方形:
package com.wangchao.design.principle.liskovsubstitution;
public class Rectangle {
private long length;
private long width;
public long getWidth() {
return width;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public void setWidth(long width) {
this.width = width;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
验证正方形是否可以替换长方形
package com.wangchao.design.principle.liskovsubstitution;
public class Test {
public static void resize(Rectangle rectangle){
while (rectangle.getWidth() <= rectangle.getLength()){
rectangle.setWidth(rectangle.getWidth()+1);
System.out.println("width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
System.out.println("resize方法结束 width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(10);
rectangle.setLength(20);
resize(rectangle);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.wangchao.design.principle.liskovsubstitution;
public class Test {
public static void resize(Rectangle rectangle){
while (rectangle.getWidth() <= rectangle.getLength()){
rectangle.setWidth(rectangle.getWidth()+1);
System.out.println("width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
System.out.println("resize方法结束 width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
/*public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(10);
rectangle.setLength(20);
resize(rectangle);
}*/
public static void main(String[] args) {
Square square = new Square();
square.setLength(10);
resize(square);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
可以看出,当长方形替换为正方形的时候,出现了死循环,resize方法直到内存溢出才会停止,所有不符合里式替换原则
# 2.6.2 符合里式替换原则示例
创建长方形和正方形的实现--四边形
package com.wangchao.design.principle.liskovsubstitution;
public interface Quadrangle {
long getWidth();
long getLength();
}
2
3
4
5
6
长方形
package com.wangchao.design.principle.liskovsubstitution;
public class Rectangle implements Quadrangle {
private long length;
private long width;
public long getWidth() {
return width;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public void setWidth(long width) {
this.width = width;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
正方形
package com.wangchao.design.principle.liskovsubstitution;
public class Square implements Quadrangle {
private long sideLength;
public long getSideLength() {
return sideLength;
}
public void setSideLength(long sideLength) {
this.sideLength = sideLength;
}
public long getWidth() {
return getSideLength();
}
public long getLength() {
return getSideLength();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
此时,resize方法变不能接受正方形的对象了:
package com.wangchao.design.principle.liskovsubstitution;
public class Test {
public static void resize(Rectangle rectangle){
while (rectangle.getWidth() <= rectangle.getLength()){
rectangle.setWidth(rectangle.getWidth()+1);
System.out.println("width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
System.out.println("resize方法结束 width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(10);
rectangle.setLength(20);
resize(rectangle);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2.6.3 方法的入参
当子类的方法重载父类的方法时,方法的前置条件(即方法的入参\输入)要比父类方法的输入参数更宽松;
父类
package com.wangchao.design.principle.liskovsubstitution.methodinput;
import java.util.HashMap;
public class Base {
public void method(HashMap map){
System.out.println("父类被执行");
}
}
2
3
4
5
6
7
8
9
子类
package com.wangchao.design.principle.liskovsubstitution.methodinput;
import java.util.HashMap;
import java.util.Map;
public class Child extends Base {
// 重写
/*@Override
public void method(HashMap map) {
System.out.println("子类HashMap入参方法被执行");
}*/
// 重载
public void method(Map map) {
System.out.println("子类HashMap入参方法被执行");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Test
package com.wangchao.design.principle.liskovsubstitution.methodinput;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
Child child = new Child();
HashMap hashMap = new HashMap();
child.method(hashMap);
}
}
2
3
4
5
6
7
8
9
10
11
可以看到,子类的Map比父类的HashMap的范围要宽松,子类的方法永远不会被执行;
如果,换一下,将父类变作Map,子类变作HashMap,则将会执行子类的方法
public class Base {
public void method(Map map){
System.out.println("父类被执行");
}
}
public class Child extends Base {
// 重载
public void method(HashMap map) {
System.out.println("子类HashMap入参方法被执行");
}
}
public class Test {
public static void main(String[] args) {
Child child = new Child();
HashMap hashMap = new HashMap();
child.method(hashMap);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
这就违反了里式替换原则,所以我们在使用子类重载父类的方法时,方法的前置条件(即方法的入参\输入)一定要比父类方法的输入参数更宽松,如果相同的话,就目前这个case而言,那就不是重载了,而是重写,所以相同也可以,宽松也可以。重写的话,肯定就是执行子类了在Test案例中,将接受的Child替换为Base也是可以的。
# 2.6.4 方法的输出
当子类的方法实现父类的方法式(重写/重载或实现抽象方法)方法的后置条件(即方法的输出\返回值)要比父类更严格或相等;
public abstract class Base {
public abstract Map method();
}
public class Child extends Base {
@Override
public HashMap method() {
HashMap hashMap = new HashMap();
System.out.println("子类method被执行");
hashMap.put("message","子类method被执行");
return hashMap;
}
}
public class Test {
public static void main(String[] args) {
Child child = new Child();
System.out.println(child.method());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
或者
public abstract class Base {
public abstract Map method();
}
public class Child extends Base {
@Override
public Map method() {
HashMap hashMap = new HashMap();
System.out.println("子类method被执行");
hashMap.put("message","子类method被执行");
return hashMap;
}
}
public class Test {
public static void main(String[] args) {
Child child = new Child();
System.out.println(child.method());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
结果都一样:
# 2.7 合成复用原则(组合复用原则)
定义:尽量使用对象组合/聚合,而不是继承关系达到软件复用的目的
聚合:has a
组合:contains a
优点:可以使系统更加灵活,降低类与类之间的耦合度,一个类的变化对其他类造成的影响相对较少;
何时使用合成/聚合、继承:聚合has a、组合contains a 继承 is a
# 2.7.1 单纯的使用继承的模式
public class DBConnection {
public String getConnection(){
return "MySQL数据库连接";
}
}
public class ProductDao extends DBConnection{
public void addProduct(){
String conn = super.getConnection();
System.out.println("使用"+conn+"增加产品");
}
}
public class Test {
public static void main(String[] args) {
ProductDao productDao = new ProductDao();
productDao.addProduct();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
结果:
# 2.7.2 使用组合的模式
public abstract class DBConnection {
public abstract String getConnection();
}
public class MySQLConnection extends DBConnection {
@Override
public String getConnection() {
return "MySQL数据库连接";
}
}
public class PostgreSQLConnection extends DBConnection {
@Override
public String getConnection() {
return "PostgreSQL数据库连接";
}
}
public class ProductDao{
private DBConnection dbConnection;
public void setDbConnection(DBConnection dbConnection) {
this.dbConnection = dbConnection;
}
public void addProduct(){
String conn = dbConnection.getConnection();
System.out.println("使用"+conn+"增加产品");
}
}
public class Test {
public static void main(String[] args) {
ProductDao productDao = new ProductDao();
productDao.setDbConnection(new PostgreSQLConnection());
productDao.addProduct();
productDao.setDbConnection(new MySQLConnection());
productDao.addProduct();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
结果: