Implementing the builder pattern

Let's start off by looking at how to create a complex structure without the builder pattern to see the problem the builder pattern is designed to solve.

The following code creates a structure named BurgerOld and does not use the builder pattern:

struct BurgerOld {  
  var name: String  
  var patties: Int  
  var bacon: Bool 
  var cheese: Bool  
  var pickles: Bool  
  var ketchup: Bool  
  var mustard: Bool  
  var lettuce: Bool  
  var tomato: Bool 
  init(name: String, patties: Int, bacon: Bool, cheese: Bool,       pickles: Bool, ketchup: Bool, mustard: Bool, lettuce: Bool, tomato: Bool) { self.name = name self.patties = patties self.bacon = bacon self.cheese = cheese self.pickles = pickles self.ketchup = ketchup self.mustard = mustard self.lettuce ...

Get Swift 4 Protocol-Oriented Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.