Skip to content

Instantly share code, notes, and snippets.

@imjhk03
Last active September 22, 2021 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjhk03/188cdf0ac899cf767b2ec71bf7ec3399 to your computer and use it in GitHub Desktop.
Save imjhk03/188cdf0ac899cf767b2ec71bf7ec3399 to your computer and use it in GitHub Desktop.
Sample code for builder pattern in swift.
import Foundation
enum Dough: String {
case basic
case cheeseCrust
}
enum Topping: String, CaseIterable {
case pepperoni
case sausage
case mushrooms
case bacon
case extraCheese
case greenPeppers
case blackOlives
case onions
}
enum Size: String {
case regular
case large
case family
}
// Product
class Pizza {
private var dough: Dough = .basic
private var toppings = [Topping]()
private var size: Size = .regular
func setDough(_ dough: Dough) {
self.dough = dough
}
func setToppings(_ toppings: [Topping]) {
self.toppings = toppings
}
func setSize(_ size: Size) {
self.size = size
}
func listPizzaInfo() -> String {
var toppingList = toppings.map { $0.rawValue }
return "Your pizza's size is \(size.rawValue) and the dough is \(dough.rawValue), toppings are \(toppingList.joined(separator: ", "))."
}
}
// Builder
protocol Builder {
func reset()
func setDough(_ dough: Dough)
func addToppings(_ toppings: [Topping])
func setSize(_ size: Size)
}
class PizzaBuilder: Builder {
private var pizza = Pizza()
func reset() {
self.pizza = Pizza()
}
func setDough(_ dough: Dough) {
self.pizza.setDough(dough)
}
func addToppings(_ toppings: [Topping]) {
self.pizza.setToppings(toppings)
}
func setSize(_ size: Size) {
self.pizza.setSize(size)
}
func getPizza() -> Pizza {
let result = self.pizza
reset()
return result
}
}
// Director
class Director {
private var builder: Builder?
func buildCheesePizza(_ builder: Builder) {
builder.reset()
builder.setDough(.cheeseCrust)
builder.addToppings([.extraCheese, .mushrooms, .sausage, .greenPeppers, .blackOlives])
builder.setSize(.regular)
}
func buildPepperoniPizza(_ builder: Builder) {
builder.reset()
builder.setDough(.basic)
builder.addToppings([.pepperoni, .mushrooms, .onions, .blackOlives])
builder.setSize(.large)
}
}
class Client {
func makeCheesePizza() {
let director = Director()
let pizzaBuilder = PizzaBuilder()
director.buildCheesePizza(pizzaBuilder)
let cheesePizza = pizzaBuilder.getPizza()
print(cheesePizza.listPizzaInfo())
}
func makePepperoniPizza() {
let director = Director()
let pizzaBuilder = PizzaBuilder()
director.buildPepperoniPizza(pizzaBuilder)
let pepperoniPizza = pizzaBuilder.getPizza()
print(pepperoniPizza.listPizzaInfo())
}
}
let client = Client()
client.makeCheesePizza()
print()
client.makePepperoniPizza()
// Prints
// "Your pizza's size is regular and the dough is cheeseCrust, toppings are extraCheese, mushrooms, sausage, greenPeppers, blackOlives."
// "Your pizza's size is large and the dough is basic, toppings are pepperoni, mushrooms, onions, blackOlives."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment