bulat-icon  Articles  
 
 

[+]  Articles

 
     
   

iOS Design Patterns

by Raviteja
 

What is Design Pattern

 
 
  • A design pattern is a recurring solution that solves a specific design problem
  • They are helpful for speeding up problem solving, ensuring that a developer doesn't have to reinvent the wheel for every situation
  • The design pattern is a common way to organize code so that it is reusable and more easily extensible
 

Design Patterns

 
 
  • Singleton
  • Delegate
  • Model View Controller
  • Observer
  • Facade
  • Command
 

1. Singleton

 
Singleton classes are very simple and useful design pattern in iOS
 
 
  • Ensure a single access point
  • Provides a global point
  • The class keeps track of its sole instance and ensures that no other instance can be created. Singleton classes are appropriate for situations where it makes sense for a single object to provide access to a global resource
 
Singleton
 

Singleton - Code

 
 

//SingletonExample.h file

#import <Foundation/Foundation.h>

@interface SingletonExample : NSObject {
}

@property (nonatomic, retain) NSString *myText;

+ (id)Ā  sharedInstance;

@end

 

//SingletonExample.m file
#import "SingletonExample.h"

@implementation SingletonExample

@synthesize myText;

#pragma mark Singleton Methods
+ (id) sharedInstance {
static SingletonExampleĀ  * sharedSingletonInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSingletonInstance =Ā  [[self alloc] init];
});
return sharedSingletonInstance;
}

- (id)init {
if (self = [super init]) {
myText = [[NSString alloc] initWithString:@"Hello World" ];
}
return self;
}
@end

 

2. Delegate

 
 
  • Delegation as a clean and simple way of connecting objects and helping them to communicate with other.
  • Simply say delegate are Protocols, it sends message to Delegate object when event handled.
 
Delegate
 

One example of this is UIScrollView

 
The UIScrollView class has no knowledge of what it should be scrolling as that is an application specific task. So to notify the application of scrolling events, UIScrollView uses the UIScrollViewDelegate
The application can implement the delegate and then intercept the scrolling events sent to it by the UIScrollView
 

Delegate -Code

 
 

UIScrollViewDelegate
- scrollViewDidScroll:
- scrollViewWillBeginDragging:
- scrollViewWillBeginDecelerating:
- scrollViewDidEndDecelerating:
- scrollViewDidZoom:

 

3. Model View Controller

 
 
  • All iOS apps use Model View Controllers (MVCs)
  • MVCs are the link between an app's data and its UI
  • The MVC is broken up as such:
    • Model - Underlying data
    • View - The view the user sees, the UI
    • Controller - Determines the interactions between the model and views
  • This keeps the program modularized, allowing for high cohesion and loose coupling
 
Model View  Controller
 

4. Observer

 
 
  • The observer pattern is used when one object wants to know when another object changes
  • This pattern is build into every NSObject vi Key-Value Observing
  • This is also often used with MVCs because when a model changes you often will want to update the views
 
Observer
 
 
  • The observer pattern is similar to the delegate pattern, however one key difference is that observable objects support multiple observers, while a delegate is just one object
  • However, with this expanded possibility comes one big pitfall: you must remember to remove an object as an observer when that object is deallocated, otherwise there will be a code leak
  • The following slide contains a code sample of what the Observable class looks like
 

Observer - Code

 
 

@interface Observable: NSObject

- (void)addObserver:(id<NSObject>)observer;
- (void)removeObserver:(id<NSObject>)observer;
- (void)notifyObservers:(NSInvocation*)invocation;

@end

 

5. Facade

 
 
  • The façade pattern is used for simplifying a complex system
  • It allows for a subsystem to be accessed through one entry point, allowing the systems using it to be unaware of the classes in the subsystem
  • This is also useful if the classes under the façade are likely to change, as we can still have the façade class have the same API
  • One example of this in iOS is the NSImage class
  • This class is a façade which provides an interface for using and loading images that can be vector-based or bitmap-based
  • So no matter what type of image the application is using, it can use NSImage and have no knowledge of what's happening underneath the class
 
Facade
 

6. Command

 
 
  • The command pattern is used for request encapsulation
  • It allows for the separation of an object sending a message from the objects receiving the message
  • The encapsulated request/message is then much more flexible and can be passed between objects, stored for later, dynamically modified, or placed on a queue
  • In iOS an example class that is used to encapsulate messages is NSInvocation
  • These objects are used for undo management
  • They contain a target, selector, arguments, and the return value
  • These elements can be set directly and the return value is set automatically when the object is dispatched
 
Command
 
 
top-icon