iOS design patterns

Although most developers probably agree that design patterns are very significant, there are not many articles on the subject, and while writing code, we developers sometimes do not pay too much attention to design patterns.

In software design, design patterns are reusable solutions to common problems. They’re templates that are designed to help you write easy to understand and reuse code. They also help you create loosely coupled code so that, without too much hassle, you can change or replace components in your code.

Types of design patterns in iOS:

Singleton:

The Singleton design pattern ensures that a class has only one example and provides a global access point to it. The class keeps track of its only example and ensures that it is not possible to create any other instance. For situations where it makes sense for a single object to provide access to a global resource, Singleton classes are appropriate. Usually, when the first time is needed, it uses lazy loading to create a single instance.

Facade:

A single interface to a complex subsystem is provided by the Facade design pattern. You only expose one simple unified API, instead of exposing the user to a set of classes and their APIs. When working with a large number of classes, this pattern is ideal, especially when they are complex to use or difficult to understand.

Decorator:

Without modifying its code, the Decorator pattern dynamically adds behaviors and duties to an object. If you modify the behavior of a class by wrapping it with another object, it is an alternative to subclassing.

Memento:

Save your stuff somewhere in the Memento Pattern. Later on, without violating encapsulation, this externalized state can be restored; that is, private data stays private. Archiving, serialization, and state restoration are examples of implementations of the Memento pattern.

Adapter:

The design pattern of the Adapter converts a class’s interface into another interface that clients expect. The adapter allows classes that couldn’t otherwise work together because of incompatible interfaces. It decouples the client from the targeted object class. To do the job, Apple utilizes protocols. Such protocols as UITableViewDelegate, UIScrollViewDelegate, NSCoding, and NSCopying may be familiar to you. As an example, any class can provide a standard copy method with the NSCopying protocol.

Observer:

A one-to-many dependency between objects is defined by the Observer design pattern so that all its dependents are automatically notified and updated when one object changes state. In essence, the Observer pattern is a publish-and-subscribe model in which the subject and its observers are loosely linked together. Communication between the objects being observed and observed can take place without either needing to know much about the other. The observer model is implemented in two ways by Cocoa: Notifications and Key-Value Observing (KVO).