Using Cookie Cutter To Understand Design Patterns in Swift

Explaining the Design Pattern in Swift with a Real-world Analogy

Shashank Thakur
Level Up Coding

--

Prototype Design Pattern in Swift
Photo by Anshu A on Unsplash

Design patterns are like architectural blueprints for building software applications. They provide solutions to common programming challenges and help in creating clean, efficient, and maintainable code. One such design pattern is the Prototype pattern, which is a creational pattern used to create objects by copying an existing object, known as the prototype. To make this pattern easier to understand, let’s explore it with a real-world analogy, and how it can be applied in Swift, a popular language for iOS and macOS development.

The Real-World Analogy: The Cookie Cutter

Imagine you’re baking cookies, and you want each cookie to have the same shape and design. To achieve this, you use a cookie cutter. A cookie cutter is a mold that defines the shape of the cookies you want to create. Instead of shaping each cookie by hand, you press the cookie cutter onto the dough, and voilà, you have uniformly shaped cookies. This process is quite similar to the Prototype design pattern.

The Prototype Pattern in Swift

In the world of software development, the Prototype pattern allows us to create new objects by cloning an existing object, just as the cookie cutter allows us to create cookies with the same shape. Let’s break down the key components of the Prototype pattern in Swift using the cookie-cutter analogy:

  1. Prototype: In our analogy, the prototype is the original cookie cutter. It defines the structure and properties that the newly created objects will inherit.
  2. Concrete Prototype: These are specific cookie cutters that implement the prototype. Each concrete prototype (cookie cutter) defines a unique shape and design.
  3. Client: The client represents the baker who uses the cookie cutters (prototypes) to create cookies. In software, the client is responsible for creating objects by cloning the prototypes.

Let’s implement the Prototype pattern in Swift using a simplified example:

import Foundation

// Step 1: Define the Prototype (Cookie Cutter) Protocol
protocol CookieCutter {
func clone() -> CookieCutter
func cutCookie()
}

// Step 2: Create Concrete Prototypes (Cookie Cutters)
class StarCookieCutter: CookieCutter {
func clone() -> CookieCutter {
return StarCookieCutter()
}

func cutCookie() {
print("Cutting a star-shaped cookie")
}
}

class HeartCookieCutter: CookieCutter {
func clone() -> CookieCutter {
return HeartCookieCutter()
}

func cutCookie() {
print("Cutting a heart-shaped cookie")
}
}

// Step 3: Create a Client (Baker) Class
class Baker {
func bakeCookies(using cookieCutter: CookieCutter) {
let cookie = cookieCutter.clone()
cookie.cutCookie()
}
}

In the code above:

  • We define a CookieCutter protocol, which acts as our prototype. It includes a clone method to create a copy (new cookie cutter) and a cutCookie method to cut the cookie.
  • We create concrete prototypes (cookie cutters), StarCookieCutter and HeartCookieCutter, that conform to the CookieCutter protocol and implement the clone method to create copies.
  • The Baker class acts as the client, and it uses a given cookie cutter to bake cookies. It first clones the cookie cutter to create a new one, and then it cuts cookies using the cutCookie method.

Let’s put this into action:

let baker = Baker()
let starCookieCutter = StarCookieCutter()
let heartCookieCutter = HeartCookieCutter()

baker.bakeCookies(using: starCookieCutter)
baker.bakeCookies(using: heartCookieCutter)

When you run this code, you’ll see that the Baker uses the clone method to create a copy of the specified cookie cutter and then cut cookies using the cutCookie method, just like a real-world baker using a cookie cutter to create cookies.

Conclusion

The Prototype design pattern in Swift, just like a cookie cutter, allows us to create objects by copying existing prototypes. This pattern is useful when you want to create numerous objects with similar structures. By understanding the analogy of a cookie cutter, you can grasp the concept of the Prototype pattern more easily and apply it effectively in your Swift applications, making your code more efficient and maintainable.

--

--