iOS Interview: Factory Design Pattern

Ravi Ranjan
CodeX
Published in
3 min readJan 15, 2023

The Factory Design Pattern is a creational design pattern that provides a way to create objects without specifying the exact class of object that will be created. In Swift, the factory pattern can be implemented by creating a factory class that has a method for creating objects of a specific type. This method can either be a static method or an instance method, depending on the needs of the application.

One common implementation of the factory pattern in Swift is to use an enumeration to define the different types of objects that can be created. The factory class will have a method that takes in a case of this enumeration and returns an object of the corresponding type.

For example, let’s say we have an app that needs to create different types of vehicles, such as cars, trucks, and motorcycles. We can create an enumeration called “VehicleType” with cases for each type of vehicle. Then, we can create a factory class called “VehicleFactory” that has a method called “createVehicle” that takes in a “VehicleType” case and returns an object of the corresponding type

enum VehicleType {
case car
case truck
case motorcycle
}

class VehicleFactory {
static func createVehicle(type: VehicleType) -> Vehicle {
switch type {
case .car:
return Car()
case .truck:
return Truck()
case .motorcycle:
return Motorcycle()
}
}
}

In this example, the createVehicle method uses a switch statement to determine the type of vehicle to create based on the input case. It then returns a new instance of the corresponding class (Car, Truck, or Motorcycle).

By using the factory pattern, we have separated the process of creating objects from the classes that use them. This allows for more flexibility in the future, as new types of vehicles can be added without affecting the existing code.

Another way to implement the factory pattern in Swift is by using a protocol and extensions. This approach involves creating a protocol that defines the methods and properties that the objects created by the factory must conform to. Then, we can create extensions for this protocol for each specific type of object that we want to create.

For example, let’s say we have a protocol called “Shape” that defines methods for calculating the area and perimeter of a shape. We can create extensions for this protocol for different types of shapes, such as circles, squares, and rectangles.

enum ShapeType {
case circle
case square
case rectangle
}

protocol Shape {
func area() -> Double
func perimeter() -> Double
}

extension Shape {
func create(type: ShapeType) -> Shape {
switch type {
case .circle:
return Circle()
case .square:
return Square()
case .rectangle:
return Rectangle()
}
}
}

In this example, the create method takes in a Shapetype Enum that represents the type of shape to create and returns an object that conforms to the Shape protocol. By using the protocol and extensions approach, we can use the factory pattern to create objects of different types, while still ensuring that they conform to a set of common methods and properties.

In conclusion, The Factory pattern is a great way to create objects in a decoupled and flexible way, giving you the ability to change the creation process without affecting the rest of the codebase.

I have created a video for factory design pattern for the Textfield validator for every input type, the video link is in the comment box. If you liked this, click the 💚 and give a clap on this post as much as you can below so other people will see this here on Medium. If you have any queries or suggestions, feel free to comment or hit me on Twitter, or Linkedin

--

--