Skip to content

Commit 06b70f6

Browse files
committed
upload header and demo
1 parent 0249cf1 commit 06b70f6

File tree

306 files changed

+14644
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

306 files changed

+14644
-2
lines changed

README.md

Lines changed: 241 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,241 @@
1-
# object-oriented-design
2-
Object-Oriented design principles and patterns.
1+
2+
![](res/header.png)
3+
4+
Object-Oriented design:
5+
- Chapter 1: 面向对象设计原则(Object Oriented Design Principles)
6+
- Chapter 2: 面向对象设计模式(Object Oriented Design Patterns)
7+
8+
9+
# Chapter 1:面向对象设计原则(Object Oriented Design Principles)
10+
11+
12+
缩写 | 英文名称 | 中文名称
13+
---| ----| -----
14+
SRP | Single Responsibility Principle | 单一职责原则
15+
OCP | Open Close Principle | 开闭原则
16+
LSP | Liskov Substitution Principle | 里氏替换原则
17+
LoD | Law of Demeter ( Least Knowledge Principle) | 迪米特法则(最少知道原则)
18+
ISP | Interface Segregation Principle | 接口分离原则
19+
DIP | Dependency Inversion Principle | 依赖倒置原则
20+
21+
22+
## 原则一:开闭原则(Open Close Principle)
23+
24+
25+
### 定义
26+
27+
> Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
28+
29+
即:一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。
30+
31+
### 定义的解读
32+
33+
- 用抽象构建框架,用实现扩展细节。
34+
- 不以改动原有类的方式来实现新需求,而是应该以实现事先抽象出来的接口(或具体类继承抽象类)的方式来实现。
35+
36+
37+
#### UML 类图对比
38+
39+
未实践开闭原则:
40+
![未实践开闭原则](http://oih3a9o4n.bkt.clouddn.com/OCP11.png)
41+
42+
实践了开闭原则:
43+
![实践了开闭原则](http://oih3a9o4n.bkt.clouddn.com/OCP2.png)
44+
45+
46+
#### Demo
47+
48+
1. 未实践开闭原则:[]()
49+
2. 实践了开闭原则:[]()
50+
51+
52+
53+
## 原则二:单一职责原则(Single Responsibility Principle)
54+
55+
56+
### 定义
57+
58+
>A class should have a single responsibility, where a responsibility is nothing but a reason to change.
59+
60+
即:一个类只允许有一个职责,即只有一个导致该类变更的原因。
61+
62+
63+
### 定义的解读
64+
65+
- 类职责的变化往往就是导致类变化的原因:也就是说如果一个类具有多种职责,就会有多种导致这个类变化的原因,从而导致这个类的维护变得困难。
66+
67+
- 往往在软件开发中随着需求的不断增加,可能会给原来的类添加一些本来不属于它的一些职责,从而违反了单一职责原则。如果我们发现当前类的职责不仅仅有一个,就应该将本来不属于该类真正的职责分离出去。
68+
- 不仅仅是类,函数(方法)也要遵循单一职责原则,即:一个函数(方法)只做一件事情。如果发现一个函数(方法)里面有不同的任务,则需要将不同的任务以另一个函数(方法)的形式分离出去。
69+
70+
71+
72+
73+
#### UML 类图对比
74+
75+
未实践单一职责原则:
76+
![未实践单一职责原则](http://oih3a9o4n.bkt.clouddn.com/SRP11.png)
77+
78+
79+
实践了单一职责原则:
80+
![实践了单一职责原则](http://oih3a9o4n.bkt.clouddn.com/SRP2.png)
81+
82+
#### Demo
83+
84+
1. 未实践单一职责原则:[]()
85+
2. 实践了单一职责原则:[]()
86+
87+
88+
89+
## 原则三:依赖倒置原则(Dependency Inversion Principle)
90+
91+
### 定义
92+
93+
>- Depend upon Abstractions. Do not depend upon concretions.
94+
>- Abstractions should not depend upon details. Details should depend upon abstractions
95+
>- High-level modules should not depend on low-level modules. Both should depend on abstractions.
96+
97+
即:
98+
99+
- 依赖抽象,而不是依赖实现。
100+
- 抽象不应该依赖细节;细节应该依赖抽象。
101+
- 高层模块不能依赖低层模块,二者都应该依赖抽象。
102+
103+
104+
### 定义解读
105+
106+
107+
- 针对接口编程,而不是针对实现编程。
108+
- 尽量不要从具体的类派生,而是以继承抽象类或实现接口来实现。
109+
- 关于高层模块与低层模块的划分可以按照决策能力的高低进行划分。业务层自然就处于上层模块,逻辑层和数据层自然就归类为底层。
110+
111+
112+
113+
#### UML 类图对比
114+
115+
未实践依赖倒置原则:
116+
117+
![未实践依赖倒置原则](http://oih3a9o4n.bkt.clouddn.com/DIP1.png)
118+
119+
120+
实践了依赖倒置原则:
121+
![实践了依赖倒置原则](http://oih3a9o4n.bkt.clouddn.com/DIP22.png)
122+
123+
#### Demo
124+
125+
126+
1. 未实践依赖倒置原则:[]()
127+
2. 实践了依赖倒置原则:[]()
128+
129+
130+
## 原则四:接口分离原则(Interface Segregation Principle)
131+
132+
133+
### 定义
134+
135+
136+
>Many client specific interfaces are better than one general purpose interface.
137+
138+
即:多个特定的客户端接口要好于一个通用性的总接口。
139+
140+
141+
### 定义解读
142+
143+
- 客户端不应该依赖它不需要实现的接口。
144+
- 不建立庞大臃肿的接口,应尽量细化接口,接口中的方法应该尽量少。
145+
146+
需要注意的是:接口的粒度也不能太小。如果过小,则会造成接口数量过多,使设计复杂化。
147+
148+
149+
150+
151+
#### UML 类图对比
152+
153+
未实践接口分离原则:
154+
![未实践接口分离原则](http://oih3a9o4n.bkt.clouddn.com/ISP1.png)
155+
156+
157+
实践了接口分离原则:
158+
![实践了接口分离原则](http://oih3a9o4n.bkt.clouddn.com/ISP2.png)
159+
160+
>通过遵守接口分离原则,接口的设计变得更加简洁,而且各种客户类不需要实现自己不需要实现的接口。
161+
162+
#### Demo
163+
164+
1. 未实践接口分离原则:[]()
165+
2. 实践了接口分离原则:[]()
166+
167+
## 原则五:迪米特法则(Law of Demeter)
168+
169+
170+
### 定义
171+
172+
>You only ask for objects which you directly need.
173+
174+
即:一个对象应该对尽可能少的对象有接触,也就是只接触那些真正需要接触的对象。
175+
176+
### 定义解读
177+
178+
179+
- 迪米特原则也叫做最少知道原则(Least Know Principle), 一个类应该只和它的成员变量,方法的输入,返回参数中的类作交流,而不应该引入其他的类(间接交流)。
180+
181+
182+
#### UML 类图对比
183+
184+
未实践迪米特法则:
185+
![未实践迪米特法则](http://oih3a9o4n.bkt.clouddn.com/LOD111.png)
186+
187+
188+
189+
实践了迪米特法则:
190+
![实践了迪米特法则](http://oih3a9o4n.bkt.clouddn.com/LOD2.png)
191+
192+
>很明显,在较好的设计的 UML 类图里面,没有了``Client````GasEngine``的依赖,耦合性降低。
193+
194+
#### Demo
195+
196+
1. 未实践迪米特法则:[]()
197+
2. 实践了迪米特法则:[]()
198+
199+
## 原则六:里氏替换原则(Liskov Substitution Principle)
200+
201+
202+
### 定义
203+
204+
>In a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e. an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of the program (correctness, task performed, etc.)
205+
206+
207+
即:所有引用基类的地方必须能透明地使用其子类的对象,也就是说子类对象可以替换其父类对象,而程序执行效果不变。
208+
209+
210+
### 定义的解读
211+
212+
在继承体系中,子类中可以增加自己特有的方法,也可以实现父类的抽象方法,但是不能重写父类的非抽象方法,否则该继承关系就不是一个正确的继承关系。
213+
214+
215+
216+
#### UML 类图对比
217+
218+
未实践里氏替换原则:
219+
220+
![未实践里氏替换原则](http://oih3a9o4n.bkt.clouddn.com/LSP11.png)
221+
222+
223+
224+
实践了里氏替换原则:
225+
![实践了里氏替换原则](http://oih3a9o4n.bkt.clouddn.com/LSP22.png)
226+
227+
#### Demo
228+
229+
1. 未实践里氏替换原则:[]()
230+
2. 实践了里氏替换原则:[]()
231+
232+
# Chapter 2:面向对象设计模式(Object Oriented Design Patterns)
233+
234+
Coming soon...
235+
236+
237+
# License
238+
239+
This repository is released under [MIT License](https://github.com/knightsj/object-oriented-design/blob/master/LICENSE)
240+
241+

0 commit comments

Comments
 (0)