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

Prototype Design Pattern: A Practical Guide to Building a Company Directory

1/14/2023

0 Comments

 
Picture
The prototype design pattern is a popular design pattern in software development that allows for the creation of new objects by copying pre-existing objects. This pattern is useful in situations where creating a new object from scratch is either impractical or inefficient.

The prototype design pattern involves creating a prototype object that serves as the blueprint for creating new objects. When a new object is needed, the prototype object is cloned, and the clone is then customized to meet the specific needs of the new object. This eliminates the need for creating a new object from scratch, which can be time-consuming and resource-intensive.

Another advantage of the prototype design pattern is that it allows for greater flexibility in object creation. Since the prototype object serves as the blueprint for creating new objects, developers can easily modify the prototype object to meet the needs of different projects. This allows for a more modular and reusable approach to object creation, which can be beneficial for large-scale software development projects.
Imagine a company with three office locations that wants to create a directory of employees and the offices they work at. To do this, we can use the prototype design pattern by creating an Employee class and an Address class. As an example, let's consider how these classes might be implemented.

First, we will start with the `Employee` class:

    
Next, the `Address` class:

    
Let's first examine how copying an instance of the Employee class could result in problems before implementing the Prototype design pattern.

    

🚨 🚨 🚨

What happened to Sara? I thought she was a Project Manager at the San Francisco office. However, when Jon made a copy of a class object, the data was passed by reference instead of value, which resulted in Jon overwriting Sara's information.
To solve this issue, we will implement a protocol called `Copyable` that both of our classes will conform to. We will then explore three different options for creating a clone of an object by copying the current one.

    
When implementing the `init` method, both the `Employee` and `Address` classes must be implemented with the `required init` keyword.

    
Before we examine an example of the `required init` method, let's create static constants for the three offices to use instead of instantiating the `Employee` class each time as in the first example.

    
Now, we can copy the `Employee.prototypeSanFranciscoOffice` and customize it to our needs:

    
This approach allows for the creation of new Employee objects in a more efficient and flexible way, using the prototype design pattern.
Option 2 involves the use of the `associatetype` keyword. If you are unfamiliar with this keyword, please refer to my previous blog post which provides in-depth explanation.

    

    
Here is an example of how to use the `clone2` method to make a deep copy the employee instance:

    
You may have noticed that the cloning methods are functioning correctly, but it can be inconvenient to have to set the properties separately each time. Wouldn't it be more efficient to set the properties within the cloning method itself?

    
First, let's create an `enum` to list the properties for the class. This will help us avoid any mistakes in spelling string literals and allow us to easily select from the available `enum` cases.

    
Here is an example of using the clone3 method to create a deep copy of an employee instance, with the option to pass in as many or as few parameters as needed:

    
In conclusion, the Prototype design pattern is a useful and efficient way to create new objects by copying existing objects. It allows for the creation of new objects without the need for costly and time-consuming processes such as initialization or setup. By implementing the Prototype design pattern, you can easily and quickly create new objects that are ready for use. Whether you're working on a small project or a large-scale application, the Prototype design pattern can be a valuable tool in your software development toolkit.
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