Skip to content

Commit d51ba61

Browse files
author
Oktawian Chojnacki
committed
+Chain Of Responsibility
1 parent 529dac4 commit d51ba61

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

README.markdown

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ enum NumberType {
128128
class NumberAbstractFactory {
129129
class func numberFactoryType(type : NumberType) -> NumberFactoryProtocol {
130130

131-
switch (type) {
131+
switch type {
132132
case .NextStep:
133133
return NextStepNumberFactory()
134134
case .Swift:
@@ -323,10 +323,11 @@ Eternal.objectForKey("Bishop")
323323
class PointConverter {
324324

325325
class func convert(#point:Point, base:Double, negative:Bool) -> Point {
326+
326327
var pointConverted = Point{
327-
if let x = point.x{ $0.x = x * base * (negative ? -1.0 : 1.0) }
328-
if let y = point.y{ $0.y = y * base * (negative ? -1.0 : 1.0) }
329-
if let z = point.z{ $0.z = z * base * (negative ? -1.0 : 1.0) }
328+
if let x = point.x { $0.x = x * base * (negative ? -1.0 : 1.0) }
329+
if let y = point.y { $0.y = y * base * (negative ? -1.0 : 1.0) }
330+
if let z = point.z { $0.z = z * base * (negative ? -1.0 : 1.0) }
330331
}
331332

332333
return pointConverted
@@ -473,7 +474,7 @@ println("Cost : \(someCoffee.getCost()); Ingredients: \(someCoffee.getIngredient
473474
```
474475

475476

476-
##🍬 Virtual Proxy
477+
##🍬 Virtual Proxy
477478

478479
**Source:**
479480
```swift
@@ -502,7 +503,7 @@ let humanInterface = HEVSuitHumanInterface()
502503
humanInterface.administerMorphine()
503504
```
504505

505-
##☔ Protection Proxy
506+
##☔ Protection Proxy
506507

507508
**Source:**
508509
```swift
@@ -524,6 +525,7 @@ class CurrentComputer : DoorOperator {
524525
if pass != "pass" {
525526
return false
526527
}
528+
527529
computer = HAL9000()
528530

529531
return true
@@ -558,7 +560,9 @@ computer.openDoors(doors)
558560
>
559561
>**Source:** [wikipedia.org](http://en.wikipedia.org/wiki/Behavioral_pattern)
560562
561-
##🚧 Chain Of Responsibility
563+
##🐝 Chain Of Responsibility
564+
565+
**Source:**
562566
```swift
563567
class MoneyPile {
564568
let value: Int
@@ -572,21 +576,24 @@ class MoneyPile {
572576
}
573577

574578
func canWithdraw(var v: Int) -> Bool {
579+
575580
func canTakeSomeBill(want: Int) -> Bool {
576581
return (want / self.value) > 0
577582
}
578583

579584
var q = self.quantity
585+
580586
while canTakeSomeBill(v) {
587+
581588
if (q == 0) {
582589
break
583590
}
584-
591+
585592
v -= self.value
586-
q--
593+
q -= 1
587594
}
588595

589-
if (v == 0) {
596+
if v == 0 {
590597
return true
591598
} else if let next = self.nextPile {
592599
return next.canWithdraw(v)
@@ -606,7 +613,11 @@ class ATM {
606613
return self.hundred
607614
}
608615

609-
init(hundred: MoneyPile, fifty: MoneyPile, twenty: MoneyPile, ten: MoneyPile) {
616+
init(hundred: MoneyPile,
617+
fifty: MoneyPile,
618+
twenty: MoneyPile,
619+
ten: MoneyPile) {
620+
610621
self.hundred = hundred
611622
self.fifty = fifty
612623
self.twenty = twenty
@@ -621,18 +632,18 @@ class ATM {
621632

622633
**Usage:**
623634
```swift
624-
/// Create piles of money and link them together 10 < 20 < 50 < 100
625-
var ten = MoneyPile(value: 10, quantity: 6, nextPile: nil)
626-
var twenty = MoneyPile(value: 20, quantity: 2, nextPile: ten)
627-
var fifty = MoneyPile(value: 50, quantity: 2, nextPile: twenty)
628-
var hundred = MoneyPile(value: 100, quantity: 1, nextPile: fifty)
635+
// Create piles of money and link them together 10 < 20 < 50 < 100.
636+
let ten = MoneyPile(value: 10, quantity: 6, nextPile: nil)
637+
let twenty = MoneyPile(value: 20, quantity: 2, nextPile: ten)
638+
let fifty = MoneyPile(value: 50, quantity: 2, nextPile: twenty)
639+
let hundred = MoneyPile(value: 100, quantity: 1, nextPile: fifty)
629640

630-
/// build atm
641+
// Build ATM.
631642
var atm = ATM(hundred: hundred, fifty: fifty, twenty: twenty, ten: ten)
632-
atm.canWithdraw(310) /// Cannot because ATM has only 300
633-
atm.canWithdraw(100) /// Can withdraw - 1x100
634-
atm.canWithdraw(165) /// Cannot withdraw because ATM doesn't has bill with value of 5
635-
atm.canWithdraw(30) /// Can withdraw - 1x20, 2x10
643+
atm.canWithdraw(310) // Cannot because ATM has only 300
644+
atm.canWithdraw(100) // Can withdraw - 1x100
645+
atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value of 5
646+
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
636647
```
637648

638649
##👫 Command
@@ -704,10 +715,13 @@ fileManager.move()
704715
```swift
705716
class StepCounter {
706717
var totalSteps: Int = 0 {
718+
707719
willSet(newTotalSteps) {
708720
println("About to set totalSteps to \(newTotalSteps)")
709721
}
722+
710723
didSet {
724+
711725
if totalSteps > oldValue {
712726
println("Added \(totalSteps - oldValue) steps")
713727
}

0 commit comments

Comments
 (0)