Every discipline of engineering uses some variation of process flow diagramming during the design phase of a project. This approach helps identify potential problems early enough in the project to allow engineers an opportunity to make changes before construction begins. Software engineering shares the same best practices as its engineering cousins.
There are many advantages to using a design pattern to document the creational, structural, and behavioral characteristics of an iOS app. Separating the user interface from the logic, for example, offers these three distinct advantages:
- Interfaces are better defined and more specific to a device;
- Apps become more adaptable to changing requirements; and
- Objects are reusable.
Separating the business layer from the presentation layer allows us to alter an iOS app with more efficiency. It is sometimes necessary to make adjustments or changes to an app to remain compatible with new releases of iOS software and new devices. It is much easier to create an iPad app from an existing iPhone project if the project was developed using design patterns. If the presentation layer were independent of the program’s main features and functions, a developer could simply adjust the user interface components while reusing the existing business logic (Figure A).
There are many design patterns available for use by software developers. Following the separation of concerns (SoC) design principle is crucial when developing software designed to scale. A few of the mainstream object-oriented design patterns follow the SoC design principle by separating features from behavior.
Here is a short list of design patterns used by software engineers:
- Abstract Factory Pattern
- Adaptor Pattern
- Object Modeling Pattern
- Chain of Responsibility Pattern
- Command Pattern
- Composite Pattern
- Decorator Pattern
- Façade Pattern
- Iterator Pattern
- Mediator Pattern
- Memento Pattern
- Model-View-Controller Pattern
- Observer Pattern
- Proxy Pattern
- Receptionist Pattern
- Singleton Pattern
- Template Method Pattern
Design patterns are separated into three categories: creational, structural, and behavioral.
- Creational patterns are specifically used for dealing with the creation of objects. The abstract factory and singleton are considered creational patterns.
- Structural patterns are used to define structures of objects. The adaptor, composite, decorator, and proxy patterns are types of structural design patterns.
- Behavioral patterns identify communication between objects. The chain of responsibility, command, iterator, memento, observer, and template method are examples of behavioral design patterns.
Which design pattern is best for iOS development?
In iOS programming, leveraging a design pattern could be the difference between speed-to-market and failure-to-launch. While it takes quite a bit longer to organize requirements and distill the information into the blueprints of your next great iOS app, the amount of time writing code can be greatly reduced.
The Model-View-Controller (MVC) design pattern is considered the cornerstone design pattern for iOS app development. The MVC design pattern is not unique to iOS development; in fact, MVC was conceptualized (like a few other technologies we use today) by Xerox Parc in the 1970s.
The MVC design pattern consists of three parts: model, view, and controller. The model contains data, information, logic, or objects considered part of the business layer of an iOS app. The view contains all of the user information components, such as text areas, buttons, and sliders, considered the presentation layer of an iOS app. The controller is the liaison, or communication layer, of an iOS app. A model will never communicate directly with a view and vice-versa. The controller joins the model to the view by handling all communication.
User actions happen on a view, such as a button click. The view sends the message to a controller. If necessary, the controller communicates with the model. The controller passes information back to the view. Using a hypothetical calculator app as an example, the MVC design pattern would be applied like this:
- Model – calculation functions (add, subtract, multiply, divide).
- View – user interface components (operand & operator buttons, calculation text).
- Controller – objects that listen and communication information to and from the model and view. When the equal button is selected, for example, the controller makes a request to the model to perform the math. The model returns the answer to the controller, which promptly communicates the results to the view. The view is responsible to displaying the answer. (Figure B)
The iOS App Programming Guide and Apple’s Start Developing iOS Apps Today tutorial recommend that every developer learn the MVC, the Delegation, and the Target-action design patterns. The Delegation pattern is based on the concept of one object acting on behalf of another, while Target-Action is a design pattern where an object stores information that will be sent to a specific object when a certain event happens. These three design patterns are considered fundamental building blocks for iOS app development.
Note: This article originally published in November 2012 in our iOS App Builder blog.