Creational Design Pattern — Factory Method in iOS-Swift

Saeid Rezaeisadrabadi
4 min readMay 19, 2021

In this story, I’m going to describe one of my favorite design patterns.

It can be extremely useful and it has basically no drawbacks. Factory Method!

Factory Pattern

The factory method lets us create instances of types that have a common superclass, or ones that conform to a given protocol.

The callers don’t need to know the complete implementation types, just the base class protocol. Thus, we can eliminate the dependencies between the implementation types and the clients.

The factory method doesn’t have any drawbacks but its usage is limited to types that share a common protocol or superclass. Now you can make those types without the protocol or superclass to make them work but only do that if the types are indeed related.

You must not enforce protocol conferments to types that have different responsibilities just for the sake of the factory method design pattern.

The main idea behind the Factory Method Pattern is to identify a type, without exposing it.

I have created a simple demonstration of implementing and using the factory method. you can find it at the end of the story.

let's describe how to implement the Factory Method pattern.

first, we need to define a protocol that has a serialize() method.

Next, I define three classes that conform to the serializable protocol.

First, I create a class called Jason serializer. And it conforms to serializable, so we have to implement the serialize method.

In this story, I will simply return the message and don't dig into implementing the serializer class.

Then I declare another class called Property List Serializer.

And a third one called XML serializer.

Instead of exposing all these classes, clients should only know about the serializable protocol.

But then how can callers create an XML serializer instance?

At the core of the Factory Method Pattern is a method that encapsulates the object creation. The method takes an argument that identifies the implementation class.

This argument is usually an enumeration or a predefined constant. So let’s create an enumeration, I’m going to call it SerializerType, and it will have three cases that match our classes.

Now we can start defining a function responsible for creating the serializable objects. Let’s call it makeSerializer.

The function's parameter would be of type serializer, the enumeration that identifies the types.

The method's return type should be the common protocol instead of a concrete type. In our case, the return type is the serializable protocol.

because I'm not in favor of using global functions, embed the make serializer method in a dedicated factory type. for example Struct

So I can simply create JSONSerializer.

let jsonSerializer = SerializerFactory.makeSerializer(.json)

The returned object is of type JSONSerializer, but that’s hidden from the caller. All caller knows is that it received an object of a type that conforms to the serializable protocol and that object can be used to serialize Jason Data.

The source code of the example, located at

https://github.com/saeed-rz/Design-Patterns-iOS

also, you can read other creational pattern stories

PrototypePattern

SingletonPattern

--

--