hooyantsing's Blog

P11 依赖倒转原则

字数统计: 510阅读时长: 2 min
2020/11/07

尚硅谷Java设计模式(图解+框架源码剖析)

依赖倒转原则

非依赖倒转原则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package xyz.hooy.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";
}
}

class Person{
public void receive(Email email){
System.out.println(email.getInfo());
}
}
依赖倒转原则
1
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
package xyz.hooy.principle.inversion;

public class DependecyInversion {
public static void main(String[] args) {
Person person = new Person();
person.receive(new Email());
person.receive(new Wechat());
}
}

interface IReceive{
String getInfo();
}

class Email implements IReceive{
@Override
public String getInfo(){
return "邮件信息:hello,world";
}
}

class Wechat implements IReceive{
@Override
public String getInfo(){
return "微信信息:hello,world";
}
}

class Person{
// 依赖接口
public void receive(IReceive iReceive){
System.out.println(iReceive.getInfo());
}
}

依赖关系传递的三种方式

1.接口传递
1
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
package xyz.hooy.principle.inversion;

public class Dependecy {
public static void main(String[] args) {
OpenAndClose openAndClose = new OpenAndClose();
Changhong changhong = new Changhong();
openAndClose.open(changhong);
}
}

interface IOpenAndClose {
void open(ITV tv);
}

interface ITV {
void play();
}

class OpenAndClose implements IOpenAndClose {
// 接口传递依赖
@Override
public void open(ITV tv) {
tv.play();
}
}

class Changhong implements ITV {
@Override
public void play() {
System.out.println("长虹电视开机");
}
}
2.构造方法传递
1
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
package xyz.hooy.principle.inversion;

public class Dependecy {
public static void main(String[] args) {
OpenAndClose openAndClose = new OpenAndClose(new Changhong());
openAndClose.open();
}
}

interface IOpenAndClose {
void open();
}

interface ITV {
void play();
}

class OpenAndClose implements IOpenAndClose {
public ITV tv;
// 构造方法传递依赖
public OpenAndClose(ITV tv){
this.tv = tv;
}
@Override
public void open() {
this.tv.play();
}
}

class Changhong implements ITV {
@Override
public void play() {
System.out.println("长虹电视开机");
}
}
3.setter方法传递
1
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
package xyz.hooy.principle.inversion;

public class Dependecy {
public static void main(String[] args) {
OpenAndClose openAndClose = new OpenAndClose();
openAndClose.setTV(new Changhong());
openAndClose.open();
}
}

interface IOpenAndClose {
void open();
void setTV(ITV tv);
}

interface ITV {
void play();
}

class OpenAndClose implements IOpenAndClose {
public ITV tv;

@Override
public void open() {
this.tv.play();
}

// setter方法传递依赖
@Override
public void setTV(ITV tv) {
this.tv = tv;
}
}

class Changhong implements ITV {
@Override
public void play() {
System.out.println("长虹电视开机");
}
}
CATALOG
  1. 1. 依赖倒转原则
    1. 1.1. 非依赖倒转原则
    2. 1.2. 依赖倒转原则
  2. 2. 依赖关系传递的三种方式
    1. 2.1. 1.接口传递
    2. 2.2. 2.构造方法传递
    3. 2.3. 3.setter方法传递