Factory Method Design Pattern in Swift

Last Updated: September 28, 2017

In this article I am going to discuss how you can implement the Factory Method Design Pattern in Swift programming language

According to the GoF definition you can define Factory method like below


Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

This is the UML diagram for Factory Design Pattern. Now we will study the each component in the diagram


Product is the interface of the ConcreteProduct which Factory creates

Concrete Product
implements the Product interface


– declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
– may call the factory method to create a Product object.

Concrete Creator
Overrides the factory method to return an instance of a ConcreteProduct.

Factory Method Design Pattern Example in Swift

Now we will try to implement the Factory Method design pattern in Swift 3

Smart Shirt Inc is a garment factory which produce quality garments. It has several departments for producing Shirts, T-Shirts,Trousers etc. Shirt department produces two types of shirts which are long sleeve shirts and short sleeve shirts

To represent the above information we can draw following UML diagram

Swift Factory

Now we will implement the above diagram in Swift code

IShirt (Product) is the protocol which is the equivalent to the interface of other program like Java,C# etc

protocol IShirt{
    func make()

Next I am going to create two ConcreteClasses named LongSleeveShirt and ShortSleeveShirt. These classes use the proptotype IShirt and implement its make() method

public class LongSleeveShirt: IShirt{
    func make(){
        print("Long Sleeve Shirt Produced")

public class ShortSleeveShirt: IShirt{
    func make(){
        print("Short Sleeve Shirt Produced")

Next I am going to create IShirtFactory proptotype which is equivalent to the Product interface in general UML diagram. This prototype has createShirt() method

protocol IShirtFactory{
    func createShirt(type: String) -> IShirt?

Once you create the IShirtFactory proptotype you can create the ConceteProduct class ShirtFactory

public class ShirtFactory: IShirtFactory{
    func createShirt(type: String) -> IShirt?{
        if(type == "Short Sleeve"){
            return  ShortSleeveShirt()
        if(type == "Long Sleeve"){
            return  LongSleeveShirt()
        return nil

Now you have created all classes and prototypes required for Factory Method Design Pattern. It is time to test the code now. So we will do the Client code now

var shirtFactory: ShirtFactory = ShirtFactory()
var shirt = shirtFactory.createShirt(type: "Short Sleeve")

shirt = shirtFactory.createShirt(type: "Long Sleeve")

Finally you can have the following output
Short Sleeve Shirt Produced
Long Sleeve Shirt Produced