You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Design-Patterns-CN.playground/Pages/Behavioral.xcplaygroundpage/Contents.swift
+74-72
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,26 @@
1
1
/*:
2
2
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.
atm.withdraw(amount:310) // Cannot because ATM has only 300
113
112
atm.withdraw(amount:100) // Can withdraw - 1x100
114
113
/*:
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.
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.
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.
spamMonster(message:"I'd Like to Add you to My Professional Network", worker: messagesMediator)
371
371
372
372
/*:
373
-
💾 Memento
374
-
----------
373
+
💾 备忘录(Memento)
374
+
--------------
375
375
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.
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)。
568
570
569
-
### Example
571
+
### 示例:
570
572
*/
571
573
572
574
structTestSubject{
@@ -604,7 +606,7 @@ final class BladeRunner {
604
606
}
605
607
606
608
/*:
607
-
### Usage
609
+
### 用法
608
610
*/
609
611
610
612
letrachel=TestSubject(pupilDiameter:30.2,
@@ -619,12 +621,12 @@ let isRachelAndroid = deckard.testIfAndroid(rachel)
619
621
letgaff=BladeRunner(test:GeneticTest())
620
622
letisDeckardAndroid= gaff.testIfAndroid(rachel)
621
623
/*:
622
-
🏃 Visitor
623
-
----------
624
+
🏃 访问者(Visitor)
625
+
--------------
624
626
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.
0 commit comments