@@ -128,7 +128,7 @@ enum NumberType {
128
128
class NumberAbstractFactory {
129
129
class func numberFactoryType (type : NumberType) -> NumberFactoryProtocol {
130
130
131
- switch ( type) {
131
+ switch type {
132
132
case .NextStep :
133
133
return NextStepNumberFactory ()
134
134
case .Swift :
@@ -323,10 +323,11 @@ Eternal.objectForKey("Bishop")
323
323
class PointConverter {
324
324
325
325
class func convert (#point :Point, base :Double , negative :Bool ) -> Point {
326
+
326
327
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 ) }
330
331
}
331
332
332
333
return pointConverted
@@ -473,7 +474,7 @@ println("Cost : \(someCoffee.getCost()); Ingredients: \(someCoffee.getIngredient
473
474
```
474
475
475
476
476
- ##🍬 Virtual Proxy
477
+ ##🍬 Virtual Proxy
477
478
478
479
** Source:**
479
480
``` swift
@@ -502,7 +503,7 @@ let humanInterface = HEVSuitHumanInterface()
502
503
humanInterface.administerMorphine ()
503
504
```
504
505
505
- ##☔ Protection Proxy
506
+ ##☔ Protection Proxy
506
507
507
508
** Source:**
508
509
``` swift
@@ -524,6 +525,7 @@ class CurrentComputer : DoorOperator {
524
525
if pass != " pass" {
525
526
return false
526
527
}
528
+
527
529
computer = HAL9000 ()
528
530
529
531
return true
@@ -558,7 +560,9 @@ computer.openDoors(doors)
558
560
>
559
561
> ** Source:** [ wikipedia.org] ( http://en.wikipedia.org/wiki/Behavioral_pattern )
560
562
561
- ##🚧 Chain Of Responsibility
563
+ ##🐝 Chain Of Responsibility
564
+
565
+ ** Source:**
562
566
``` swift
563
567
class MoneyPile {
564
568
let value: Int
@@ -572,21 +576,24 @@ class MoneyPile {
572
576
}
573
577
574
578
func canWithdraw (var v : Int ) -> Bool {
579
+
575
580
func canTakeSomeBill (want : Int ) -> Bool {
576
581
return (want / self .value ) > 0
577
582
}
578
583
579
584
var q = self .quantity
585
+
580
586
while canTakeSomeBill (v) {
587
+
581
588
if (q == 0 ) {
582
589
break
583
590
}
584
-
591
+
585
592
v -= self .value
586
- q--
593
+ q -= 1
587
594
}
588
595
589
- if ( v == 0 ) {
596
+ if v == 0 {
590
597
return true
591
598
} else if let next = self .nextPile {
592
599
return next.canWithdraw (v)
@@ -606,7 +613,11 @@ class ATM {
606
613
return self .hundred
607
614
}
608
615
609
- init (hundred : MoneyPile, fifty : MoneyPile, twenty : MoneyPile, ten : MoneyPile) {
616
+ init (hundred : MoneyPile,
617
+ fifty : MoneyPile,
618
+ twenty : MoneyPile,
619
+ ten : MoneyPile) {
620
+
610
621
self .hundred = hundred
611
622
self .fifty = fifty
612
623
self .twenty = twenty
@@ -621,18 +632,18 @@ class ATM {
621
632
622
633
** Usage:**
623
634
``` 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)
629
640
630
- /// build atm
641
+ // Build ATM.
631
642
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
636
647
```
637
648
638
649
##👫 Command
@@ -704,10 +715,13 @@ fileManager.move()
704
715
``` swift
705
716
class StepCounter {
706
717
var totalSteps: Int = 0 {
718
+
707
719
willSet (newTotalSteps) {
708
720
println (" About to set totalSteps to \( newTotalSteps ) " )
709
721
}
722
+
710
723
didSet {
724
+
711
725
if totalSteps > oldValue {
712
726
println (" Added \( totalSteps - oldValue ) steps" )
713
727
}
0 commit comments