标签:end out http 修改 inf set his 学习java version
1.定义:高层模块不应该依赖低层模块,二者都应该依赖其抽象
2.抽象不应该依赖细节,细节应该依赖抽象
3.针对接口编程,不要针对实现编程
4.优点:可以减少类间的耦合性、提高系统稳定性,提高代码可读性和可维护性,可降低修改程序所造成的风险
5.实例目录package
6.UML类图
7.代码
1 package com.geely.design.principle.dependenceinversion; 2 3 public interface ICourse { 4 void studyCourse(); 5 }
1 package com.geely.design.principle.dependenceinversion; 2 3 public class JavaCourse implements ICourse{ 4 public void studyCourse() { 5 System.out.println("Geely在学习Java课程"); 6 } 7 }
1 package com.geely.design.principle.dependenceinversion; 2 3 public class PythonCourse implements ICourse { 4 public void studyCourse() { 5 System.out.println("Geely在学习Python课程"); 6 } 7 }
1 package com.geely.design.principle.dependenceinversion; 2 3 public class FECourse implements ICourse { 4 public void studyCourse() { 5 System.out.println("Geely在学习FE课程"); 6 } 7 }
1 package com.geely.design.principle.dependenceinversion; 2 3 public class Geely { 4 private ICourse iCourse; 5 6 public Geely(){ 7 8 } 9 public Geely(ICourse iCourse){ 10 this.iCourse = iCourse; 11 } 12 public void studyImoocCourse(){//ICourse iCourse 13 iCourse.studyCourse(); 14 } 15 16 public void setiCourse(ICourse iCourse){ 17 this.iCourse = iCourse; 18 } 19 /*public void studyJavaCourse(){ 20 System.out.println("Geely在学习Java课程"); 21 } 22 public void studyFECourse(){ 23 System.out.println("Geely在学习FE课程"); 24 } 25 public void studyPythonCourse(){ 26 System.out.println("Geely在学习Python课程"); 27 }*/ 28 }
1 package com.geely.design.principle.dependenceinversion; 2 3 public class Test { 4 public static void main(String[] args){ 5 /*Geely geely = new Geely(); 6 geely.studyJavaCourse(); 7 geely.studyFECourse(); 8 geely.studyPythonCourse();*/ 9 10 /*Geely geely = new Geely(); 11 geely.studyImoocCourse(new JavaCourse()); 12 geely.studyImoocCourse(new FECourse()); 13 geely.studyImoocCourse(new PythonCourse());*/ 14 15 /*Geely geely = new Geely(new JavaCourse()); 16 geely.studyImoocCourse(); 17 Geely geely2 = new Geely(new FECourse()); 18 geely2.studyImoocCourse();*/ 19 20 Geely geely = new Geely(); 21 geely.setiCourse(new JavaCourse()); 22 geely.studyImoocCourse(); 23 geely.setiCourse(new FECourse()); 24 geely.studyImoocCourse(); 25 } 26 }
标签:end out http 修改 inf set his 学习java version
原文地址:https://www.cnblogs.com/linbq1911/p/10204521.html