Design Patterns in iOS Development

By Peng Xie

“Object-oriented” is a popular word in today’s software industry. The goal of object-oriented design is to find flexible and reusable solutions to common problems. Design patterns are powerful tools for developers to achieve that goal. So, what are design patterns and how are they useful?

Brief History of Design Patterns

According to the book “Design Patterns, Elements of Reusable Object-Oriented Software,” design patterns are “simple and elegant solutions to specific problems in object-oriented software design.” Originated as an architectural concept by Christopher Alexander, design patterns are used by architects to document universal solutions for design problems. Computer programmers and engineers started to applying patterns to software designs in 1980s. Today, software engineers use design patterns to design software solutions that are easier to maintain in the future and have more reusability and flexibility.

Why are Design Patterns Important?

Since design patterns are developed and evolved over time, their behaviors are more predictable. Hence, design patterns can greatly help to prevent subtle issues that may cause problems in development. For software architects and engineers, design patterns provide excellent capabilities to maintain and expand the functionalities of the software. For example, by using some design patterns, a software engineer can add new functions to a software module without even accessing the module’s source code. We will discuss more about different kinds of design patterns in later sections.

Common Design Patterns in iOS App Development

Model View Controller (MVC) Pattern

When talking about design patterns in iOS App development, nobody can ignore the MVC pattern. MVC is one of the most used design patterns in iOS App development. It has three parts: Model, View, and Controller. The Model is the object that provide storage structures and defines manipulating interfaces for application’s data. The View is used to present the Model for users and provides user interaction functionalities. Finally, the Controller manages the  communication between the Model and View.

An example for MVC pattern in iOS App development is the usage of view controllers, views and data objects. Tables are commonly used in iOS apps to present data to users. To design a table, a developer needs to create an UITableViewController, an UITableView and if necessary, an object to hold the data for the table. In this case, the UITableViewController is the Controller that handles the communication between the data object (Model) and UITableView (View).

MVC

MVC

By using the MVC pattern, the developer can easily change the type of data in the table or the style of the table without changing other part of the application.

The Decorator Pattern

The decorator pattern is another commonly used pattern in iOS App development. Instead of subclassing an existing class to modify its behavior, a developer can user decorator patterns to add new behaviors and functionalities to the class dynamically without even touch the its code. One implementation of decorator pattern in Objective-C is called “Category.”

Category allows the developer to add new methods (behaviors and functionalities) to existing class without modifying old codes or subclassing. The compiler will add the methods defined in categories to the corresponding classes at runtime.

The Observer Pattern

When one part of the application needs to respond to the changes of another part of the application, the observer pattern starts to show its importance. By setting an object as the observer of another object, it can be notified once the observed object makes any changes. Notifications is one kind of observer pattern implementation in iOS App development. These kinds of notifications are not the same kind as push or local notifications. It is used by different parts of the application to notify the interested objects when something interesting happens. Let’s use an example to demonstrate the notifications in iOS App development.

A Warehouse and a SalesPerson class don’t have to know each other, but the Warehouse class can register itself as the observer of a SalesPerson class. When a SalesPerson class gets an order from a customer, it will send out a notification to the Warehouse class so that the Warehouse class can ship the order to the customer.

Conclusion

Design patterns are powerful tools for developers. They help to reduce the difficulties of maintenance and increase the flexibility and reusability of application modules. The design patterns mentioned in this article are only a small part of the patterns provided in Objective-C.

As iOS developers, the more familiar we are with the design patterns, the better we can design the application. Utilizing design patterns reduces unexpected behaviors of applications so we can provide better user experience. In addition, giving the design patterns’ abilities of expanding and adapting, we can also easily offer users more new functions without making many changes to existing applications. Design patterns will certainly make our lives as both developers and users much easier.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s