Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of Swift:
A Tech Blog Series

Welcome to our tech blog series dedicated to exploring the world of Swift and its related topics. Our goal is to provide valuable insights and in-depth analysis on the latest advancements in the Swift programming language, including design patterns and data structures. Stay tuned for our upcoming articles and join the discussion on the exciting world of Swift programming!

Explore all Swift Topics

From Crust to Toppings: Using the Builder Design Pattern to Make a Pizza

1/7/2023

0 Comments

 
Picture
The Builder design pattern is a creational design pattern that allows developers to create complex objects step by step. It separates the construction of an object from its representation, making it easier to create different representations of the same object.

One common example of the Builder design pattern is the creation of a pizza. The pizza has various ingredients such as crust, meats, sauces, and toppings that can be added in different combinations to create a unique pizza. The Builder design pattern allows a developer to specify the steps for creating a pizza, while leaving the actual construction to a separate object.

In this example, the pizza would be the complex object that is being constructed, and the Builder would be the object responsible for creating it. The Builder would have methods for adding the various ingredients to the pizza, such as `setCrust(_ crust: Pizza.Crust)`, `func addMeat(_ meat: Pizza.Meats)`, and `func removeMeat(_ meat: Pizza.Meats)`, `func addSauce(_ sauce: Pizza.Sauces)`, etc.

    
In this example, the Pizza class represents the object being constructed, and the PizzaBuilder class provides the methods for constructing the pizza step-by-step. The PizzaBuilder class has methods for setting the size, crust, meats, sauces, and toppings of the pizza, as well as a build() method that returns the completed pizza object.

To use the PizzaBuilder, a client can create a new PizzaBuilder object and then use its methods to customize the pizza being constructed. The PizzaBuilder methods return the PizzaBuilder object itself, allowing for method chaining, which makes it easy to specify the desired characteristics of the pizza in a single line of code. Once the pizza has been customized, the `build()` method can be called to return the completed Pizza object.

    
Here is an example of how to build a pizza:

    
The Director is another important component of the Builder design pattern. The Director is responsible for managing the construction process of a complex object. It does this by using a Builder interface to delegate the construction steps to concrete builder classes.

To create a veggie pizza, we would create an instance of the PizzaBuilder and pass it to the Director. The Director would then call the appropriate static method to add the necessary ingredients in the correct order.

Here is an example:

    
In summary, the Builder design pattern is a useful tool for creating complex objects in a systematic and flexible way. It consists of a Builder object that is responsible for creating the object, and a Director that oversees the construction process by calling the appropriate methods on the Builder.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Picture

    Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.


    Archives

    November 2023
    October 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022


    Categories

    All
    Abstract Factory
    Animation
    Array
    Associatetype
    Avl
    Behavioral
    Bridge
    Builder
    Case
    Chaining
    Chain Of Responsibility
    Class
    Closure
    Coalescing
    Coercion
    Command
    Composite
    Computed Property
    Conditional Conformance
    Crash
    Creational
    Data Structure
    Decorator
    Default Case
    Design Pattern
    Dictionary
    Enum
    Extension
    Facade
    Factory
    Factory Method
    Flyweight
    Function
    Generics
    Graphs
    Guard
    Hash Table
    Heap
    Initialization
    Interpreter
    Ios
    Iterator
    Linked List
    Lowercase
    Mapping
    Mediator
    Memento
    Observer
    Optional
    Pattern Matching
    Protocol
    Prototype
    Proxy
    Quadratic Probing
    Queue
    Search
    Self
    Sequence
    Singleton
    Stack
    State
    Strategy
    Strikethrough
    Struct
    Structural
    Subscript
    Swift
    Template Method
    Text
    Tree
    Tries
    Try
    UIKit
    Uilabel
    UITableView
    Uppercase
    Visitior

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk