package com.renxu.shejiyuanze.jiekougeli.improve;publicclassSegregation2{publicstaticvoidmain(String[] args){
A a =newA();
a.depend1(newB());// A 类通过接口去依赖 B 类
a.depend2(newB());
a.depend3(newB());
C c =newC();
c.depend1(newD());// C 类通过接口去依赖(使用)D 类
c.depend4(newD());
c.depend5(newD());}}interfaceInterface1{voidoperation1();}interfaceInterface2{voidoperation2();voidoperation3();}interfaceInterface3{voidoperation4();voidoperation5();}classBimplementsInterface1, Interface2 {publicvoidoperation1(){
System.out.println("B 实现了 operation1");}publicvoidoperation2(){
System.out.println("B 实现了 operation2");}publicvoidoperation3(){
System.out.println("B 实现了 operation3");}}classDimplementsInterface1, Interface3 {publicvoidoperation1(){
System.out.println("D 实现了 operation1");}publicvoidoperation4(){
System.out.println("D 实现了 operation4");}publicvoidoperation5(){
System.out.println("D 实现了 operation5");}}classA{// A 类通过接口 Interface1,Interface2 依赖(使用) B 类,但是只会用到 1,2,3 方法publicvoiddepend1(Interface1 i){
i.operation1();}publicvoiddepend2(Interface2 i){
i.operation2();}publicvoiddepend3(Interface2 i){
i.operation3();}}classC{// C 类通过接口 Interface1,Interface3 依赖(使用) D 类,但是只会用到 1,4,5 方法publicvoiddepend1(Interface1 i){
i.operation1();}publicvoiddepend4(Interface3 i){
i.operation4();}publicvoiddepend5(Interface3 i){
i.operation5();}}
依赖倒转原则
package com.renxu.shejiyuanze.yilaidaozhuan.improve;publicclassDependencyPass{publicstaticvoidmain(String[] args){
ChangHong changHong =newChangHong();//方式一// IOpenAndClose iOpenAndClose =new OpenAndClose();// iOpenAndClose.open(changHong);//方式二// IOpenAndClose iOpenAndClose = new OpenAndClose(changHong);// iOpenAndClose.open();//方式三
IOpenAndClose iOpenAndClose =newOpenAndClose();
iOpenAndClose.setTv(changHong);
iOpenAndClose.open();}}interfaceITV{//ITV 接口publicvoidplay();}classChangHongimplementsITV{@Overridepublicvoidplay(){
System.out.println("长虹电视机,打开");}}//方式一// interface IOpenAndClose {// public void open(ITV tv); //抽象方法,接收接口// }// } 实现接口// class OpenAndClose implements IOpenAndClose{// public void open(ITV tv){// tv.play();// }// }// 方式 2: 通过构造方法依赖传递// interface IOpenAndClose {// public void open(); //抽象方法// }// class OpenAndClose implements IOpenAndClose{// public ITV tv; //成员// public OpenAndClose(ITV tv){ //构造器// this.tv = tv;// }// public void open(){// this.tv.play();// }// }//方式三:通过set方法进行依赖倒转interfaceIOpenAndClose{publicvoidopen();// 抽象方法publicvoidsetTv(ITV tv);}classOpenAndCloseimplementsIOpenAndClose{private ITV tv;publicvoidsetTv(ITV tv){this.tv = tv;}publicvoidopen(){this.tv.play();}}
里氏替换原则
package com.renxu.shejiyuanze.lishitihuan;publicclassLiskov{publicstaticvoidmain(String[] args){
A a =newA();
System.out.println("11-3="+ a.func1(11,3));
System.out.println("1-8="+ a.func1(1,8));
B b =newB();
System.out.println("11-3="+ b.func1(11,3));//这里本意是求出 11-3
System.out.println("1-8="+ b.func1(1,8));// 1-8
System.out.println("11+3+9="+ b.func2(11,3));}}// A 类classA{// 返回两个数的差publicintfunc1(int num1,int num2){return num1 - num2;}}// B 类继承了 A// 增加了一个新功能:完成两个数相加,然后和 9 求和classBextendsA{//这里,重写了 A 类的方法, 可能是无意识publicintfunc1(int a,int b){return a + b;}publicintfunc2(int a,int b){returnfunc1(a, b)+9;}}
package com.renxu.shejiyuanze.lishitihuan.improve;publicclassLiskov1{publicstaticvoidmain(String[] args){
A a =newA();
System.out.println("11-3="+ a.func1(11,3));
System.out.println("1-8="+ a.func1(1,8));
B b =newB();//因为 B 类不再继承 A 类,因此调用者,不会再 func1 是求减法//调用完成的功能就会很明确
System.out.println("11+3="+ b.func1(11,3));//这里本意是求出 11+3
System.out.println("1+8="+ b.func1(1,8));// 1+8
System.out.println("11+3+9="+ b.func2(11,3));//使用组合仍然可以使用到 A 类相关方法
System.out.println("11-3="+ b.func3(11,3));// 这里本意是求出 11-3}}//创建一个更加基础的基类classBase{//把更加基础的方法和成员写到 Base 类}// A 类classAextendsBase{// 返回两个数的差publicintfunc1(int num1,int num2){return num1 - num2;}}// 增加了一个新功能:完成两个数相加,然后和 9 求和classBextendsBase{//如果 B 需要使用 A 类的方法,使用组合关系private A a =newA();//这里,重写了 A 类的方法, 可能是无意识publicintfunc1(int a,int b){return a + b;}publicintfunc2(int a,int b){returnfunc1(a, b)+9;}//我们仍然想使用 A 的方法publicintfunc3(int a,int b){returnthis.a.func1(a, b);}}