Skip to content

Commit 8f1f6ef

Browse files
author
Oktawian Chojnacki
committed
Descriptions for patterns.
1 parent 1b6af0e commit 8f1f6ef

24 files changed

+167
-9
lines changed

README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ A short cheat-sheet with Xcode 6 Playground ([Design-Patterns.playground.zip](ht
2323
2424
##🐝 Chain Of Responsibility
2525

26-
**Source:**
26+
The chain of responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.
27+
28+
**Example:**
29+
2730
```swift
2831
class MoneyPile {
2932
let value: Int
@@ -108,6 +111,10 @@ atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
108111
```
109112
##👫 Command
110113

114+
The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.
115+
116+
**Example:**
117+
111118
```swift
112119
protocol FileOperationCommand {
113120
init(file: String)
@@ -169,10 +176,20 @@ fileManager.move()
169176

170177
##🚧 Iterator
171178

179+
The iterator pattern is used to provide a standard interface for traversing a collection of items in an aggregate object without the need to understand its underlying structure.
180+
181+
*No example, yet.*
172182
##🚧 Mediator
173183

184+
The mediator pattern is used to reduce coupling between classes that communicate with each other. Instead of classes communicating directly, and thus requiring knowledge of their implementation, the classes send messages via a mediator object.
185+
186+
*No example, yet.*
174187
##💾 Memento
175188

189+
The memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.
190+
191+
**Example:**
192+
176193
```swift
177194
typealias Memento = Dictionary<NSObject, AnyObject>
178195

@@ -241,6 +258,11 @@ gameState.restoreFromMemeto(CheckPoint.restorePreviousState(keyName: "gameState2
241258
```
242259
##👓 Observer
243260

261+
The observer pattern is used to allow an object to publish changes to its state.
262+
Other objects subscribe to be immediately notified of any changes.
263+
264+
**Example:**
265+
244266
```swift
245267
class StepCounter {
246268
var totalSteps: Int = 0 {
@@ -273,6 +295,11 @@ stepCounter.totalSteps = 896
273295
```
274296
##🐉 State
275297

298+
The state pattern is used to alter the behaviour of an object as its internal state changes.
299+
The pattern allows the class for an object to apparently change at run-time.
300+
301+
**Example:**
302+
276303
```swift
277304
class Context {
278305
private var state: State = UnauthorizedState()
@@ -327,6 +354,10 @@ context.changeStateToUnauthorized()
327354
```
328355
##💡 Strategy
329356

357+
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
358+
359+
**Example:**
360+
330361
```swift
331362
protocol PrintStrategy {
332363
func printString(string: String) -> String
@@ -367,6 +398,10 @@ upper.printString("O tempora, o mores!")
367398
```
368399
##🏃 Visitor
369400

401+
The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.
402+
403+
**Example:**
404+
370405
```swift
371406
protocol PlanetVisitor {
372407
func visit(planet: PlanetEarth)
@@ -408,7 +443,6 @@ let names = planets.map { (planet: Planet) -> String in
408443

409444
names
410445
```
411-
412446
# Creational
413447

414448
> 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.
@@ -417,6 +451,11 @@ names
417451
418452
##🌰 Abstract Factory
419453

454+
The abstract factory pattern is used to provide a client with a set of related or dependant objects.
455+
The "family" of objects created by the factory are determined at run-time.
456+
457+
**Example:**
458+
420459
```swift
421460
// Protocols.
422461

@@ -486,6 +525,11 @@ numberTwo.stringValue()
486525
```
487526
##👷 Builder
488527

528+
The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm.
529+
An external class controls the construction algorithm.
530+
531+
**Example:**
532+
489533
```swift
490534
protocol ThreeDimensions {
491535
var x: Double? {get}
@@ -530,6 +574,10 @@ let uglierPoint = Point {
530574
```
531575
##🏭 Factory Method
532576

577+
The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.
578+
579+
**Example:**
580+
533581
```swift
534582
protocol Currency {
535583
func symbol() -> String
@@ -587,6 +635,11 @@ CurrencyFactory.currencyForCountry(.UK)?.code() ?? noCurrencyCode
587635
```
588636
##🃏 Prototype
589637

638+
The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone.
639+
This practise is particularly useful when the construction of a new object is inefficient.
640+
641+
**Example:**
642+
590643
```swift
591644
class ThieveryCorporationPersonDisplay {
592645
var name: String?
@@ -615,6 +668,12 @@ let Eduardo = Prototype.clone()
615668
Eduardo.name = "Eduardo"
616669
```
617670
##💍 Singleton
671+
672+
The singleton pattern ensures that only one object of a particular class is ever created.
673+
All further references to objects of the singleton class refer to the same underlying instance.
674+
675+
**Example:**
676+
618677
```swift
619678
class SingletonClass {
620679
class var shared : SingletonClass {
@@ -639,6 +698,11 @@ let instance = SingletonClass.shared
639698
>**Source:** [wikipedia.org](http://en.wikipedia.org/wiki/Structural_pattern)
640699
641700
##🔌 Adapter
701+
702+
The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.
703+
704+
**Example:**
705+
642706
```swift
643707
// WARNING: This example uses Point class from Builder pattern!
644708

@@ -667,6 +731,7 @@ extension PointConverter{
667731

668732
}
669733
```
734+
670735
**Usage:**
671736
```swift
672737
var tuple = PointConverter.convert(x:1.1, y:2.2, z:3.3, base:2.0, negative:true)
@@ -943,10 +1008,12 @@ class HEVSuitHumanInterface : HEVSuitMedicalAid {
9431008
let humanInterface = HEVSuitHumanInterface()
9441009
humanInterface.administerMorphine()
9451010
```
1011+
9461012
Info
9471013
====
9481014

9491015
🍺 Playground generated with: [playground](https://github.com/jas/playground) by [@jasonsandmeyer](http://twitter.com/jasonsandmeyer)
9501016

951-
🚀 How to generate playground (+zip) from this README: [GENERATE.markdown](https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.markdown)
1017+
📖 Descriptions from: [Gang of Four Design Patterns Reference Sheet](http://www.blackwasp.co.uk/GangOfFour.aspx)
9521018

1019+
🚀 How to generate playground (+zip) from this README: [GENERATE.markdown](https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.markdown)

docs/behavioral/chain_of_responsibility.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
##🐝 Chain Of Responsibility
22

3-
**Source:**
3+
The chain of responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.
4+
5+
**Example:**
6+
47
```swift
58
class MoneyPile {
69
let value: Int

docs/behavioral/command.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
##👫 Command
22

3+
The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.
4+
5+
**Example:**
6+
37
```swift
48
protocol FileOperationCommand {
59
init(file: String)

docs/behavioral/iterator.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
##🚧 Iterator
22

3+
The iterator pattern is used to provide a standard interface for traversing a collection of items in an aggregate object without the need to understand its underlying structure.
4+
5+
*No example, yet.*

docs/behavioral/mediator.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
##🚧 Mediator
22

3+
The mediator pattern is used to reduce coupling between classes that communicate with each other. Instead of classes communicating directly, and thus requiring knowledge of their implementation, the classes send messages via a mediator object.
4+
5+
*No example, yet.*

docs/behavioral/memento.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
##💾 Memento
22

3+
The memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.
4+
5+
**Example:**
6+
37
```swift
48
typealias Memento = Dictionary<NSObject, AnyObject>
59

docs/behavioral/observer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##👓 Observer
22

3+
The observer pattern is used to allow an object to publish changes to its state.
4+
Other objects subscribe to be immediately notified of any changes.
5+
6+
**Example:**
7+
38
```swift
49
class StepCounter {
510
var totalSteps: Int = 0 {

docs/behavioral/state.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##🐉 State
22

3+
The state pattern is used to alter the behaviour of an object as its internal state changes.
4+
The pattern allows the class for an object to apparently change at run-time.
5+
6+
**Example:**
7+
38
```swift
49
class Context {
510
private var state: State = UnauthorizedState()

docs/behavioral/strategy.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
##💡 Strategy
22

3+
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
4+
5+
**Example:**
6+
37
```swift
48
protocol PrintStrategy {
59
func printString(string: String) -> String

docs/behavioral/visitor.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
##🏃 Visitor
22

3+
The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.
4+
5+
**Example:**
6+
37
```swift
48
protocol PlanetVisitor {
59
func visit(planet: PlanetEarth)
@@ -41,4 +45,3 @@ let names = planets.map { (planet: Planet) -> String in
4145

4246
names
4347
```
44-

docs/creational/abstract_factory.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##🌰 Abstract Factory
22

3+
The abstract factory pattern is used to provide a client with a set of related or dependant objects.
4+
The "family" of objects created by the factory are determined at run-time.
5+
6+
**Example:**
7+
38
```swift
49
// Protocols.
510

docs/creational/builder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##👷 Builder
22

3+
The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm.
4+
An external class controls the construction algorithm.
5+
6+
**Example:**
7+
38
```swift
49
protocol ThreeDimensions {
510
var x: Double? {get}

docs/creational/factory.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
##🏭 Factory Method
22

3+
The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.
4+
5+
**Example:**
6+
37
```swift
48
protocol Currency {
59
func symbol() -> String

docs/creational/prototype.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##🃏 Prototype
22

3+
The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone.
4+
This practise is particularly useful when the construction of a new object is inefficient.
5+
6+
**Example:**
7+
38
```swift
49
class ThieveryCorporationPersonDisplay {
510
var name: String?

docs/creational/singleton.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
##💍 Singleton
2+
3+
The singleton pattern ensures that only one object of a particular class is ever created.
4+
All further references to objects of the singleton class refer to the same underlying instance.
5+
6+
**Example:**
7+
28
```swift
39
class SingletonClass {
410
class var shared : SingletonClass {

docs/footer.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ Info
33

44
🍺 Playground generated with: [playground](https://github.com/jas/playground) by [@jasonsandmeyer](http://twitter.com/jasonsandmeyer)
55

6-
🚀 How to generate playground (+zip) from this README: [GENERATE.markdown](https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.markdown)
6+
📖 Descriptions from: [Gang of Four Design Patterns Reference Sheet](http://www.blackwasp.co.uk/GangOfFour.aspx)
77

8+
🚀 How to generate playground (+zip) from this README: [GENERATE.markdown](https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.markdown)

docs/header.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ A short cheat-sheet with Xcode 6 Playground ([Design-Patterns.playground.zip](ht
1414
* [Behavioral](#behavioral)
1515
* [Creational](#creational)
1616
* [Structural](#structural)
17-

docs/structural/adapter.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
##🔌 Adapter
2+
3+
The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.
4+
5+
**Example:**
6+
27
```swift
38
// WARNING: This example uses Point class from Builder pattern!
49

@@ -27,6 +32,7 @@ extension PointConverter{
2732

2833
}
2934
```
35+
3036
**Usage:**
3137
```swift
3238
var tuple = PointConverter.convert(x:1.1, y:2.2, z:3.3, base:2.0, negative:true)

docs/structural/bridge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
##🌉 Bridge
2+
3+
The bridge pattern is used to separate the abstract elements of a class from the implementation details, providing the means to replace the implementation details without modifying the abstraction.
4+
5+
**Example:**
6+
27
```swift
38
protocol Switch {
49
var appliance: Appliance {get set}

docs/structural/composite.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
##🌿 Composite
22

3+
The composite pattern is used to create hierarchical, recursive tree structures of related objects where any element of the structure may be accessed and utilised in a standard manner.
4+
5+
**Example:**
6+
37
```swift
48
/**
59
* Component

docs/structural/decorator.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
##🍧 Decorator
22

3+
The decorator pattern is used to extend or alter the functionality of objects at run- time by wrapping them in an object of a decorator class.
4+
This provides a flexible alternative to using inheritance to modify behaviour.
5+
6+
**Example:**
7+
38
```swift
49
protocol Coffee {
510
func getCost() -> Double

0 commit comments

Comments
 (0)