Skip to content

Commit cfb60d8

Browse files
committed
Merge pull request ochococo#7 from fjbelchi/CompositeAndFactoryPatterns
Factory method and Composite Patterns added
2 parents 5315af5 + fe029ea commit cfb60d8

23 files changed

+397
-114
lines changed

Design-Patterns.playground.zip

3.11 KB
Binary file not shown.

Design-Patterns.playground/Documentation/section-19.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h1 id="structural">Structural</h1>
15-
<blockquote>
16-
<p>In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.</p>
17-
<p><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Structural_pattern">wikipedia.org</a></p>
18-
</blockquote>
19-
<h2 id="composite">Composite</h2>
20-
<h2 id="fa-ade">Façade</h2>
14+
<h2 id="factory-method">Factory Method</h2>
2115

2216
</section>
2317
</div>

Design-Patterns.playground/Documentation/section-23.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="adapter">Adapter</h2>
14+
<h1 id="structural">Structural</h1>
15+
<blockquote>
16+
<p>In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.</p>
17+
<p><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Structural_pattern">wikipedia.org</a></p>
18+
</blockquote>
19+
<h2 id="composite">Composite</h2>
1520

1621
</section>
1722
</div>

Design-Patterns.playground/Documentation/section-27.html

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,7 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="bridge">Bridge</h2>
15-
<h2 id="decorator">Decorator</h2>
16-
<h2 id="proxy">Proxy</h2>
17-
<h1 id="behavioral">Behavioral</h1>
18-
<blockquote>
19-
<p>In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.</p>
20-
<p><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Behavioral_pattern">wikipedia.org</a></p>
21-
</blockquote>
22-
<h2 id="chain-of-responsibility">Chain Of Responsibility</h2>
23-
<h2 id="command">Command</h2>
24-
<h2 id="iterator">Iterator</h2>
25-
<h2 id="mediator">Mediator</h2>
26-
<h2 id="memento">Memento</h2>
27-
<h2 id="observer">Observer</h2>
28-
<h2 id="state">State</h2>
29-
<h2 id="strategy">Strategy</h2>
14+
<h2 id="fa-ade">Façade</h2>
3015

3116
</section>
3217
</div>

Design-Patterns.playground/Documentation/section-31.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="visitor">Visitor</h2>
14+
<h2 id="adapter">Adapter</h2>
1515

1616
</section>
1717
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 36</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<h2 id="bridge">Bridge</h2>
15+
<h2 id="decorator">Decorator</h2>
16+
<h2 id="proxy">Proxy</h2>
17+
<h1 id="behavioral">Behavioral</h1>
18+
<blockquote>
19+
<p>In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.</p>
20+
<p><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Behavioral_pattern">wikipedia.org</a></p>
21+
</blockquote>
22+
<h2 id="chain-of-responsibility">Chain Of Responsibility</h2>
23+
<h2 id="command">Command</h2>
24+
<h2 id="iterator">Iterator</h2>
25+
<h2 id="mediator">Mediator</h2>
26+
<h2 id="memento">Memento</h2>
27+
<h2 id="observer">Observer</h2>
28+
<h2 id="state">State</h2>
29+
<h2 id="strategy">Strategy</h2>
30+
31+
</section>
32+
</div>
33+
</body>
34+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 38</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<p><strong>Usage:</strong></p>
15+
16+
</section>
17+
</div>
18+
</body>
19+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 40</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<h2 id="visitor">Visitor</h2>
15+
16+
</section>
17+
</div>
18+
</body>
19+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 42</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<p><strong>Usage:</strong></p>
15+
16+
</section>
17+
</div>
18+
</body>
19+
</html>

Design-Patterns.playground/contents.xcplayground

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,21 @@
7171
</documentation>
7272
<code source-file-name="section-34.swift">
7373
</code>
74+
<documentation relative-path="section-35.html">
75+
</documentation>
76+
<code source-file-name="section-36.swift">
77+
</code>
78+
<documentation relative-path="section-37.html">
79+
</documentation>
80+
<code source-file-name="section-38.swift">
81+
</code>
82+
<documentation relative-path="section-39.html">
83+
</documentation>
84+
<code source-file-name="section-40.swift">
85+
</code>
86+
<documentation relative-path="section-41.html">
87+
</documentation>
88+
<code source-file-name="section-42.swift">
89+
</code>
7490
</sections>
7591
</playground>
Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
class Eternal{
1+
protocol Currency {
2+
func symbol() -> String
3+
func code() -> String
4+
}
25

3-
class func setObject(value: AnyObject!, forKey defaultName: String!){
4-
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
5-
defaults.setObject(value,forKey:defaultName)
6-
defaults.synchronize()
6+
class Euro : Currency {
7+
func symbol() -> String {
8+
return ""
79
}
10+
11+
func code() -> String {
12+
return "EUR"
13+
}
14+
}
815

9-
class func objectForKey(defaultName: String!) -> AnyObject!{
10-
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
11-
return defaults.objectForKey(defaultName)
16+
class UnitedStatedDolar : Currency {
17+
func symbol() -> String {
18+
return "$"
19+
}
20+
21+
func code() -> String {
22+
return "USD"
1223
}
24+
}
1325

26+
enum Country {
27+
case UnitedStates, Spain, France, UK
28+
}
29+
30+
class CurrencyFactory {
31+
class func currencyForCountry(country:Country) -> Currency? {
32+
switch country {
33+
case .Spain, .France :
34+
return Euro()
35+
case .UnitedStates :
36+
return UnitedStatedDolar()
37+
default:
38+
return nil
39+
}
40+
41+
}
1442
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
Eternal.setObject("Disconnect me. I’d rather be nothing",forKey:"Bishop")
1+
let noCurrencyCode = "No Currency Code Available"
22

3-
Eternal.objectForKey("Bishop")
3+
CurrencyFactory.currencyForCountry(.Spain)?.code() ?? noCurrencyCode
4+
CurrencyFactory.currencyForCountry(.UnitedStates)?.code() ?? noCurrencyCode
5+
CurrencyFactory.currencyForCountry(.France)?.code() ?? noCurrencyCode
6+
CurrencyFactory.currencyForCountry(.UK)?.code() ?? noCurrencyCode
Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1-
// WARNING: This example uses Point class from Builder pattern!
2-
3-
class PointConverter{
1+
/**
2+
* Component
3+
*/
4+
protocol Shape {
5+
func draw(fillColor:String)
6+
}
47

5-
class func convert(#point:Point, base:Double, negative:Bool) -> Point{
8+
/**
9+
* Leafs
10+
*/
11+
class Square : Shape {
12+
func draw(fillColor: String) {
13+
print("Drawing a Square with color \(fillColor)")
14+
}
15+
}
616

7-
var pointConverted = Point{
8-
if let x = point.x{ $0.x = x * base * (negative ? -1.0 : 1.0) }
9-
if let y = point.y{ $0.y = y * base * (negative ? -1.0 : 1.0) }
10-
if let z = point.z{ $0.z = z * base * (negative ? -1.0 : 1.0) }
11-
}
12-
13-
return pointConverted
17+
class Circle : Shape {
18+
func draw(fillColor: String) {
19+
print("Drawing a circle with color \(fillColor)")
1420
}
1521
}
1622

17-
extension PointConverter{
23+
/**
24+
* Composite
25+
*/
26+
class Whiteboard : Shape {
27+
lazy var shapes = [Shape]()
1828

19-
class func convert(#x:Double!, y:Double!, z:Double!, base:Double!, negative:Bool!) -> (x:Double!,y:Double!,z:Double!){
20-
21-
var point = Point{ $0.x = x; $0.y = y; $0.z = z }
22-
var pointCalculated = self.convert(point:point, base:base, negative:negative)
23-
24-
return (pointCalculated.x!,pointCalculated.y!,pointCalculated.z!)
29+
init(_ shapes:[Shape]) {
30+
self.shapes = shapes
31+
}
32+
33+
func draw(fillColor:String) {
34+
for shape in self.shapes {
35+
shape.draw(fillColor)
36+
}
2537
}
26-
2738
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
var tuple = PointConverter.convert(x:1.1, y:2.2, z:3.3, base:2.0, negative:true)
2-
3-
tuple.x
4-
tuple.y
5-
tuple.z
1+
var whiteboard = Whiteboard([Circle(), Square()])
2+
whiteboard.draw("Red")
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
1-
protocol PrintStrategy {
2-
func printString(string: String) -> String
3-
}
1+
class Eternal{
42

5-
class Printer {
6-
7-
let strategy: PrintStrategy
8-
9-
func printString(string:String)->String{
10-
return self.strategy.printString(string);
11-
}
12-
13-
init(strategy: PrintStrategy){
14-
self.strategy = strategy
3+
class func setObject(value: AnyObject!, forKey defaultName: String!){
4+
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
5+
defaults.setObject(value,forKey:defaultName)
6+
defaults.synchronize()
157
}
16-
}
178

18-
class UpperCaseStrategy: PrintStrategy{
19-
func printString(string:String)->String{
20-
return string.uppercaseString;
9+
class func objectForKey(defaultName: String!) -> AnyObject!{
10+
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
11+
return defaults.objectForKey(defaultName)
2112
}
22-
}
2313

24-
class LowerCaseStrategy: PrintStrategy{
25-
func printString(string:String)->String{
26-
return string.lowercaseString;
27-
}
2814
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var lower = Printer(strategy: LowerCaseStrategy())
2-
lower.printString("O tempora, o mores!")
1+
Eternal.setObject("Disconnect me. I’d rather be nothing",forKey:"Bishop")
32

4-
var upper = Printer(strategy: UpperCaseStrategy())
5-
upper.printString("O tempora, o mores!")
3+
Eternal.objectForKey("Bishop")

0 commit comments

Comments
 (0)