Factory Design Pattern

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.

Class Diagram

Consider the following UML representing the structure of a Class system in airlines.

Implementation

Have a look at the following chunks of code:

SwiftKotlin
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
    }
}

Usage

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.

SwiftKotlin
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

Pros

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.

Cons

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.

Conclusion

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.

Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he's probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago

Design Patterns

Design Patterns are an important part of a Software design. In this article you'll learn…

5 years ago