Skip to content

Commit c6fa2cb

Browse files
committed
add demo link and README of demo code
1 parent a0de547 commit c6fa2cb

File tree

59 files changed

+1844
-19
lines changed

Some content is hidden

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

59 files changed

+1844
-19
lines changed

README.md

+26-19
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,21 @@
4646
- [2.26 享元模式](<https://github.com/knightsj/object-oriented-design#226-%E4%BA%AB%E5%85%83%E6%A8%A1%E5%BC%8F>)
4747

4848
- [2.3 行为型设计模式](https://github.com/knightsj/object-oriented-design#23-%E8%A1%8C%E4%B8%BA%E5%9E%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F)
49-
- [2.31 模板方法模式]
50-
- [2.32 策略模式]
51-
- [2.33 责任链模式]
52-
- [2.34 状态模式]
53-
- [2.35 命令模式]
54-
- [2.36 观察者模式]
55-
- [2.37 中介者模式]
56-
49+
50+
- [2.31 模板方法模式](https://github.com/knightsj/object-oriented-design#231-%E6%A8%A1%E6%9D%BF%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F)
51+
52+
- [2.32 策略模式](https://github.com/knightsj/object-oriented-design#232-%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F)
53+
54+
- [2.33 责任链模式](https://github.com/knightsj/object-oriented-design#233-%E8%B4%A3%E4%BB%BB%E9%93%BE%E6%A8%A1%E5%BC%8F)
55+
56+
- [2.34 状态模式](https://github.com/knightsj/object-oriented-design#234-%E7%8A%B6%E6%80%81%E6%A8%A1%E5%BC%8F)
57+
58+
- [2.35 命令模式](https://github.com/knightsj/object-oriented-design#235-%E5%91%BD%E4%BB%A4%E6%A8%A1%E5%BC%8F)
59+
60+
- [2.36 观察者模式](https://github.com/knightsj/object-oriented-design#236-%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F)
61+
62+
- [2.37 中介者模式](https://github.com/knightsj/object-oriented-design#237-%E4%B8%AD%E4%BB%8B%E8%80%85%E6%A8%A1%E5%BC%8F)
63+
5764
# Chapter 1:面向对象设计原则
5865

5966

@@ -679,7 +686,7 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
679686

680687

681688

682-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
689+
- Demo Link: [Template Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B13%5D.%20Template%20Pattern%20Demo)
683690

684691
- Demo UML:
685692

@@ -708,7 +715,7 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
708715

709716

710717

711-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
718+
- Demo Link: [Strategy Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B14%5D.%20Strategy%20Pattern%20Demo)
712719

713720
- Demo UML:
714721

@@ -736,15 +743,16 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
736743

737744

738745

739-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
740-
746+
- Demo Link:
747+
- Before refactoring:[Chain of Responsibilty Pattern Demo (before refactoring)](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B15%5D.%20Chain%20of%20Responsibilty%20Pattern%20Demo%20(before%20refactoring))
748+
- After refactoring:[Chain of Responsibilty Pattern Demo (after refactoring)](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B16%5D.%20Chain%20of%20Responsibilty%20Pattern%20Demo%20(after%20refactoring))
741749
- Demo UML:
742750

743751
- Before refactoring:
744-
![责任链模式代码示例类图一](http://jknight-blog.oss-cn-shanghai.aliyuncs.com/design-pattern-behavior/cp_2.png)
752+
![责任链模式代码示例类图一](http://jknight-blog.oss-cn-shanghai.aliyuncs.com/design-pattern-behavior/cp_2.png)
745753

746754
- After refactoring:
747-
![责任链模式代码示例类图二](http://jknight-blog.oss-cn-shanghai.aliyuncs.com/design-pattern-behavior/cp_3.png)
755+
![责任链模式代码示例类图二](http://jknight-blog.oss-cn-shanghai.aliyuncs.com/design-pattern-behavior/cp_3.png)
748756

749757

750758

@@ -769,8 +777,7 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
769777

770778

771779

772-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
773-
780+
- Demo Link: [State Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B17%5D.%20State%20Pattern%20Demo)
774781
- Demo UML:
775782

776783
![状态模式代码示例类图](http://jknight-blog.oss-cn-shanghai.aliyuncs.com/design-pattern-behavior/sp_2.png)
@@ -795,7 +802,7 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
795802

796803

797804

798-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
805+
- Demo Link: [Command Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B18%5D.%20Command%20Pattern%20Demo)
799806

800807
- Demo UML:
801808

@@ -823,7 +830,7 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
823830

824831

825832

826-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
833+
- Demo Link: [Obsever Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B19%5D.%20Observer%20Pattern%20Demo)
827834

828835
- Demo UML:
829836

@@ -848,7 +855,7 @@ DIP | Dependency Inversion Principle | 依赖倒置原则
848855

849856

850857

851-
- Demo Link: [Flyweight Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B12%5D.%20Flyweight%20Pattern%20Demo)
858+
- Demo Link: [Mediator Pattern Demo](https://github.com/knightsj/object-oriented-design/tree/master/%5B2%5D.%20Ojbect-Oriented%20design%20pattern%20Demo/%5B20%5D.Mediator%20Pattern%20Demo)
852859

853860
- Demo UML:
854861

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# 模板方法模式代码示例
2+
3+
4+
首先我们创建算法类``HotDrink``
5+
6+
```objc
7+
//================== HotDrink.h ==================
8+
9+
@interface HotDrink : NSObject
10+
11+
- (void)makingProcess;
12+
13+
- (void)addMainMaterial;
14+
15+
- (void)addIngredients;
16+
17+
@end
18+
19+
20+
21+
//================== HotDrink.m ==================
22+
23+
@implementation HotDrink
24+
25+
- (void)makingProcess{
26+
27+
NSLog(@" ===== Begin to making %@ ===== ", NSStringFromClass([self class]));
28+
29+
[self boilWater];
30+
[self addMainMaterial];
31+
[self addIngredients];
32+
}
33+
34+
35+
- (void)prepareHotWater{
36+
37+
NSLog(@"prepare hot water");
38+
}
39+
40+
41+
- (void)addMainMaterial{
42+
43+
NSLog(@"implemetation by subClasses");
44+
}
45+
46+
47+
- (void)addIngredients{
48+
49+
NSLog(@"implemetation by subClasses");
50+
}
51+
52+
53+
@end
54+
```
55+
56+
57+
``HotDrink``向外部暴露了一个制作过程的接口``makingProcess``,这个接口内部调用了热饮的所有制作步骤方法:
58+
59+
```objc
60+
- (void)makingProcess{
61+
62+
//准备热水
63+
[self prepareHotWater];
64+
65+
//添加主成分
66+
[self addMainMaterial];
67+
68+
//添加辅助成分
69+
[self addIngredients];
70+
}
71+
```
72+
73+
74+
``HotDrink``只向外暴露了**这三个步骤中的两个**需要子类按照自己方式实现的接口:
75+
76+
```objc
77+
//添加主成分
78+
- (void)addMainMaterial;
79+
80+
//添加辅助成分
81+
- (void)addIngredients;
82+
```
83+
84+
因为热饮的第一步都是一致的(准备热水),所以第一步骤的接口没有暴露出来给子类实现,而是直接在当前类实现了,这也就是模板方法的一个可以复用代码的优点。
85+
86+
OK,我们现在创建好了算法类,那么根据上面的需求,我们接着创建三个具体算法类:
87+
88+
- ``HotDrinkTea`` : 热茶
89+
- ``HotDrinkLatte`` : 热拿铁
90+
- ``HotDrinkAmericano ``: 热美式
91+
92+
93+
94+
```objc
95+
//================== HotDrinkTea.h ==================
96+
97+
@interface HotDrinkTea : HotDrink
98+
99+
@end
100+
101+
102+
103+
//================== HotDrinkTea.m ==================
104+
105+
@implementation HotDrinkTea
106+
107+
108+
- (void)addMainMaterial{
109+
110+
NSLog(@"add tea leaf");
111+
}
112+
113+
114+
- (void)addIngredients{
115+
116+
NSLog(@"add nothing");
117+
}
118+
119+
120+
@end
121+
```
122+
123+
热茶在``addMainMaterial``步骤里面是添加了茶叶,而在``addIngredients``步骤没有做任何事情(这里先假定是纯的茶叶)。
124+
125+
类似地,我们看一下两种热咖啡的实现。首先是热拿铁``HotDrinkLatte``:
126+
127+
```objc
128+
//================== HotDrinkLatte.h ==================
129+
130+
@interface HotDrinkLatte : HotDrink
131+
132+
@end
133+
134+
135+
136+
//================== HotDrinkLatte.m ==================
137+
138+
@implementation HotDrinkLatte
139+
140+
- (void)addMainMaterial{
141+
142+
NSLog(@"add ground coffee");
143+
}
144+
145+
146+
- (void)addIngredients{
147+
148+
NSLog(@"add milk");
149+
}
150+
151+
152+
@end
153+
```
154+
155+
热拿铁在``addMainMaterial``步骤里面是添加了咖啡粉,而在``addIngredients``步骤添加了牛奶。
156+
157+
下面再看一下热美式``HotDrinkAmericano``
158+
159+
```objc
160+
//================== HotDrinkAmericano.h ==================
161+
162+
@interface HotDrinkAmericano : HotDrink
163+
164+
@end
165+
166+
167+
168+
//================== HotDrinkAmericano.m ==================
169+
170+
@implementation HotDrinkAmericano
171+
172+
- (void)addMainMaterial{
173+
174+
NSLog(@"add ground coffee");
175+
}
176+
177+
178+
- (void)addIngredients{
179+
180+
NSLog(@"add nothing");
181+
}
182+
183+
@end
184+
```
185+
186+
热美式在``addMainMaterial``步骤里面是添加了咖啡粉,而在``addIngredients``步骤没有做任何事,因为美式就是纯的咖啡,理论上除了水和咖啡不需要添加任何其他东西。
187+
188+
189+
到现在三种热饮类创建好了,我们现在分别制作这三种热饮,并看一下日至输出:
190+
191+
192+
```
193+
===== Begin to making HotDrinkTea =====
194+
prepare hot water
195+
add tea leaf
196+
add nothing
197+
===== Begin to making HotDrinkLatte =====
198+
prepare hot water
199+
add ground coffee
200+
add milk
201+
===== Begin to making HotDrinkAmericano =====
202+
prepare hot water
203+
add ground coffee
204+
add nothing
205+
```
206+
207+
上面的日至输出准确无误地反映了我们所定义的这三种热饮制作过程:
208+
209+
- 热茶:准备热水 + 茶叶
210+
- 热拿铁:准备热水 + 咖啡 + 牛奶
211+
- 热美式:准备热水 + 咖啡

0 commit comments

Comments
 (0)