Factory pattern is a creational pattern that provides an interface for creating objects in the superclass, but allows sub-classes to alter the type of objects that will be created.
A creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
Now let’s talk about why we want to use the Factory pattern? (The problem) :
Let’s say we are building a transportation application like uber, at first you only support user transportation using cars so your code is basically about cars and how it is gonna work with cars. Now let’s imagine your business is growing and you will add a new type of transportation using helicopters, ships, or any other type of transportation. Woww this is great news for your business but what about your code? If you remember you build the application at first for cars only that’s mean your code is coupled to Car Class so adding helicopters or ships will effect with changes in the entire codebase and every time you want to add a new transportation type you will need to do more and more changes and at last you will find you ended with pretty nasty code with dozens of conditions switches to handle transportation type.
How Factory Pattern will solve our problem?
The Factory Method pattern will replace direct object construction calls with calls to a special factory method. Objects returned by a factory method are often referred to as products.
The structure:-
The Product: declares the interface, which is common to all objects that can be produced by the creator and its subclasses.
Concrete Products: are different implementations of the product interface.
The Creator class: declares the factory method that returns new product objects. It’s important that the return type of this method matches the product interface.
Concrete Creators: override the base factory method so it returns a different type of product.
It's Implementation Time🤓:
protocol TransportationCreator {
func factoryMethod() -> Transporation
}
class CarCreator: TransportationCreator {
public func factoryMethod() -> Transporation {
return Car()
}
}
class HelicaptorCreator: TransportationCreator {
public func factoryMethod() -> Transporation {
return Helicopter()
}
}
protocol Transporation {
func operation() -> String
}
class Car: Transporation {
func operation() -> String {
return "This is Car Operation"
}
}
class Helicopter: Transporation {
func operation() -> String {
return "This is Helicopter Operation"
}
}
Cons & Pros of Factory Design Pattern :
- Cons :
You avoid tight coupling between the creator and the concrete products.
Single Responsibility Principle. You can move the product creation code into one place in the program, making the code easier to support.
Open/Closed Principle. You can introduce new types of products into the program without breaking existing client code
- Pros:
- The code may become more complicated when you need to introduce a lot of new subclasses to implement the pattern
That’s it hopefully you got it and it was simple and clear for you, if you found this article helpful please leave some claps and share it so everyone can benefit from it too
Greetings to SwiftCairo ❤️🇪🇬