iOS Design Patterns

Felicity Johnson
4 min readDec 17, 2016

--

No, I don’t mean background patterns and UI layout. The concept of “design patterns” in Swift is a method for structuring the code within your app in such a way that it can be reused. There are three main design pattern categories: Creational, Structural, and Behavioral.

Creational Design Patterns

As the name suggests, creational design patterns are used when writing code to create an object. Some examples include:

Builder: creates a complex object composed of components that must be created in a specific order or using a specific algorithm that is controlled by an external class. Think of a factory that makes shoes. The factory has a line of people, each in a specific order with a specific task. The shoe is not complete until each person has performed his/her task at the correct moment within the line of people.

Singleton: only one instance of the singleton class is created and all references to this class is this one, unique instance.

Other Creational design patterns include: Abstract Factory, Prototype, and Factory Method.

Structural Design Patterns

Structural design patterns are used to establish a structure for realizing relationships between entities. Some examples include:

Adapter: links two otherwise incompatible types by wrapping the protocol of one class with a class that supports the protocol required by the client. Think “appliance adapter,” where the appliance is one class and the adapter is the “wrapper” that allows the “appliance,” or class, to interact with the client. In the code below, VetInformation acts as the “adapter” between the protocol Cat, which has a property of type Int, and the struct Vet, which has a property of type NSNumber.

Decorator: provides an alternative way to implementing inheritance to modify an object’s behavior; the decorator pattern wraps the object inside a decorator class that alters the functionality of the object at run time. This is useful when you would like to create many similar objects without creating several subclasses of similar code.

Other Structural design patterns include: Bridge, Facade, Flyweight, and Proxy.

Behavioral Design Patterns

Behavioral design patterns recognize common communication patterns between objects. Some examples include:

Observer: allows an object to publish changes to its state and other objects subscribe to be notified of any changes. In the example below, ImageCell’s state changes and the Observer class is immediately notified of this change.

Memento: captures the current state of an object and stores it in such a way that it can be restored at a later time. Implementing the memento design pattern makes sense when you know that you would like to return to a previous state. For instance, in the example below I am stepping through the “what comes first?” with the chicken and an egg cycle; when the egg becomes “old,” the chicken becomes “young,” and vice versa.

Iterator: provides a protocol to access a collection of elements while keeping track of the current element and without needing to understand the underlying collection of elements. Within the example below, the underlying element could be any property since it is taking in a generic parameter. The one stipulation is that the struct MUST conform to the Sequence protocol.

Other Behavioral design patterns include: Chain of Responsibility, Command, Interpreter, State, Mediator, and Visitor.

--

--