Skip to content

Commit f3dd327

Browse files
committed
Rebase chinese from BinBoy and link to README-CN.md.
1 parent d635d79 commit f3dd327

File tree

13 files changed

+1836
-367
lines changed

13 files changed

+1836
-367
lines changed

Design-Patterns-CN.playground.zip

1.38 KB
Binary file not shown.

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

+74-72
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
/*:
22

3-
Behavioral
4-
==========
5-
6-
>In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
7-
>
8-
>**Source:** [wikipedia.org](http://en.wikipedia.org/wiki/Behavioral_pattern)
9-
10-
## Table of Contents
11-
12-
* [Behavioral](Behavioral)
13-
* [Creational](Creational)
14-
* [Structural](Structural)
15-
3+
行为型模式
4+
========
5+
6+
>在软件工程中, 行为型模式为设计模式的一种类型,用来识别对象之间的常用交流模式并加以实现。如此,可在进行这些交流活动时增强弹性。
7+
>
8+
>**来源:** [维基百科](https://zh.wikipedia.org/wiki/%E8%A1%8C%E7%82%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F)
9+
10+
## 目录
11+
12+
* [行为型模式](Behavioral)
13+
* [创建型模式](Creational)
14+
* [结构型模式](Structural)
1615
*/
1716
import Foundation
1817
/*:
19-
🐝 Chain Of Responsibility
20-
--------------------------
18+
🐝 责任链(Chain Of Responsibility
19+
------------------------------
2120

22-
The chain of responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.
21+
责任链模式在面向对象程式设计里是一种软件设计模式,它包含了一些命令对象和一系列的处理对象。每一个处理对象决定它能处理哪些命令对象,它也知道如何将它不能处理的命令对象传递给该链中的下一个处理对象。
2322

24-
### Example:
23+
### 示例:
2524
*/
2625

2726
protocol Withdrawing {
@@ -99,25 +98,26 @@ final class ATM: Withdrawing {
9998
}
10099
}
101100
/*:
102-
### Usage
103-
*/
104-
// Create piles of money and link them together 10 < 20 < 50 < 100.**
101+
### 用法
102+
*/
103+
// 创建一系列的钱堆,并将其链接起来:10<20<50<100
105104
let ten = MoneyPile(value: 10, quantity: 6, next: nil)
106105
let twenty = MoneyPile(value: 20, quantity: 2, next: ten)
107106
let fifty = MoneyPile(value: 50, quantity: 2, next: twenty)
108107
let hundred = MoneyPile(value: 100, quantity: 1, next: fifty)
109108

110-
// Build ATM.
109+
// 创建 ATM 实例
111110
var atm = ATM(hundred: hundred, fifty: fifty, twenty: twenty, ten: ten)
112111
atm.withdraw(amount: 310) // Cannot because ATM has only 300
113112
atm.withdraw(amount: 100) // Can withdraw - 1x100
114113
/*:
115-
👫 Command
116-
----------
117-
118-
The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.
119-
120-
### Example:
114+
👫 命令(Command)
115+
------------
116+
命令模式是一种设计模式,它尝试以对象来代表实际行动。命令对象可以把行动(action) 及其参数封装起来,于是这些行动可以被:
117+
* 重复多次
118+
* 取消(如果该对象有实现的话)
119+
* 取消后又再重做
120+
### 示例:
121121
*/
122122
protocol DoorCommand {
123123
func execute() -> String
@@ -165,20 +165,20 @@ final class HAL9000DoorsOperations {
165165
}
166166
}
167167
/*:
168-
### Usage:
168+
### 用法
169169
*/
170170
let podBayDoors = "Pod Bay Doors"
171171
let doorModule = HAL9000DoorsOperations(doors:podBayDoors)
172172

173173
doorModule.open()
174174
doorModule.close()
175175
/*:
176-
🎶 Interpreter
177-
--------------
176+
🎶 解释器(Interpreter
177+
------------------
178178

179-
The interpreter pattern is used to evaluate sentences in a language.
179+
给定一种语言,定义他的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中句子。
180180

181-
### Example
181+
### 示例:
182182
*/
183183

184184
protocol IntegerExpression {
@@ -246,7 +246,7 @@ final class AddExpression: IntegerExpression {
246246
}
247247
}
248248
/*:
249-
### Usage
249+
### 用法
250250
*/
251251
var context = IntegerContext()
252252

@@ -262,12 +262,12 @@ context.assign(expression: c, value: 3)
262262

263263
var result = expression.evaluate(context)
264264
/*:
265-
🍫 Iterator
266-
-----------
267-
268-
The iterator pattern is used to provide a standard interface for traversing a collection of items in an aggregate object without the need to understand its underlying structure.
265+
🍫 迭代器(Iterator)
266+
---------------
269267

270-
### Example:
268+
迭代器模式可以让用户通过特定的接口巡访容器中的每一个元素而不用了解底层的实现。
269+
270+
### 示例:
271271
*/
272272
struct Novella {
273273
let name: String
@@ -298,20 +298,20 @@ extension Novellas: Sequence {
298298
}
299299
}
300300
/*:
301-
### Usage
301+
### 用法
302302
*/
303303
let greatNovellas = Novellas(novellas: [Novella(name: "The Mist")] )
304304

305305
for novella in greatNovellas {
306306
print("I've read: \(novella)")
307307
}
308308
/*:
309-
💐 Mediator
310-
-----------
309+
💐 中介者(Mediator
310+
---------------
311311

312-
The mediator pattern is used to reduce coupling between classes that communicate with each other. Instead of classes communicating directly, and thus requiring knowledge of their implementation, the classes send messages via a mediator object.
312+
用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互。
313313

314-
### Example
314+
### 示例:
315315
*/
316316
protocol Receiver {
317317
associatedtype MessageType
@@ -354,7 +354,7 @@ final class MessageMediator: Sender {
354354
}
355355

356356
/*:
357-
### Usage
357+
### 用法
358358
*/
359359
func spamMonster(message: String, worker: MessageMediator) {
360360
worker.send(message: message)
@@ -370,16 +370,16 @@ messagesMediator.add(recipient: user1)
370370
spamMonster(message: "I'd Like to Add you to My Professional Network", worker: messagesMediator)
371371

372372
/*:
373-
💾 Memento
374-
----------
373+
💾 备忘录(Memento
374+
--------------
375375

376-
The memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.
376+
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样就可以将该对象恢复到原先保存的状态
377377

378-
### Example
378+
### 示例:
379379
*/
380380
typealias Memento = [String: String]
381381
/*:
382-
Originator
382+
发起人(Originator
383383
*/
384384
protocol MementoConvertible {
385385
var memento: Memento { get }
@@ -416,7 +416,7 @@ struct GameState: MementoConvertible {
416416
}
417417
}
418418
/*:
419-
Caretaker
419+
管理者(Caretaker
420420
*/
421421
enum CheckPoint {
422422

@@ -432,7 +432,7 @@ enum CheckPoint {
432432
}
433433
}
434434
/*:
435-
### Usage
435+
### 用法
436436
*/
437437
var gameState = GameState(chapter: "Black Mesa Inbound", weapon: "Crowbar")
438438

@@ -453,13 +453,12 @@ if let memento = CheckPoint.restore(saveName: "gameState1") as? Memento {
453453
dump(finalState)
454454
}
455455
/*:
456-
👓 Observer
457-
-----------
456+
👓 观察者(Observer
457+
---------------
458458

459-
The observer pattern is used to allow an object to publish changes to its state.
460-
Other objects subscribe to be immediately notified of any changes.
459+
一个目标对象管理所有相依于它的观察者对象,并且在它本身的状态改变时主动发出通知
461460

462-
### Example
461+
### 示例:
463462
*/
464463
protocol PropertyObserver : class {
465464
func willChange(propertyName: String, newPropertyValue: Any?)
@@ -496,20 +495,20 @@ final class Observer : PropertyObserver {
496495
}
497496
}
498497
/*:
499-
### Usage
498+
### 用法
500499
*/
501500
var observerInstance = Observer()
502501
var testChambers = TestChambers()
503502
testChambers.observer = observerInstance
504503
testChambers.testChamberNumber += 1
505504
/*:
506-
🐉 State
505+
🐉 状态(State
507506
---------
508507

509-
The state pattern is used to alter the behaviour of an object as its internal state changes.
510-
The pattern allows the class for an object to apparently change at run-time.
508+
在状态模式中,对象的行为是基于它的内部状态而改变的。
509+
这个模式允许某个类对象在运行时发生改变。
511510

512-
### Example
511+
### 示例:
513512
*/
514513
final class Context {
515514
private var state: State = UnauthorizedState()
@@ -552,7 +551,7 @@ class AuthorizedState: State {
552551
func userId(context: Context) -> String? { return userId }
553552
}
554553
/*:
555-
### Usage
554+
### 用法
556555
*/
557556
let userContext = Context()
558557
(userContext.isAuthorized, userContext.userId)
@@ -561,12 +560,15 @@ userContext.changeStateToAuthorized(userId: "admin")
561560
userContext.changeStateToUnauthorized()
562561
(userContext.isAuthorized, userContext.userId)
563562
/*:
564-
💡 Strategy
565-
-----------
563+
💡 策略(Strategy
564+
--------------
566565

567-
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
566+
对象有某个行为,但是在不同的场景中,该行为有不同的实现算法。策略模式:
567+
* 定义了一族算法(业务规则);
568+
* 封装了每个算法;
569+
* 这族的算法可互换代替(interchangeable)。
568570

569-
### Example
571+
### 示例:
570572
*/
571573

572574
struct TestSubject {
@@ -604,7 +606,7 @@ final class BladeRunner {
604606
}
605607

606608
/*:
607-
### Usage
609+
### 用法
608610
*/
609611

610612
let rachel = TestSubject(pupilDiameter: 30.2,
@@ -619,12 +621,12 @@ let isRachelAndroid = deckard.testIfAndroid(rachel)
619621
let gaff = BladeRunner(test: GeneticTest())
620622
let isDeckardAndroid = gaff.testIfAndroid(rachel)
621623
/*:
622-
🏃 Visitor
623-
----------
624+
🏃 访问者(Visitor
625+
--------------
624626

625-
The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.
627+
封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作。
626628

627-
### Example
629+
### 示例:
628630
*/
629631
protocol PlanetVisitor {
630632
func visit(planet: PlanetAlderaan)
@@ -663,7 +665,7 @@ final class NameVisitor: PlanetVisitor {
663665
}
664666

665667
/*:
666-
### Usage
668+
### 用法
667669
*/
668670
let planets: [Planet] = [PlanetAlderaan(), PlanetCoruscant(), PlanetTatooine(), MoonJedha()]
669671

0 commit comments

Comments
 (0)