Design Patterns: IOS app development

Chandini Baratam
Sep 4, 2018 · 3 min read

In mobile application development, there is a lot of work which is delegated from one part of the app to the other. For suppose, when a button is tapped on the UI, another view is triggered. All this happens through delegation. IOS provides us various design patterns to deal with activation of events. The types of design patterns provided are Key Value Observation, Notification Centre, Delegation.

Key Value Observation (KVO)

Lets take an example of a banking app, which shows the current balance in green if the balance is positive and red, if its negative. To do this, the view controller needs to be notified when the value of the balance gets changed during deposits and withdrawals. Using KVO, we can register to be notified of changes to specific properties of other objects.

Things to care of:

If object A wants to be notified of changes to some property of object B.

1. That specific property in B, should use a setter to change its value.

2. Object A has to register as an observer of Object B for the specific key path (property).

3. Class A has to implement the method ObserveValueforKeypath. This method has to specify what to do with the changed value.

For the case of the banking app, the Account class should have a setter method for property: current balance. The view controller has to register as an observer of account object for property: current balance. And finally the view controller class has to implement the method ObserveValueforKeypath as below.

Key Value Observation

Notification Center:

Notification center is feature provided by IOS, using which information can be broadcast-ed to the registered objects. NSNotificationCentre is a singleton class through which different objects can add themselves as observers and be notified of the desired notifications.

Notification handling

In the above example, the object added itself as an observer to the NSNotificationCenter for the notification type:UIDeviceBatteryLevelDidChangeNotification. When a UIDeviceBatteryLevelDidChangeNotification is received, the function batteryLevelChanged is triggered.

Delegation:

Delegates help us in sending messages from one object to another. Lets take a simple application which has two views. When view1 is tapped, view2 appears with its background color changed. For this to occur, there should be some kind of communication between them, and that’s where delegation comes into the picture.

Delegation

When view1 is tapped, it is delegating the work of changeBackgroundColor to its registered observers, which in the above code is object view2. As class View2 is adopting the ClassView1Delegate protocol, it should implement changeBackgroundColor function to conform the protocol.

More or less, all the above three design patterns does the similar job. But depending on the needs of the application a suitable pattern should be chosen.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade