Skip to content

Commit 01afb5e

Browse files
committed
Add Flyweight by DavydLiu.
1 parent 4c0e7f7 commit 01afb5e

File tree

5 files changed

+249
-6
lines changed

5 files changed

+249
-6
lines changed

Design-Patterns.playground.zip

481 Bytes
Binary file not shown.

Design-Patterns.playground/Pages/Structural.xcplaygroundpage/Contents.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,70 @@ enum Eternal {
268268
Eternal.setObject("Disconnect me. I’d rather be nothing", forKey:"Bishop")
269269
Eternal.objectForKey("Bishop")
270270
/*:
271+
## 🍃 Flyweight
272+
The flyweight pattern is used to minimize memory usage or computational expenses by sharing as much as possible with other similar objects.
273+
### Example
274+
*/
275+
// Instances of CoffeeFlavour will be the Flyweights
276+
class CoffeeFlavor : Printable {
277+
var flavor: String
278+
var description: String {
279+
get {
280+
return flavor
281+
}
282+
}
283+
284+
init(flavor: String) {
285+
self.flavor = flavor
286+
}
287+
}
288+
289+
// Menu acts as a factory and cache for CoffeeFlavour flyweight objects
290+
class Menu {
291+
private var flavors: [String: CoffeeFlavor] = [:]
292+
293+
func lookup(flavor: String) -> CoffeeFlavor {
294+
if flavors.indexForKey(flavor) == nil {
295+
flavors[flavor] = CoffeeFlavor(flavor: flavor)
296+
}
297+
return flavors[flavor]!
298+
}
299+
}
300+
301+
class CoffeeShop {
302+
private var orders: [Int: CoffeeFlavor] = [:]
303+
private var menu = Menu()
304+
305+
func takeOrder(#flavor: String, table: Int) {
306+
orders[table] = menu.lookup(flavor)
307+
}
308+
309+
func serve() {
310+
for (table, flavor) in orders {
311+
println("Serving \(flavor) to table \(table)")
312+
}
313+
}
314+
}
315+
/*:
316+
### Usage
317+
*/
318+
let coffeeShop = CoffeeShop()
319+
320+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 1)
321+
coffeeShop.takeOrder(flavor: "Frappe", table: 3);
322+
coffeeShop.takeOrder(flavor: "Espresso", table: 2);
323+
coffeeShop.takeOrder(flavor: "Frappe", table: 15);
324+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 10);
325+
coffeeShop.takeOrder(flavor: "Frappe", table: 8);
326+
coffeeShop.takeOrder(flavor: "Espresso", table: 7);
327+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 4);
328+
coffeeShop.takeOrder(flavor: "Espresso", table: 9);
329+
coffeeShop.takeOrder(flavor: "Frappe", table: 12);
330+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 13);
331+
coffeeShop.takeOrder(flavor: "Espresso", table: 5);
332+
333+
coffeeShop.serve()
334+
/*:
271335
☔ Protection Proxy
272336
------------------
273337

README.md

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ A short cheat-sheet with Xcode 7.3 Playground ([Design-Patterns.playground.zip](
55

66
👷 Project maintained by: [@nsmeme](http://twitter.com/nsmeme) (Oktawian Chojnacki)
77

8-
⚠️ See my newest project: [OOD-Principles-In-Swift](https://github.com/ochococo/OOD-Principles-In-Swift)
9-
108
## Table of Contents
119

1210
* [Behavioral](#behavioral)
1311
* [Creational](#creational)
1412
* [Structural](#structural)
1513

14+
15+
```swift
16+
Behavioral |
17+
[Creational](Creational) |
18+
[Structural](Structural)
19+
```
20+
1621
Behavioral
1722
==========
1823

@@ -123,6 +128,9 @@ atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
123128

124129
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
125130
131+
```swift
132+
133+
```
126134

127135
👫 Command
128136
----------
@@ -289,6 +297,9 @@ var result = expression?.evaluate(intContext)
289297

290298
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
291299
300+
```swift
301+
302+
```
292303

293304
🍫 Iterator
294305
-----------
@@ -396,6 +407,9 @@ user0.send("Hello") // user1 receives message
396407

397408
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
398409
410+
```swift
411+
412+
```
399413

400414
💾 Memento
401415
----------
@@ -534,6 +548,9 @@ testChambers.testChamberNumber++
534548

535549
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
536550
551+
```swift
552+
553+
```
537554

538555
🐉 State
539556
---------
@@ -602,6 +619,10 @@ context.changeStateToUnauthorized()
602619

603620
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
604621
622+
```swift
623+
624+
```
625+
605626
💡 Strategy
606627
-----------
607628

@@ -654,6 +675,9 @@ upper.printString("O tempora, o mores!")
654675

655676
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
656677
678+
```swift
679+
680+
```
657681

658682
🏃 Visitor
659683
----------
@@ -710,6 +734,12 @@ names
710734

711735
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
712736
737+
```swift
738+
739+
[Behavioral](Behavioral) |
740+
Creational |
741+
[Structural](Structural)
742+
```
713743

714744
Creational
715745
==========
@@ -732,6 +762,9 @@ The "family" of objects created by the factory are determined at run-time.
732762

733763
### Example
734764

765+
```swift
766+
767+
```
735768

736769
Protocols
737770

@@ -864,6 +897,9 @@ let deathStar = DeathStar(builder:empire)
864897

865898
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
866899
900+
```swift
901+
902+
```
867903

868904
🏭 Factory Method
869905
-----------------
@@ -973,6 +1009,9 @@ Eduardo.name = "Eduardo"
9731009

9741010
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
9751011
1012+
```swift
1013+
1014+
```
9761015

9771016
💍 Singleton
9781017
------------
@@ -999,7 +1038,9 @@ class DeathStarSuperlaser {
9991038
```swift
10001039

10011040
let laser = DeathStarSuperlaser.sharedInstance
1002-
1041+
[Behavioral](Behavioral) |
1042+
[Creational](Creational) |
1043+
Structural
10031044
```
10041045

10051046
Structural
@@ -1079,6 +1120,9 @@ oldFormat.angleV
10791120

10801121
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
10811122
1123+
```swift
1124+
1125+
```
10821126

10831127
🌉 Bridge
10841128
----------
@@ -1141,6 +1185,9 @@ The composite pattern is used to create hierarchical, recursive tree structures
11411185

11421186
### Example
11431187

1188+
```swift
1189+
1190+
```
11441191

11451192
Component
11461193

@@ -1313,6 +1360,76 @@ Eternal.setObject("Disconnect me. I’d rather be nothing", forKey:"Bishop")
13131360
Eternal.objectForKey("Bishop")
13141361
```
13151362

1363+
## 🍃 Flyweight
1364+
The flyweight pattern is used to minimize memory usage or computational expenses by sharing as much as possible with other similar objects.
1365+
### Example
1366+
1367+
```swift
1368+
1369+
// Instances of CoffeeFlavour will be the Flyweights
1370+
class CoffeeFlavor : Printable {
1371+
var flavor: String
1372+
var description: String {
1373+
get {
1374+
return flavor
1375+
}
1376+
}
1377+
1378+
init(flavor: String) {
1379+
self.flavor = flavor
1380+
}
1381+
}
1382+
1383+
// Menu acts as a factory and cache for CoffeeFlavour flyweight objects
1384+
class Menu {
1385+
private var flavors: [String: CoffeeFlavor] = [:]
1386+
1387+
func lookup(flavor: String) -> CoffeeFlavor {
1388+
if flavors.indexForKey(flavor) == nil {
1389+
flavors[flavor] = CoffeeFlavor(flavor: flavor)
1390+
}
1391+
return flavors[flavor]!
1392+
}
1393+
}
1394+
1395+
class CoffeeShop {
1396+
private var orders: [Int: CoffeeFlavor] = [:]
1397+
private var menu = Menu()
1398+
1399+
func takeOrder(#flavor: String, table: Int) {
1400+
orders[table] = menu.lookup(flavor)
1401+
}
1402+
1403+
func serve() {
1404+
for (table, flavor) in orders {
1405+
println("Serving \(flavor) to table \(table)")
1406+
}
1407+
}
1408+
}
1409+
```
1410+
1411+
### Usage
1412+
1413+
```swift
1414+
1415+
let coffeeShop = CoffeeShop()
1416+
1417+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 1)
1418+
coffeeShop.takeOrder(flavor: "Frappe", table: 3);
1419+
coffeeShop.takeOrder(flavor: "Espresso", table: 2);
1420+
coffeeShop.takeOrder(flavor: "Frappe", table: 15);
1421+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 10);
1422+
coffeeShop.takeOrder(flavor: "Frappe", table: 8);
1423+
coffeeShop.takeOrder(flavor: "Espresso", table: 7);
1424+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 4);
1425+
coffeeShop.takeOrder(flavor: "Espresso", table: 9);
1426+
coffeeShop.takeOrder(flavor: "Frappe", table: 12);
1427+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 13);
1428+
coffeeShop.takeOrder(flavor: "Espresso", table: 5);
1429+
1430+
coffeeShop.serve()
1431+
```
1432+
13161433
☔ Protection Proxy
13171434
------------------
13181435

generate-playground.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/bash
22

3-
rm ./contents.swift
4-
53
cleanThisMessForReadme () {
64

75
FILENAME=$1
@@ -33,4 +31,4 @@ zip -r -X Design-Patterns.playground.zip ./Design-Patterns.playground
3331
rm ./Behavioral.swift
3432
rm ./Creational.swift
3533
rm ./Structural.swift
36-
rm ./contents.swift
34+
rm ./contents.swift

source/structural/flyweight.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*:
2+
## 🍃 Flyweight
3+
The flyweight pattern is used to minimize memory usage or computational expenses by sharing as much as possible with other similar objects.
4+
### Example
5+
*/
6+
// Instances of CoffeeFlavour will be the Flyweights
7+
class CoffeeFlavor : Printable {
8+
var flavor: String
9+
var description: String {
10+
get {
11+
return flavor
12+
}
13+
}
14+
15+
init(flavor: String) {
16+
self.flavor = flavor
17+
}
18+
}
19+
20+
// Menu acts as a factory and cache for CoffeeFlavour flyweight objects
21+
class Menu {
22+
private var flavors: [String: CoffeeFlavor] = [:]
23+
24+
func lookup(flavor: String) -> CoffeeFlavor {
25+
if flavors.indexForKey(flavor) == nil {
26+
flavors[flavor] = CoffeeFlavor(flavor: flavor)
27+
}
28+
return flavors[flavor]!
29+
}
30+
}
31+
32+
class CoffeeShop {
33+
private var orders: [Int: CoffeeFlavor] = [:]
34+
private var menu = Menu()
35+
36+
func takeOrder(#flavor: String, table: Int) {
37+
orders[table] = menu.lookup(flavor)
38+
}
39+
40+
func serve() {
41+
for (table, flavor) in orders {
42+
println("Serving \(flavor) to table \(table)")
43+
}
44+
}
45+
}
46+
/*:
47+
### Usage
48+
*/
49+
let coffeeShop = CoffeeShop()
50+
51+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 1)
52+
coffeeShop.takeOrder(flavor: "Frappe", table: 3);
53+
coffeeShop.takeOrder(flavor: "Espresso", table: 2);
54+
coffeeShop.takeOrder(flavor: "Frappe", table: 15);
55+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 10);
56+
coffeeShop.takeOrder(flavor: "Frappe", table: 8);
57+
coffeeShop.takeOrder(flavor: "Espresso", table: 7);
58+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 4);
59+
coffeeShop.takeOrder(flavor: "Espresso", table: 9);
60+
coffeeShop.takeOrder(flavor: "Frappe", table: 12);
61+
coffeeShop.takeOrder(flavor: "Cappuccino", table: 13);
62+
coffeeShop.takeOrder(flavor: "Espresso", table: 5);
63+
64+
coffeeShop.serve()

0 commit comments

Comments
 (0)