Skip to content

Commit 461815b

Browse files
committed
Translate template method pattern
1 parent 7b0dcce commit 461815b

File tree

6 files changed

+109
-8
lines changed

6 files changed

+109
-8
lines changed
14.4 KB
Binary file not shown.

โ€ŽDesign-Patterns-CN.playground/Pages/Behavioral.xcplaygroundpage/Contents.swiftโ€Ž

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,55 @@ let isRachelAndroid = deckard.testIfAndroid(rachel)
621621
let gaff = BladeRunner(test: GeneticTest())
622622
let isDeckardAndroid = gaff.testIfAndroid(rachel)
623623
/*:
624+
๐Ÿ“ ๆจกๆฟๆ–นๆณ•ๆจกๅผ
625+
-----------
626+
627+
ๆจกๆฟๆ–นๆณ•ๆจกๅผๆ˜ฏไธ€็ง่กŒไธบ่ฎพ่ฎกๆจกๅผ๏ผŒ ๅฎƒ้€š่ฟ‡็ˆถ็ฑป/ๅ่ฎฎไธญๅฎšไน‰ไบ†ไธ€ไธช็ฎ—ๆณ•็š„ๆก†ๆžถ๏ผŒ ๅ…่ฎธๅญ็ฑป/ๅ…ทไฝ“ๅฎž็Žฐๅฏน่ฑกๅœจไธไฟฎๆ”น็ป“ๆž„็š„ๆƒ…ๅ†ตไธ‹้‡ๅ†™็ฎ—ๆณ•็š„็‰นๅฎšๆญฅ้ชคใ€‚
628+
629+
### ็คบไพ‹๏ผš
630+
*/
631+
protocol Garden {
632+
func prepareSoil()
633+
func plantSeeds()
634+
func waterPlants()
635+
func prepareGarden()
636+
}
637+
638+
extension Garden {
639+
640+
func prepareGarden() {
641+
prepareSoil()
642+
plantSeeds()
643+
waterPlants()
644+
}
645+
}
646+
647+
final class RoseGarden: Garden {
648+
649+
func prepare() {
650+
prepareGarden()
651+
}
652+
653+
func prepareSoil() {
654+
print ("prepare soil for rose garden")
655+
}
656+
657+
func plantSeeds() {
658+
print ("plant seeds for rose garden")
659+
}
660+
661+
func waterPlants() {
662+
print ("water the rose garden")
663+
}
664+
}
665+
666+
/*:
667+
### ็”จๆณ•
668+
*/
669+
670+
let roseGarden = RoseGarden()
671+
roseGarden.prepare()
672+
/*:
624673
๐Ÿƒ ่ฎฟ้—ฎ่€…๏ผˆVisitor๏ผ‰
625674
--------------
626675

โ€ŽREADME-CN.mdโ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ print("ๆ‚จๅฅฝ๏ผ")
2929
| [๐Ÿ‰ ็Šถๆ€ State](#-็Šถๆ€state) | | [๐Ÿฌ ่™šๆ‹Ÿไปฃ็† Virtual Proxy](#-่™šๆ‹Ÿไปฃ็†virtual-proxy) |
3030
| [๐Ÿ’ก ็ญ–็•ฅ Strategy](#-็ญ–็•ฅstrategy) | | |
3131
| [๐Ÿƒ ่ฎฟ้—ฎ่€… Visitor](#-่ฎฟ้—ฎ่€…visitor) | | |
32+
| [๐Ÿ“ ๆจกๆฟๆ–นๆณ• Templdate Method](#-template-method) | | |
3233

3334

3435
่กŒไธบๅž‹ๆจกๅผ
@@ -687,12 +688,12 @@ let gaff = BladeRunner(test: GeneticTest())
687688
let isDeckardAndroid = gaff.testIfAndroid(rachel)
688689
```
689690

690-
๐Ÿ“ Template Method
691+
๐Ÿ“ ๆจกๆฟๆ–นๆณ•ๆจกๅผ
691692
-----------
692693

693-
The template method pattern defines the steps of an algorithm and allows the redefinition of one or more of these steps. In this way, the template method protects the algorithm, the order of execution and provides abstract methods that can be implemented by concrete types.
694+
ๆจกๆฟๆ–นๆณ•ๆจกๅผๆ˜ฏไธ€็ง่กŒไธบ่ฎพ่ฎกๆจกๅผ๏ผŒ ๅฎƒ้€š่ฟ‡็ˆถ็ฑป/ๅ่ฎฎไธญๅฎšไน‰ไบ†ไธ€ไธช็ฎ—ๆณ•็š„ๆก†ๆžถ๏ผŒ ๅ…่ฎธๅญ็ฑป/ๅ…ทไฝ“ๅฎž็Žฐๅฏน่ฑกๅœจไธไฟฎๆ”น็ป“ๆž„็š„ๆƒ…ๅ†ตไธ‹้‡ๅ†™็ฎ—ๆณ•็š„็‰นๅฎšๆญฅ้ชคใ€‚
694695

695-
### Example
696+
### ็คบไพ‹๏ผš
696697

697698
```swift
698699
protocol Garden {
@@ -732,7 +733,7 @@ final class RoseGarden: Garden {
732733

733734
```
734735

735-
### Usage
736+
### ็”จๆณ•
736737

737738
```swift
738739

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*:
2+
๐Ÿ“ ๆจกๆฟๆ–นๆณ•ๆจกๅผ
3+
-----------
4+
5+
ๆจกๆฟๆ–นๆณ•ๆจกๅผๆ˜ฏไธ€็ง่กŒไธบ่ฎพ่ฎกๆจกๅผ๏ผŒ ๅฎƒ้€š่ฟ‡็ˆถ็ฑป/ๅ่ฎฎไธญๅฎšไน‰ไบ†ไธ€ไธช็ฎ—ๆณ•็š„ๆก†ๆžถ๏ผŒ ๅ…่ฎธๅญ็ฑป/ๅ…ทไฝ“ๅฎž็Žฐๅฏน่ฑกๅœจไธไฟฎๆ”น็ป“ๆž„็š„ๆƒ…ๅ†ตไธ‹้‡ๅ†™็ฎ—ๆณ•็š„็‰นๅฎšๆญฅ้ชคใ€‚
6+
7+
### ็คบไพ‹๏ผš
8+
*/
9+
protocol Garden {
10+
func prepareSoil()
11+
func plantSeeds()
12+
func waterPlants()
13+
func prepareGarden()
14+
}
15+
16+
extension Garden {
17+
18+
func prepareGarden() {
19+
prepareSoil()
20+
plantSeeds()
21+
waterPlants()
22+
}
23+
}
24+
25+
final class RoseGarden: Garden {
26+
27+
func prepare() {
28+
prepareGarden()
29+
}
30+
31+
func prepareSoil() {
32+
print ("prepare soil for rose garden")
33+
}
34+
35+
func plantSeeds() {
36+
print ("plant seeds for rose garden")
37+
}
38+
39+
func waterPlants() {
40+
print ("water the rose garden")
41+
}
42+
}
43+
44+
/*:
45+
### ็”จๆณ•
46+
*/
47+
48+
let roseGarden = RoseGarden()
49+
roseGarden.prepare()

โ€Žsource-cn/contentsReadme.mdโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
| [๐Ÿ‰ ็Šถๆ€ State](#-็Šถๆ€state) | | [๐Ÿฌ ่™šๆ‹Ÿไปฃ็† Virtual Proxy](#-่™šๆ‹Ÿไปฃ็†virtual-proxy) |
1414
| [๐Ÿ’ก ็ญ–็•ฅ Strategy](#-็ญ–็•ฅstrategy) | | |
1515
| [๐Ÿƒ ่ฎฟ้—ฎ่€… Visitor](#-่ฎฟ้—ฎ่€…visitor) | | |
16+
| [๐Ÿ“ ๆจกๆฟๆ–นๆณ• Templdate Method](#-template-method) | | |
1617

โ€Žsource/contentsReadme.mdโ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
| [๐Ÿ Chain Of Responsibility](#-chain-of-responsibility) | [๐ŸŒฐ Abstract Factory](#-abstract-factory) | [๐Ÿ”Œ Adapter](#-adapter) |
77
| [๐Ÿ‘ซ Command](#-command) | [๐Ÿ‘ท Builder](#-builder) | [๐ŸŒ‰ Bridge](#-bridge) |
88
| [๐ŸŽถ Interpreter](#-interpreter) | [๐Ÿญ Factory Method](#-factory-method) | [๐ŸŒฟ Composite](#-composite) |
9-
| [๐Ÿซ Iterator](#-iterator) | [๐Ÿ”‚ Monostate](#-monostate) | [๐Ÿง Decorator](#-decorator) |
10-
| [๐Ÿ’ Mediator](#-mediator) | [๐Ÿƒ Prototype](#-prototype) | [๐ŸŽ Faรงade](#-fa-ade) |
9+
| [๐Ÿซ Iterator](#-iterator) | [๐Ÿ”‚ Monostate](#-monostate) | [๐Ÿง Decorator](#-decorator) |
10+
| [๐Ÿ’ Mediator](#-mediator) | [๐Ÿƒ Prototype](#-prototype) | [๐ŸŽ Faรงade](#-fa-ade) |
1111
| [๐Ÿ’พ Memento](#-memento) | [๐Ÿ’ Singleton](#-singleton) | [๐Ÿƒ Flyweight](#-flyweight) |
12-
| [๐Ÿ‘“ Observer](#-observer) | | [โ˜” Protection Proxy](#-protection-proxy) |
13-
| [๐Ÿ‰ State](#-state) | | [๐Ÿฌ Virtual Proxy](#-virtual-proxy) |
12+
| [๐Ÿ‘“ Observer](#-observer) | | [โ˜” Protection Proxy](#-protection-proxy) |
13+
| [๐Ÿ‰ State](#-state) | | [๐Ÿฌ Virtual Proxy](#-virtual-proxy) |
1414
| [๐Ÿ’ก Strategy](#-strategy) | | |
1515
| [๐Ÿƒ Visitor](#-visitor) | | |
16+
| [๐Ÿ“ Template Method](#-template-method) | | |
1617

0 commit comments

Comments
ย (0)