The Factory Pattern using Swift
Create objects the right way!

Difficulty: Beginner | Easy | Normal | Challenging
The factory pattern provides an interface for creating objects in a superclass. This article provides some details around this design pattern, and also has some examples of it! We should get building!
A factory
In programming, a factory is an object for creating other objects. In Swift that object will typically be a class, and will produce a concrete instance of an object. In the original book it is declared that there is no strict factory pattern, but rather a factory method pattern and an abstract factory pattern.
The idea of this article is to cover both.
Why use a factory?
Complexity
Instantiations may be complex, and encapsulating instantiation can simplify creating concrete instances by defining a single place. This means that we can follow the Dependency Inversion Principle, and even open the door to dependency injection.
Decouple the use of an object from creating it
We can abstract our code, so where modifications are made to a class the client of that class can continue to use it without further modificaiton. By implementing to an interface we are provided with an abstraction rather than a concrete type, so code is protected from unwanted implementation details.
The difference between a factory method and an abstract factory
The factory method is (well, obviously) a method, and an abstract factory is an object. Factory methods can be overridden in a subclass. An abstract factory is an object that can have multiple factory methods.
The Factory Method Pattern
A factory protocol will return new objects. In Swift we usually code to a protocol, an extension could provide a defailt implementation but there will usually be a concrete instance of this factory.
Since we are using Swift this overriding is NOT something that we would usually do. Overriding a function? I’d rather not, thank you.
In any case, here is the code. In this example we will create a factory to create a Struct
that just holds the name as a String
:
The Abstract Pattern
This particular guide is going to give you a definition for the abstract factory pattern.That is, the abstract:
“Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”
In this example we will create a factory to create a Struct
that just holds the name as a String
:
The Factory example
We can set up a factory method for the property initialization, this lives inside an abstract factory.
of course we are in the Swift world, so coding to protocols means that we can swap out the factory.
Here is the code:
so when we decide to test, we can use the following:
which can then be elegantly swapped out in the test:
A Factory making use of Enum
It can make sense to create a complicated object using Swift’s rather nice Enum
. Now this isn't something I've used in production (for a URL) since I use my own URLBuilder for this functionality, but this gives a good example of using a factory in real code!
Conclusion
This is used in my article Dependency Injection using Storyboards, and I would recommend that you think seriously about using the factory pattern — in Swift projects it is rather well-used as well as being useful for conversations about projects.
If you’ve any questions, comments or suggestions please hit me up on Twitter