A design pattern is a general repeatable solution to a commonly occurring problem in software design.

We use many design patterns in our daily coding practice, it is just we are mostly unaware of their names. This post will provide short description on each design pattern, focus is not going in details, but on making you aware what design pattern you might be using or you can use in your daily coding practice, so that you can relate them and study in detail to improve your design. Using what feature of language results in which design pattern is mentioned in each pattern description.

Design patterns are divided into three types

Creational

These patterns are about class/object creation.

E.g - Singleton, Abstract Factory

Structural

These are about class/object composition.

E.g - MVC, Decorator, Adapter, Facade, Composite.

Behavioral

These defines rules for communication between objects.

E.g - Observer, Chain of Responsibility, Command, Delegation

List of frequently used design patterns

1. Singleton

This pattern involves a single class which is responsible to create an object while making sure that only single object gets created.

Note, I have seen many developer write sharedInstance class method and don’t override init method. Just writing sharedInstance class method will only create common accessible instance of class, to make class singleton you need to block all possible ways to create it’s another instance which needs overriding init.

E.g - NSFileManager and UIApplication are singleton classes

2. MVC

This is high level composite pattern it is made up of two patterns Mediator and Observer. Model - What is it. View - How it looks. Controller - What it does

Controller acts as mediator, it also observes changes and updates view about it.

The view sits at the top of the architecture, the controller sits below the view. The model sits below the controller. So the view knows about the controller, the controller knows the model. The view is notified when the model changes. Model and View are unaware of each other’s existence.

There are two more design patterns MVP (Model View Presenter) and MVVM (Model View ViewModel) which are derived from MVC, to avoid confusion I have not mentioned any details about it. Many people nowadays prefer MVP over MVC.

3. Adapter

Acts as bridge between two incompatible interfaces Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. It decouples the client from the class of the targeted object.

If you are using Protocols then you are already using this pattern.

4. Decorator

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This pattern acts as a wrapper to existing class.

Using Category or Subclassing will result into this pattern.

5. Observer

When one object changes state, all its dependents are notified and updated automatically

E.g - NSNotification, KVO

6. Iterator

The Iterator design pattern provides a way to access the elements of an aggregate object (that is, a collection) sequentially without exposing its underlying representation.

E.g - NSEnumerator

7. Command

Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

E.g - NSInvocation

8. Chain of Responsibility (Responder Chain)

Decouples the sender of a request from its receiver by giving more than one object a chance to handle the request.

E.g - NS/UIResponder

9. Composite

Composes related objects into tree structures to represent part-whole hierarchies.

E.g - view hierarchy in window

10. Facade

The pattern defines a higher-level interface that makes the subsystem easier to use by reducing complexity and hiding the communication and dependencies between subsystems.

E.g - NSImage

11. Bouncer

Like real life bouncer it you don’t fit or condition doesn’t match it will refuse you to let you in or avoid further execution.

E.g - Using guard statement in swift or Assert in obj c or just simple if condition check based actions

12. Abstract Factory

It is blueprint or provide an interface for creating families of related or dependent objects without specifying their concrete classes.

13. Factory Method

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Defining a "virtual" constructor in java.

Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.

14. Delegation

The advantage of the delegate design pattern is loose coupling. It enables class A (the delegate) to depend on class B (the delegating class) without class B having to have any knowledge of class A. This ensures that the dependency relationship is one-way only, rather than being circular.

E.g - Protocol

15. Mediator

Promotes loose coupling by keeping objects from referring to each other explicitly, It centralizes complex communication and control logic between objects in a system. These objects tell the mediator object when their state changes and, in turn, respond to requests from the mediator object.

If you are notifying some common class usually singleton about something happened and take this action in different module then you have already used it. Mostly unaware of this pattern people name these type of classes as manager, common controller, action handler etc.

16. Memento

The Memento pattern captures and externalizes an object’s internal state—without violating encapsulation—so that the object can be restored to this state later.

E.g - Saving some object state in user default and retrieving it back.

17. Proxy

Provides a surrogate, or placeholder, for another object in order to control access to that other object. You use this pattern to create a representative, or proxy, object that controls access to another object, which may be remote, expensive to create, or in need of securing.

We usually write wrappers for third party codes, just to provide only few required apis and secure instance of that third party code instance.

18. Receptionist

Addresses the general problem of redirecting an event occurring in one execution context of an application to another execution context for handling. It is also a variant of the Trampoline pattern because it bounces/redirects requests.

E.g - KVO handling at common place and redirecting events or Push notification handling at common place and redirecting to desired actions

19. Template Method

The Template Method design pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. The Template Method pattern lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Previous Post Next Post