Skip to content

Commit

Permalink
Added Builder Pattern to Suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Boldt committed Feb 11, 2017
1 parent 7b2171e commit 98bcd9a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//: [Previous](@previous)

import Foundation

class StudentBuilder {
var firstName : String?
var lastName : String?
var age : Int?

init(buildClosure: (StudentBuilder) -> ()){
buildClosure(self)
}
}

struct Student {
let firstName : String
let lastName : String
let age : Int

init?(builder: StudentBuilder) {
if let firstName = builder.firstName, let lastName = builder.lastName, let age = builder.age {
self.firstName = firstName
self.lastName = lastName
self.age = age
} else {
return nil
}
}
}

let studentBuilder = StudentBuilder{ builder in
builder.age = 10
builder.firstName = "Sebastian"
builder.lastName = "Boldt"
}

let student = Student(builder: studentBuilder)
student?.lastName
student?.firstName
student?.age

//: [Next](@next)
1 change: 1 addition & 0 deletions Creational.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<page name='Singleton'/>
<page name='Prototype'/>
<page name='Factory Method'/>
<page name='Builder'/>
</pages>
</playground>
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* βœ… Singleton
* βœ… Prototype
* βœ… Factory Method
* βœ… Builder
* ❌ Abstract Factory
* ❌ Builder


##Structural
Expand Down

0 comments on commit 98bcd9a

Please sign in to comment.