The factory pattern belongs in the category of the Creational Design pattern. If you take the word “factory” literally. How does a real factory work? You bring in some raw material and do some processing on it and get an output on the outside. For example:
Raw plastic –> Factory –> Some plastic moulds
As the category suggests itself. In the factory pattern, an interface/class creates an object but lets the subclass infer which object to instantiate. All the subclasses implement a common interface that can have one or more functions. You must be wondering why is there an example of a real factory above?
Well, continue reading.
Consider the following UML representing the structure of a Class system in airlines.
Have a look at the following chunks of code:
protocol TravelClassProtocol { func getFlightRate() -> Int } enum TravelType { case first case business case economy } class FirstClass: TravelClassProtocol { func getFlightRate() -> Int { return 1000 } } class BusinessClass: TravelClassProtocol { func getFlightRate() -> Int { return 750 } } class EconomyClass: TravelClassProtocol { func getFlightRate() -> Int { return 500 } } class TravelClassFactory { func getTravelClassFrom(type: TravelType) -> TravelClassProtocol { var travelClass: TravelClassProtocol switch type { case .first: travelClass = FirstClass() case .business: travelClass = BusinessClass() case .economy: travelClass = EconomyClass() } return travelClass } }
interface TravelClassInterface { fun getFlightRate(): Int } enum class TravelType { FIRST, BUSINESS, ECONOMY } class FirstClass : TravelClassInterface { override fun getFlightRate(): Int { return 1000 } } class BusinessClass : TravelClassInterface { override fun getFlightRate(): Int { return 750 } } class EconomyClass : TravelClassInterface { override fun getFlightRate(): Int { return 500 } } class TravelClassFactory { fun getTravelClassFrom(type: TravelType): TravelClassInterface { var travelClass: TravelClassInterface when (type) { TravelType.FIRST -> { travelClass = FirstClass() } TravelType.BUSINESS -> { travelClass = BusinessClass() } TravelType.ECONOMY -> { travelClass = EconomyClass() } } return travelClass } }
Create an object of TravelClassFactory. Call getTravelClassFrom from the object. Pass in .business/TravelType.BUSINESS as an argument. TravelClassFactory returns a particular TravelClass based on the type.
let travelClassFactory = TravelClassFactory() let travelClass: TravelClassProtocol = travelClassFactory.getTravelClassFrom(type: .business) travelClass.getFlightRate() // returns 750
val travelClassFactory = TravelClassFactory() val travelClass: TravelClassInterface = travelClassFactory.getTravelClassFrom(TravelType.BUSINESS) travelClass.getFlightRate() // returns 750
It helps in containing all the object creation logic in a single class. It provides you an object based on the data passed in. It also promotes loose-coupling. It depends on the interface or abstract class rather than a concrete class.
The factory class itself handles making objects. Thus, making it hard to mock that particular object. Factory classes may go bigger and difficult to maintain.
Use a factory pattern when dealing with the creation of many similar objects. If your requirements don’t include the creation of multiple objects. Then using it is not advised. It’s always good to have a concise factory rather than bloating it with all kinds of stuff.
If you have any questions please feel free to leave them in the comment section below.
This article covers some important things you must know when you are considering a move…
What is Unit Testing? In its simplest term, unit testing is testing a small piece…
In this tutorial, you will go through the use of targets to achieve two separate…
In this article, you will learn about a type of Structural Design Pattern which is…
In this article you will learn about a type of Creational Design Pattern which is…
Design Patterns are an important part of a Software design. In this article you'll learn…