Using Design Patterns
A design pattern solves a common software engineering problem. Patterns are abstract designs, not code. You use them to help you define the structure of your data model and its interaction with the rest of your app. When you adopt a design, you adapt its general pattern to your specific needs. No matter what type of app you’re creating, it’s good to know the fundamental design patterns used in the frameworks. Understanding design patterns helps you use frameworks more effectively and allows you to write apps that are more reusable, more extensible, and easier to change.
Model-View-Controller (MVC) is central to a good design for any iOS app. MVC assigns the objects in an app to one of three roles: model, view, or controller. In this pattern, models keep track of your app’s data, views display your user interface and make up the content of an app, and controllers manage your views. By responding to user actions and populating views with content from the data model, controllers serve as a gateway for communication between the model and views.
As you’ve built your ToDoList app, you’ve followed an MVC-centric design. The interface you built in storyboards makes up the view layer.
ToDoListTableViewController are the controllers that manage your views. In Tutorial: Add Data, you’ll be incorporating a data model to work with the views and controllers in your app. When you begin designing your own app, it’s important to keep MVC at the center of your design.
Target-action is a conceptually simple design in which one object sends a message to another object when a specific event occurs. The action message is a selector defined in source code, and the target—the object that receives the message—is an object capable of performing the action, typically a view controller. The object that sends the action message is usually a control—such as a button, slider, or switch—that can trigger an event in response to user interaction such as tap, drag, or value change.
For example, imagine that you want to restore default settings in your app whenever a user taps the Restore Defaults button (which you create in your user interface). First, you implement an action,
restoreDefaults:, to perform the logic to restore default settings. Next, you register the button’s Touch Up Inside event to send the
restoreDefaults: action message to the view controller that implements that method.
You’ve already used target-action in your ToDoList app. When a user taps the Save button in the
AddToDoItemViewController, it triggers the
unwindToList: action. In this case, the Save button is the object sending the message, the target object is the
ToDoListTableViewController, the action message is
unwindToList:, and the event that triggers the action message to be sent is a user tapping the Save button. Target-action is a powerful mechanism for defining interaction and sending information between different parts of your app.
Delegation is a simple and powerful pattern in which one object in an app acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time, the delegating object sends a message to the delegate. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance (or state) of itself or of other objects in the app, and in some cases it will return a value that affects how an impending event is handled.
The delegate pattern is prevalent in existing framework classes, but you can also implement delegation between two custom objects in an app. A common design uses delegation to allow a child view controller to communicate a value (typically a user-entered value) to its parent view controller.
You haven’t worked with delegation yet, but in Tutorial: Add Data, you’ll see an example of it when you add additional behavior to your
These are a few of the most common design patterns that you’ll encounter during iOS development, but there are many more. As you learn more about Objective-C, you’ll spot other design patterns that you can apply in your app.