Skip to content

Commit

Permalink
Merge pull request ochococo#57 from kingreza/master
Browse files Browse the repository at this point in the history
Add further example. update source to match README
  • Loading branch information
ochococo committed Jun 6, 2016
2 parents 6b86c20 + 2d7c6a4 commit dc27931
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 22 deletions.
Binary file modified Design-Patterns.playground.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ atm.canWithdraw(100) // Can withdraw - 1x100
atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value of 5
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
*/
/*:
👫 Command
----------

Expand Down Expand Up @@ -250,6 +253,9 @@ intContext.assign(c, value: 3)

var result = expression?.evaluate(intContext)
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
*/
/*:
🍫 Iterator
-----------

Expand Down Expand Up @@ -342,6 +348,9 @@ messagesMediator.addColleague(user1)

user0.send("Hello") // user1 receives message
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
*/
/*:
💾 Memento
----------

Expand Down Expand Up @@ -373,14 +382,14 @@ class GameState {
/*:
Caretaker
*/
class CheckPoint {
class func saveState(memento: Memento, keyName: String = DPMementoGameState) {
enum CheckPoint {
static func saveState(memento: Memento, keyName: String = DPMementoGameState) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(memento, forKey: keyName)
defaults.synchronize()
}

class func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
static func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
let defaults = NSUserDefaults.standardUserDefaults()

return defaults.objectForKey(keyName) as? Memento ?? Memento()
Expand Down Expand Up @@ -459,6 +468,9 @@ var testChambers = TestChambers()
testChambers.observer = observerInstance
testChambers.testChamberNumber++
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
*/
/*:
🐉 State
---------

Expand Down Expand Up @@ -518,6 +530,9 @@ context.changeStateToAuthorized(userId: "admin")
context.changeStateToUnauthorized()
(context.isAuthorized, context.userId)
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
*/
/*:
💡 Strategy
-----------

Expand Down Expand Up @@ -561,7 +576,9 @@ lower.printString("O tempora, o mores!")

var upper = Printer(strategy:UpperCaseStrategy())
upper.printString("O tempora, o mores!")

/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
*/
/*:
🏃 Visitor
----------
Expand Down Expand Up @@ -609,3 +626,6 @@ let names = planets.map { (planet: Planet) -> String in
}

names
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
*/
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ enum NumberType {
case NextStep, Swift
}

class NumberHelper {
class func factoryFor(type : NumberType) -> NumberFactory {
enum NumberHelper {
static func factoryFor(type : NumberType) -> NumberFactory {
switch type {
case .NextStep:
return NextStepNumber.make
Expand Down Expand Up @@ -135,6 +135,9 @@ let empire = DeathStarBuilder { builder in

let deathStar = DeathStar(builder:empire)
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
*/
/*:
🏭 Factory Method
-----------------

Expand Down Expand Up @@ -171,8 +174,8 @@ enum Country {
case UnitedStates, Spain, UK, Greece
}

class CurrencyFactory {
class func currencyForCountry(country:Country) -> Currency? {
enum CurrencyFactory {
static func currencyForCountry(country:Country) -> Currency? {

switch country {
case .Spain, .Greece :
Expand Down Expand Up @@ -229,6 +232,9 @@ Christoph.name = "Christoph"
let Eduardo = Prototype.clone()
Eduardo.name = "Eduardo"
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
*/
/*:
💍 Singleton
------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ let oldFormat = OldDeathStarSuperlaserTarget(target)
oldFormat.angleH
oldFormat.angleV
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
*/
/*:
🌉 Bridge
----------

Expand Down Expand Up @@ -244,15 +247,15 @@ The facade pattern is used to define a simplified interface to a more complex su

### Example
*/
class Eternal {
enum Eternal {

class func setObject(value: AnyObject!, forKey defaultName: String!) {
static func setObject(value: AnyObject!, forKey defaultName: String!) {
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(value, forKey:defaultName)
defaults.synchronize()
}

class func objectForKey(defaultName: String!) -> AnyObject! {
static func objectForKey(defaultName: String!) -> AnyObject! {
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()

return defaults.objectForKey(defaultName)
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ A short cheat-sheet with Xcode 7beta Playground ([Design-Patterns.playground.zip
* [Creational](#creational)
* [Structural](#structural)


```swift
Behavioral |
[Creational](Creational) |
[Structural](Structural)
```

Behavioral
==========

Expand Down Expand Up @@ -117,6 +124,8 @@ atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
👫 Command
----------

Expand Down Expand Up @@ -280,6 +289,8 @@ intContext.assign(c, value: 3)
var result = expression?.evaluate(intContext)
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
🍫 Iterator
-----------

Expand Down Expand Up @@ -384,6 +395,8 @@ messagesMediator.addColleague(user1)
user0.send("Hello") // user1 receives message
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
💾 Memento
----------

Expand Down Expand Up @@ -519,6 +532,8 @@ testChambers.observer = observerInstance
testChambers.testChamberNumber++
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
🐉 State
---------

Expand Down Expand Up @@ -584,6 +599,8 @@ context.changeStateToUnauthorized()
(context.isAuthorized, context.userId)
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
💡 Strategy
-----------

Expand Down Expand Up @@ -635,6 +652,8 @@ upper.printString("O tempora, o mores!")

```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
🏃 Visitor
----------

Expand Down Expand Up @@ -686,6 +705,11 @@ let names = planets.map { (planet: Planet) -> String in
}

names
```
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
```swift

[Behavioral](Behavioral) |
Creational |
[Structural](Structural)
Expand Down Expand Up @@ -845,6 +869,9 @@ let empire = DeathStarBuilder { builder in
let deathStar = DeathStar(builder:empire)
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)

🏭 Factory Method
-----------------

Expand Down Expand Up @@ -951,6 +978,8 @@ let Eduardo = Prototype.clone()
Eduardo.name = "Eduardo"
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
💍 Singleton
------------

Expand Down Expand Up @@ -1056,6 +1085,8 @@ oldFormat.angleH
oldFormat.angleV
```

>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
🌉 Bridge
----------

Expand Down
3 changes: 3 additions & 0 deletions source/behavioral/chain_of_responsibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ atm.canWithdraw(310) // Cannot because ATM has only 300
atm.canWithdraw(100) // Can withdraw - 1x100
atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value of 5
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
*/
3 changes: 3 additions & 0 deletions source/behavioral/interpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ intContext.assign(b, value: 1)
intContext.assign(c, value: 3)

var result = expression?.evaluate(intContext)
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
*/
3 changes: 3 additions & 0 deletions source/behavioral/mediator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ messagesMediator.addColleague(user0)
messagesMediator.addColleague(user1)

user0.send("Hello") // user1 receives message
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
*/
6 changes: 3 additions & 3 deletions source/behavioral/memento.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class GameState {
/*:
Caretaker
*/
class CheckPoint {
class func saveState(memento: Memento, keyName: String = DPMementoGameState) {
enum CheckPoint {
static func saveState(memento: Memento, keyName: String = DPMementoGameState) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(memento, forKey: keyName)
defaults.synchronize()
}

class func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
static func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
let defaults = NSUserDefaults.standardUserDefaults()

return defaults.objectForKey(keyName) as? Memento ?? Memento()
Expand Down
3 changes: 3 additions & 0 deletions source/behavioral/observer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ var observerInstance = Observer()
var testChambers = TestChambers()
testChambers.observer = observerInstance
testChambers.testChamberNumber++
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
*/
3 changes: 3 additions & 0 deletions source/behavioral/state.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ context.changeStateToAuthorized(userId: "admin")
(context.isAuthorized, context.userId) // now logged in as "admin"
context.changeStateToUnauthorized()
(context.isAuthorized, context.userId)
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
*/
4 changes: 3 additions & 1 deletion source/behavioral/strategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ lower.printString("O tempora, o mores!")

var upper = Printer(strategy:UpperCaseStrategy())
upper.printString("O tempora, o mores!")

/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
*/
3 changes: 3 additions & 0 deletions source/behavioral/visitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ let names = planets.map { (planet: Planet) -> String in
}

names
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
*/
4 changes: 2 additions & 2 deletions source/creational/abstract_factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ enum NumberType {
case NextStep, Swift
}

class NumberHelper {
class func factoryFor(type : NumberType) -> NumberFactory {
enum NumberHelper {
static func factoryFor(type : NumberType) -> NumberFactory {
switch type {
case .NextStep:
return NextStepNumber.make
Expand Down
3 changes: 3 additions & 0 deletions source/creational/builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ let empire = DeathStarBuilder { builder in
}

let deathStar = DeathStar(builder:empire)
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
*/
4 changes: 2 additions & 2 deletions source/creational/factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ enum Country {
case UnitedStates, Spain, UK, Greece
}

class CurrencyFactory {
class func currencyForCountry(country:Country) -> Currency? {
enum CurrencyFactory {
static func currencyForCountry(country:Country) -> Currency? {

switch country {
case .Spain, .Greece :
Expand Down
3 changes: 3 additions & 0 deletions source/creational/prototype.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ Christoph.name = "Christoph"

let Eduardo = Prototype.clone()
Eduardo.name = "Eduardo"
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
*/
3 changes: 3 additions & 0 deletions source/structural/adapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ let oldFormat = OldDeathStarSuperlaserTarget(target)

oldFormat.angleH
oldFormat.angleV
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
*/
6 changes: 3 additions & 3 deletions source/structural/facade.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ The facade pattern is used to define a simplified interface to a more complex su

### Example
*/
class Eternal {
enum Eternal {

class func setObject(value: AnyObject!, forKey defaultName: String!) {
static func setObject(value: AnyObject!, forKey defaultName: String!) {
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(value, forKey:defaultName)
defaults.synchronize()
}

class func objectForKey(defaultName: String!) -> AnyObject! {
static func objectForKey(defaultName: String!) -> AnyObject! {
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()

return defaults.objectForKey(defaultName)
Expand Down

0 comments on commit dc27931

Please sign in to comment.