1 课程讲解
1.1 定义
1.2 优点
1.3 细节描述
2 代码演练
2.0 代码展示优点
2.1 非面向接口编程
2.2 面向接口编程1 传参
2.3 面向接口编程2 构造函数
2.4 面向接口编程3 set方法
1 课程讲解
1.1 定义
◆定义:高层模块不应该依赖低层模块,二者都应该依赖其抽象
1.2 优点
◆优点:降低耦合,提高内聚,增强代码的可维护性。
1.3 细节描述
◆抽象不应该依赖细节;细节应该依赖抽象
◆针对接口编程,不要针对实现编程
每个类尽量实现接口或者抽象类,或者继承抽象类并且实现接口
2 代码演练
2.0 代码展示优点
面向接口编程优点:Geely,JavaCourse和PreCourse都不需要动,只扩展其他类和修改test即可
2.1 非面向接口编程
缺点:Test实现依赖于具体实现,对于后续细节频繁的需求变更不适用。
测试类:
package com.geely.design.principle.dependencyinversion; public class Test {
//v1.1 普通方法调取java类
public static void main(String[] args) {
Geely geely = new Geely();
geely.studyJavaClass();
geely.studyPreClass();
}
}
实体类:
package com.geely.design.principle.dependencyinversion; public class Geely {
public void studyJavaClass(){
System.out.println("Geely Study Java Lesson");
} public void studyPreClass(){
System.out.println("Geely Study Pre Lesson");
} }
打印结果:
Geely Study Java Lesson
Geely Study Pre Lesson Process finished with exit code 0
2.2 面向接口编程1 接口方法传参
测试类:
package com.geely.design.principle.dependencyinversion; public class Test {
//v1.1 普通方法调取java类
/*public static void main(String[] args) {
Geely geely = new Geely();
geely.studyJavaClass();
geely.studyPreClass();
}*/ //v1.2 面向接口编程 传参
public static void main(String[] args) {
Geely geely = new Geely();
geely.studyImoocClass(new JavaCourse());
geely.studyImoocClass(new PreCourse());
}
}
实体类:
package com.geely.design.principle.dependencyinversion; public class Geely {
public void studyJavaClass(){
System.out.println("Geely Study Java Lesson");
} public void studyPreClass(){
System.out.println("Geely Study Pre Lesson");
} public void studyImoocClass(Icourse icourse){
icourse.studyClass();
} }
课程类1:
package com.geely.design.principle.dependencyinversion; public class JavaCourse implements Icourse{
@Override
public void studyClass() {
System.out.println("Geely Study Java Lesson");
}
}
课程类2:
package com.geely.design.principle.dependencyinversion; public class PreCourse implements Icourse{
@Override
public void studyClass() {
System.out.println("Geely Study Pre Lesson2");
}
}
课程接口:
package com.geely.design.principle.dependencyinversion; public interface Icourse {
void studyClass();
}
打印日志:
Geely Study Java Lesson
Geely Study Pre Lesson2 Process finished with exit code 0
2.3 面向接口编程2 构造函数
测试类:
package com.geely.design.principle.dependencyinversion; public class Test {
//v1.1 普通方法调取java类
/*public static void main(String[] args) {
Geely geely = new Geely();
geely.studyJavaClass();
geely.studyPreClass();
}*/ //v1.2 面向接口编程 传参
/*public static void main(String[] args) {
Geely geely = new Geely();
geely.studyImoocClass(new JavaCourse());
geely.studyImoocClass(new PreCourse());
}*/ //v1.3 面向接口编程 构造函数
public static void main(String[] args) {
Geely geely = new Geely(new JavaCourse());
geely.studyImoocClass();
}
}
实体类:
package com.geely.design.principle.dependencyinversion; public class Geely {
Icourse icourse ;
public Geely(Icourse icourse){
this.icourse = icourse;
} public void studyImoocClass(){
icourse.studyClass();
} }
课程类:
同上
课程接口:
同上
打印日志:
Geely Study Java Lesson Process finished with exit code 0
2.4 面向接口编程3 set方法
测试类:
package com.geely.design.principle.dependencyinversion; public class Test {
//v1.1 普通方法调取java类
/*public static void main(String[] args) {
Geely geely = new Geely();
geely.studyJavaClass();
geely.studyPreClass();
}*/ //v1.2 面向接口编程 传参
/*public static void main(String[] args) {
Geely geely = new Geely();
geely.studyImoocClass(new JavaCourse());
geely.studyImoocClass(new PreCourse());
}*/ //v1.3 面向接口编程 构造函数
/*public static void main(String[] args) {
Geely geely = new Geely(new JavaCourse());
geely.studyImoocClass();
}*/ //v1.4 面向接口编程 set方法
public static void main(String[] args) {
Geely geely = new Geely();
geely.setIcourse(new JavaCourse());
geely.studyImoocClass(); geely.setIcourse(new PreCourse());
geely.studyImoocClass();
} }
实体类:
package com.geely.design.principle.dependencyinversion; public class Geely {
Icourse icourse ; public void setIcourse(Icourse icourse) {
this.icourse = icourse;
} public void studyImoocClass(){
icourse.studyClass();
} }
打印日志:
Geely Study Java Lesson
Geely Study Pre Lesson3 Process finished with exit code 0
课程类:
同上
课程接口:
同上