iOS templates

Today we will talk about iOS templates.

What are templates? These are reusable solutions to common development problems. They help to write code that can be easily understood and used.

Requirements: XCode 8.2.1, knowledge of Swift 3, time and brain.

We divide all patterns into categories: Creational-, Structural- and Behavioral-patterns.

Singleton (Creational category)

Imagine the situation when you need to have only one copy of the object. For example, it can be a real object: a printer, a server, or something, which should be only one. Apple, for example, has a UserDefaults object, which is accessed through the standard property. Example of implementation:

For the EventManager class is made the private  initializer so that no one can create a copy of the object. The static variable sharedInstance was added and initialized by the EventManager() object. Next, to avoid copying the object, the copy() and mutableCopy() methods are rewritten. In previous versions of swift, you could write dispatch_once{}, but in swift 3.0 this function is no longer available.

Factory Method (Creational category)

This method is used when it is necessary to make a choice between classes that implement a common protocol or share a common base class. The template contains logic that decides which class to choose.

All logic, as the template name suggests, is in a method that encapsulates a solution.

There are two options for implementation – the global method or the use of the base class.

The Global method:

Using a base class implies transferring this logic to the base class.

Abstract Factory (Creational category)

This template is very similar to the one described above, except that it is used to create a group of objects. The template does not know which implementation will be used, but it knows how to select the appropriate specific object.

This method is in the base abstract class. Further in the structure of Car we can use it:

Builder (Creational category)

This template is used to separate the configuration from the creation of the object. The caller contains the configuration data and passes it to the Builder object, which is responsible for creating the object. Suppose that we have a restaurant, and we need to create an application for ordering burgers:

We create default values in the Builder class to avoid asking the visitor about each component he wants in his burger.

If in the future, after conducting a survey, it turns out that clients still want to add a little sauce, then the default value in the Builder class changes to true, and the client survey time remains the same.

MVC (Structural category)

Everything is simple: model works only with data (model); view works with everything related to the drawing of interface elements and animation of various buttons (although it is better to use a separate class for animation: suddenly you want to remake something later); The controller “gathers” all this together.

Over time, of course, the controller will increase from the logic associated with the model, and you will have to switch to a more advanced MVVM pattern (model – view – viewModel).

Facade (Structural category)

This template offers one (simplified) interface for complex systems. Instead of showing the user a whole bunch of methods with different interfaces, we create our class by encapsulating other objects in it and show a more simplified interface for the user.

This is useful when you have to replace, for example, Alamofire with NSURLSession. You make the change only in your Facade class, without touching its interface.

An example of an interface is the SocketManager class:

The end user may not know that SocketRocket is being used. And after some time it is possible to replace it with something else, and it will not be necessary to make changes in all the places where it was used: it will be enough just to fix one class.

Decorator (Structural category)

Decorator adds behavior and responsibilities to an object without modifying its code, for example, when 3d party libraries are used and there is no access to the source code.

In swift, there are two very common implementations of this pattern: extensions and delegation.

Adapter (Structural category)

The adapter allows classes with incompatible interfaces to work together. Apple implements this pattern using protocols. The adapter is used when you need to integrate a component whose code cannot be changed. Perhaps this is an old legacy product (we don’t know how it works and are afraid to touch it).

Bridge (Structural category)

This template is very similar to the Adapter, but with a few differences. We can change the source code (we have access to it). The template separates the abstraction from the implementation, so that they can be modified without corresponding changes in another class. Example:

Now you can change run() methods without making changes to the main file.

Observer (Behavioral category)

One object notifies others about a change in its state. Usually, one object “subscribes” to the changes of the other. Push Notifications is a global example. More local: Notifications, Key-Value Observing (KVO).

Momento (Behavioral category)

Momento saves your objects. This UserDefaults, and Archiving using the NSCoding protocol, and, in principle, the same CoreData.

Command (Behavioral category)

When we associate a method with an action (action touch) for some element of the interface (buttons, for example), this is the Command-template.

Leave a Reply

Your email address will not be published. Required fields are marked *