Skip to content

Commit 35ed618

Browse files
author
Oktawian Chojnacki
committed
Merge branch 'patch-1' of https://github.com/TheDmitry/Design-Patterns-In-Swift into TheDmitry-patch-1
# By TheDmitry # Via TheDmitry * 'patch-1' of https://github.com/TheDmitry/Design-Patterns-In-Swift: generation Add Interpreter pattern # Conflicts: # Design-Patterns.playground.zip
2 parents d726b87 + 2c42b3a commit 35ed618

File tree

4 files changed

+280
-11
lines changed

4 files changed

+280
-11
lines changed

Design-Patterns.playground.zip

5.95 KB
Binary file not shown.

Design-Patterns.playground/contents.swift

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ A short cheat-sheet with Xcode 6.3beta Playground ([Design-Patterns.playground.z
1616
* [Creational](#creational)
1717
* [Structural](#structural)
1818

19-
*/
20-
/*:
19+
*//*:
2120
Behavioral
2221
==========
2322

2423
>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.
2524
>
2625
>**Source:** [wikipedia.org](http://en.wikipedia.org/wiki/Behavioral_pattern)
27-
*/
28-
/*:
26+
*//*:
2927
🐝 Chain Of Responsibility
3028
--------------------------
3129

@@ -174,6 +172,96 @@ let doorModule = HAL9000DoorsOperations(doors:podBayDoors)
174172
doorModule.open()
175173
doorModule.close()
176174
/*:
175+
🎶 Interpreter
176+
--------------
177+
178+
The interpreter pattern is used to evaluate sentences in a language.
179+
180+
### Example
181+
*/
182+
183+
protocol IntegerExp {
184+
func evaluate(context: IntegerContext) -> Int
185+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp
186+
func copy() -> IntegerExp
187+
}
188+
189+
class IntegerContext {
190+
private var data: [Character:Int] = [:]
191+
192+
func lookup(name: Character) -> Int {
193+
return self.data[name]!
194+
}
195+
196+
func assign(integerVarExp: IntegerVarExp, value: Int) {
197+
self.data[integerVarExp.name] = value
198+
}
199+
}
200+
201+
class IntegerVarExp: IntegerExp {
202+
let name: Character
203+
204+
init(name: Character) {
205+
self.name = name
206+
}
207+
208+
func evaluate(context: IntegerContext) -> Int {
209+
return context.lookup(self.name)
210+
}
211+
212+
func replace(name: Character, integerExp: IntegerExp) -> IntegerExp {
213+
if name == self.name {
214+
return integerExp.copy()
215+
} else {
216+
return IntegerVarExp(name: self.name)
217+
}
218+
}
219+
220+
func copy() -> IntegerExp {
221+
return IntegerVarExp(name: self.name)
222+
}
223+
}
224+
225+
class AddExp: IntegerExp {
226+
private var operand1: IntegerExp
227+
private var operand2: IntegerExp
228+
229+
init(op1: IntegerExp, op2: IntegerExp) {
230+
self.operand1 = op1
231+
self.operand2 = op2
232+
}
233+
234+
func evaluate(context: IntegerContext) -> Int {
235+
return self.operand1.evaluate(context) + self.operand2.evaluate(context)
236+
}
237+
238+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp {
239+
return AddExp(op1: operand1.replace(character, integerExp: integerExp),
240+
op2: operand2.replace(character, integerExp: integerExp))
241+
}
242+
243+
func copy() -> IntegerExp {
244+
return AddExp(op1: self.operand1, op2: self.operand2)
245+
}
246+
}
247+
/*:
248+
### Usage
249+
*/
250+
var expression: IntegerExp?
251+
var intContext = IntegerContext()
252+
253+
var a = IntegerVarExp(name: "A")
254+
var b = IntegerVarExp(name: "B")
255+
var c = IntegerVarExp(name: "C")
256+
257+
expression = AddExp(op1: a, op2: AddExp(op1: b, op2: c)) // a + (b + c)
258+
259+
intContext.assign(a, value: 2)
260+
intContext.assign(b, value: 1)
261+
intContext.assign(c, value: 3)
262+
263+
var result = expression?.evaluate(intContext)
264+
/*:
177265
🍫 Iterator
178266
-----------
179267

@@ -537,8 +625,7 @@ Creational
537625
> In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
538626
>
539627
>**Source:** [wikipedia.org](http://en.wikipedia.org/wiki/Creational_pattern)
540-
*/
541-
/*:
628+
*//*:
542629
🌰 Abstract Factory
543630
-------------------
544631

@@ -784,8 +871,7 @@ Structural
784871
>In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.
785872
>
786873
>**Source:** [wikipedia.org](http://en.wikipedia.org/wiki/Structural_pattern)
787-
*/
788-
/*:
874+
*//*:
789875
🔌 Adapter
790876
----------
791877

@@ -898,8 +984,7 @@ protocol Shape {
898984
}
899985
/*:
900986
Leafs
901-
*/
902-
987+
*/
903988
class Square : Shape {
904989
func draw(fillColor: String) {
905990
print("Drawing a Square with color \(fillColor)")
@@ -1129,4 +1214,4 @@ Info
11291214

11301215
🚀 How to generate playground (+zip) from source: [GENERATE.md](https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.md)
11311216

1132-
*/
1217+
*/

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,100 @@ doorModule.open()
182182
doorModule.close()
183183
```
184184

185+
🎶 Interpreter
186+
--------------
187+
188+
The interpreter pattern is used to evaluate sentences in a language.
189+
190+
### Example
191+
192+
```swift
193+
194+
protocol IntegerExp {
195+
func evaluate(context: IntegerContext) -> Int
196+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp
197+
func copy() -> IntegerExp
198+
}
199+
200+
class IntegerContext {
201+
private var data: [Character:Int] = [:]
202+
203+
func lookup(name: Character) -> Int {
204+
return self.data[name]!
205+
}
206+
207+
func assign(integerVarExp: IntegerVarExp, value: Int) {
208+
self.data[integerVarExp.name] = value
209+
}
210+
}
211+
212+
class IntegerVarExp: IntegerExp {
213+
let name: Character
214+
215+
init(name: Character) {
216+
self.name = name
217+
}
218+
219+
func evaluate(context: IntegerContext) -> Int {
220+
return context.lookup(self.name)
221+
}
222+
223+
func replace(name: Character, integerExp: IntegerExp) -> IntegerExp {
224+
if name == self.name {
225+
return integerExp.copy()
226+
} else {
227+
return IntegerVarExp(name: self.name)
228+
}
229+
}
230+
231+
func copy() -> IntegerExp {
232+
return IntegerVarExp(name: self.name)
233+
}
234+
}
235+
236+
class AddExp: IntegerExp {
237+
private var operand1: IntegerExp
238+
private var operand2: IntegerExp
239+
240+
init(op1: IntegerExp, op2: IntegerExp) {
241+
self.operand1 = op1
242+
self.operand2 = op2
243+
}
244+
245+
func evaluate(context: IntegerContext) -> Int {
246+
return self.operand1.evaluate(context) + self.operand2.evaluate(context)
247+
}
248+
249+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp {
250+
return AddExp(op1: operand1.replace(character, integerExp: integerExp),
251+
op2: operand2.replace(character, integerExp: integerExp))
252+
}
253+
254+
func copy() -> IntegerExp {
255+
return AddExp(op1: self.operand1, op2: self.operand2)
256+
}
257+
}
258+
```
259+
260+
### Usage
261+
262+
```swift
263+
var expression: IntegerExp?
264+
var intContext = IntegerContext()
265+
266+
var a = IntegerVarExp(name: "A")
267+
var b = IntegerVarExp(name: "B")
268+
var c = IntegerVarExp(name: "C")
269+
270+
expression = AddExp(op1: a, op2: AddExp(op1: b, op2: c)) // a + (b + c)
271+
272+
intContext.assign(a, value: 2)
273+
intContext.assign(b, value: 1)
274+
intContext.assign(c, value: 3)
275+
276+
var result = expression?.evaluate(intContext)
277+
```
278+
185279
🍫 Iterator
186280
-----------
187281

source/behavioral/interpreter.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*:
2+
🎶 Interpreter
3+
--------------
4+
5+
The interpreter pattern is used to evaluate sentences in a language.
6+
7+
### Example
8+
*/
9+
10+
protocol IntegerExp {
11+
func evaluate(context: IntegerContext) -> Int
12+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp
13+
func copy() -> IntegerExp
14+
}
15+
16+
class IntegerContext {
17+
private var data: [Character:Int] = [:]
18+
19+
func lookup(name: Character) -> Int {
20+
return self.data[name]!
21+
}
22+
23+
func assign(integerVarExp: IntegerVarExp, value: Int) {
24+
self.data[integerVarExp.name] = value
25+
}
26+
}
27+
28+
class IntegerVarExp: IntegerExp {
29+
let name: Character
30+
31+
init(name: Character) {
32+
self.name = name
33+
}
34+
35+
func evaluate(context: IntegerContext) -> Int {
36+
return context.lookup(self.name)
37+
}
38+
39+
func replace(name: Character, integerExp: IntegerExp) -> IntegerExp {
40+
if name == self.name {
41+
return integerExp.copy()
42+
} else {
43+
return IntegerVarExp(name: self.name)
44+
}
45+
}
46+
47+
func copy() -> IntegerExp {
48+
return IntegerVarExp(name: self.name)
49+
}
50+
}
51+
52+
class AddExp: IntegerExp {
53+
private var operand1: IntegerExp
54+
private var operand2: IntegerExp
55+
56+
init(op1: IntegerExp, op2: IntegerExp) {
57+
self.operand1 = op1
58+
self.operand2 = op2
59+
}
60+
61+
func evaluate(context: IntegerContext) -> Int {
62+
return self.operand1.evaluate(context) + self.operand2.evaluate(context)
63+
}
64+
65+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp {
66+
return AddExp(op1: operand1.replace(character, integerExp: integerExp),
67+
op2: operand2.replace(character, integerExp: integerExp))
68+
}
69+
70+
func copy() -> IntegerExp {
71+
return AddExp(op1: self.operand1, op2: self.operand2)
72+
}
73+
}
74+
/*:
75+
### Usage
76+
*/
77+
var expression: IntegerExp?
78+
var intContext = IntegerContext()
79+
80+
var a = IntegerVarExp(name: "A")
81+
var b = IntegerVarExp(name: "B")
82+
var c = IntegerVarExp(name: "C")
83+
84+
expression = AddExp(op1: a, op2: AddExp(op1: b, op2: c)) // a + (b + c)
85+
86+
intContext.assign(a, value: 2)
87+
intContext.assign(b, value: 1)
88+
intContext.assign(c, value: 3)
89+
90+
var result = expression?.evaluate(intContext)

0 commit comments

Comments
 (0)