本文主要是介绍图解java设计模式_08_依赖倒转原则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
依赖倒转的中心思想是面向接口编程
package com.atguigu.principle.inversion;public class DependecyInversion {public static void main(String[] args) {Person person = new Person();person.receive(new Email());}}class Email {public String getInfo() {return "电子邮件信息: hello,world";}
}//完成Person接收消息的功能
//方式1分析
//1. 简单,比较容易想到
//2. 如果我们获取的对象是 微信,短信等等,则新增类,同时Perons也要增加相应的接收方法
//3. 解决思路:引入一个抽象的接口IReceiver, 表示接收者, 这样Person类与接口IReceiver发生依赖
// 因为Email, WeiXin 等等属于接收的范围,他们各自实现IReceiver 接口就ok, 这样我们就符号依赖倒转原则
class Person {public void receive(Email email ) {System.out.println(email.getInfo());}
}
解决思路:引入一个抽象的接口IReceiver, 表示接收者, 这样Person类与接口IReceiver发生依赖
因为Email, WeiXin 等等属于接收的范围,他们各自实现IReceiver 接口就ok, 这样我们就符号依赖倒转原则
package com.atguigu.principle.inversion.improve;public class DependecyInversion {public static void main(String[] args) {//客户端无需改变Person person = new Person();person.receive(new Email());person.receive(new WeiXin());}}//定义接口
interface IReceiver {public String getInfo();
}class Email implements IReceiver {public String getInfo() {return "电子邮件信息: hello,world";}
}//增加微信
class WeiXin implements IReceiver {public String getInfo() {return "微信信息: hello,ok";}
}//方式2
class Person {//这里我们是对接口的依赖public void receive(IReceiver receiver ) {System.out.println(receiver.getInfo());}
}
package com.atguigu.principle.inversion.improve;public class DependencyPass {public static void main(String[] args) {// TODO Auto-generated method stubChangHong changHong = new ChangHong();
// OpenAndClose openAndClose = new OpenAndClose();
// openAndClose.open(changHong);//通过构造器进行依赖传递
// OpenAndClose openAndClose = new OpenAndClose(changHong);
// openAndClose.open();//通过setter方法进行依赖传递OpenAndClose openAndClose = new OpenAndClose();openAndClose.setTv(changHong);openAndClose.open();}}// 方式1: 通过接口传递实现依赖// 开关的接口
// interface IOpenAndClose {
// public void open(ITV tv); //抽象方法,接收接口
// }
//
// interface ITV { //ITV接口
// public void play();
// }
//
// class ChangHong implements ITV {
//
// @Override
// public void play() {
// // TODO Auto-generated method stub
// System.out.println("长虹电视机,打开");
// }
//
// }实现接口
// class OpenAndClose implements IOpenAndClose{
// public void open(ITV tv){
// tv.play();
// }
// }// 方式2: 通过构造方法依赖传递// interface IOpenAndClose {
// public void open(); //抽象方法
// }
// interface ITV { //ITV接口
// public void play();
// }
// class OpenAndClose implements IOpenAndClose{
// public ITV tv; //成员
// public OpenAndClose(ITV tv){ //构造器
// this.tv = tv;
// }
// public void open(){
// this.tv.play();
// }
// }// 方式3 , 通过setter方法传递interface IOpenAndClose {public void open(); // 抽象方法public void setTv(ITV tv);
}interface ITV { // ITV接口public void play();
}class OpenAndClose implements IOpenAndClose {private ITV tv;public void setTv(ITV tv) {this.tv = tv;}public void open() {this.tv.play();}
}class ChangHong implements ITV {@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("长虹电视机,打开");}}
这篇关于图解java设计模式_08_依赖倒转原则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!