我是靠谱客的博主 勤恳玉米,这篇文章主要介绍Java设计模式的六大设计原则以及他们的关系?单一职责原则开闭原则里氏代换原则依赖注入原则,现在分享给大家,希望可以做个参考。

文章目录

  • 单一职责原则
  • 开闭原则
  • 里氏代换原则
  • 依赖注入原则

模式名称定义
单一职责原则一个类只负责一个区域的相应职责
开闭原则软件实体对扩展放开,对修改关闭
里氏代换所有引用基类的地方可以透明的调用到他的子类
依赖注入抽象不应该依赖于细节,细节应该依赖于抽象
接口隔离使用多个专门的接口,不要使用总接口
合成复用原则复用时不要使用继承,尽量使用合成/复合关系
米迪特乐原则一个软件经历减少于其他实体的相互作用

单一职责原则

复制代码
1
2
3
单一职责原则,一个类、模块、方法承担一个区域的相应职责,避免多个职责相互交叉,导致修改其中一个职责的时候,影响其他职责。 __单一职责优点:__
  • 降低复杂度
  • 提高可读性,维护性
  • 降低变更引起的风险
    例子
    DemoTest这个例子是正确的单一职责原则
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class DemoTest{ public static void main(String[] args){ Demo d =new Demo(); d.run("大侠”); d.run("汉堡"); } } class Demo{ public void run(String active){ System.out.println(active) } }

例子
错误的单一职责原则

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ErrorTest{ public static void main(String[] args){ Errord =new Error(); d.superMan("大侠”); d.hanburger("汉堡"); } } class Error{ public void superMan(String active){ System.out.println(active) } public void hamburger(String active){ System.out.println(active) } }

开闭原则

复制代码
1
2
开闭原则是指一个实体如类、方法、模块应该只提供扩展,不提供修改。

优势:

  1. 保持产品的稳定性;
  2. 不影响原有代码测试运行;
  3. 使代码更具模块化,方便维护;
  4. 提高开发效率;

示例:

复制代码
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/** *顶层定义一个接口,获取家具信息的接口方法 */ public interface Furniture{ public String getName(); public double getPrice(); public String getBrand(); public double getDiscounts(); public String getMaterials(); public String getColor(); } public class Desk implements Furniture{ private double price; private String brand; private String materials; private String color; private double width; private double height; public Desk( double price, String brand, String materials, String color, double width, double height) { this.price = price; this.brand = brand; this.materials = materials; this.color = color; this.width = width; this.height = height; } @Override public double getPrice() { return price; } @Override public String getBrand() { return brand; } @Override public String getMaterials() { return materials; } @Override public String getColor() { return color; } @Override public double getWidth() { return width; } @Override public double getHeight() { return height; } } class BSPHDesk extends Desk { private double discount; public BSPHDesk(double discount, double price, String brand, String materials, String color, double width, double height) { super(price, brand, materials, color, width, height); this.discount = discount; } public double getDiscountPrice() { return getPrice() * discount; } } public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { BSPHDesk desk = new BSPHDesk(0.5,5000,"BSPH","实木","白色",500,600); desk.getDiscountPrice(); System.out.println("原有价格:"+desk.getPrice()+","+"折扣价格:"+desk.getDiscountPrice()); } }

里氏代换原则

复制代码
1
2
里氏代换原则 是指 一个实体如果使用父类,那么一定适用于子类,如果使用子类,那么他不能够使用父类。

优点

  1. 提高代码的重用性,子类拥有父类的方法和属性;
  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
36
37
public class LSPBase { /** * 父类方法运行 */ public void parentRun(){ System.out.println("这个是里氏代换 父类方法 "); } } public class LSPChild extends LSPBase{ @Override public void parentRun() { System.out.println("里氏代换覆盖"); } public void childRun(String content){ System.out.println("childRun里氏代换覆盖"+content); } } public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { LSPBase base = new LSPBase(); run(base);//错误 LSPChild child = new LSPChild(); baseRun(child);//非错误 } private void baseRun(LSPBase base){ base.parentRun(); } private void run(LSPChild child) { child.parentRun(); } }

依赖注入原则

复制代码
1
2
3
4
5
依赖注入原则 不要依赖于具体实现,要依赖于抽象。 1)高层模块不应该依赖底层模块,两者应该都依赖于抽象(抽象类和接口); 2)抽象(类或接口)不应该依赖于具体细节(实现类); 3)细节(具体类或接口)应该依赖于抽象

优点:

  1. 关注点分离;
  2. 应用程序类中的样板代码减少,因为所有用于初始化依赖性的工作都由注入器组件处理;
  3. 配件组件使应用易扩展
  4. 通过模拟对象来单元测试会很简单

缺点:

  1. 过度使用会导致很难维护;

不使用依赖注入

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class NonDependency { public void send(String message, String title) { System.out.println("这个消息内容:" + message + ",标题:" + title); } } public class NonDependencyApplication { NonDependency dependency = new NonDependency(); public void receive(String message, String title) { dependency.send(message, title); } } public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { NonDependencyApplication application = new NonDependencyApplication(); application.receive("这个市各消息","这个市各标题"); } }

使用依赖注入

复制代码
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
public interface Dependency { void send(String message, String title); } class EmailDependency implements Dependency { @Override public void send(String message, String title) { System.out.println("Email sent to "+message+ " with title="+title); } } public class SMSSDependency implements Dependency{ @Override public void send(String message, String title) { System.out.println("Email sent to "+message+ " with title="+title); } } public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { EmailDependency email= new EmailDependency(); email.send("这个是一个邮箱内容 ","这个是一个邮箱标题"); SMSSDependency smss = new SMSSDependency(); email.send("这个是一个smss内容 ","这个是一个smss标题"); } }

最后

以上就是勤恳玉米最近收集整理的关于Java设计模式的六大设计原则以及他们的关系?单一职责原则开闭原则里氏代换原则依赖注入原则的全部内容,更多相关Java设计模式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部