Design Patterns Journey With iOS-1
Factory Pattern
A design pattern is a reusable solution developed by developers to solve the difficulties, deadlocks, and time losses that developers frequently encounter in software development.
Design patterns are not a framework, tool, algorithm, or code. Nor is it a specific language, platform, or similar software tool. The design pattern is a solution, it’s architecture.
In this series of articles, I will try to tell you how to use the most popular design patterns with iOS.
Types of design patterns
Let’s start with the first pattern of this list
Factory Method is an interface that can create objects from a superclass and give subclasses the ability to modify these objects.
For example, let’s consider a factory that creates vehicle types. Let this factory produce air, sea, and land vehicles. Each vehicle type represents a different class, while the factory is a class that can have them all.
Starting from this example, using the factory method provides some convenience to us. The vehicle factory allows the subclasses of its vehicle creation and driving functions to access and modify, thus enabling the creation of new objects through these functions.
With the image above, maybe a very noticeable operation may not have been done, only the relationship between vehicle types and the factory may seem to be shown, but thanks to this distinction,
Now, you can override the factory method in a subclass and change the class of products being created by the method.
Now, if we consider the diagram above, each tool can adapt and use the “drive” method acquired through the interface according to its own characteristics. In general, all vehicles will use the superclass drive method according to their own needs.
Let’s see how can we implement a sample for swift
import Foundation
protocol VehicleFactory {
func createVehicle() -> Vehicle
}
extension VehicleFactory {
func createNewVehicle() -> Vehicle{
let newVehicle = createVehicle()
return newVehicle
}
}
protocol Vehicle {
// Basic Vehicle feature and information
var productionYear: Int {get}
func driveVehicle()
}
class SeaVehicleFactory : VehicleFactory {
func createVehicle() -> Vehicle {
return SeaVehicle()
}
}
class SeaVehicle : Vehicle {
/**
- You can create init function for this Vehicle Object
*/
var productionYear: Int = 2021
func driveVehicle() {
//Drive sea vehicle with specific way
}
}
class LandVehicleFactory : VehicleFactory {
func createVehicle() -> Vehicle {
return LandVehicle()
}
}
class LandVehicle : Vehicle {
/**
- You can create init function for this Vehicle Object
*/
var productionYear: Int = 2022
func driveVehicle() {
//Drive land vehicle with specific way
}
}
class AirVehicleFactory : VehicleFactory {
func createVehicle() -> Vehicle {
return AirVehicle()
}
}
class AirVehicle : Vehicle{
/**
- You can create init function for this Vehicle Object
*/
var productionYear: Int = 2023
func driveVehicle() {
//Drive air vehicle with specific way
}
}
I hope I have been able to convey one of the most useful and popular patterns to you simply. See you in the next section.