MVC and Singleton patterns with iOS

Hello iOS lovers… 🙂 Today I’m going to write a short and simple blog post related to iOS development. As the title indicate, today we will talk little bit about two design patterns call MVC and Singleton, but in more practical manner. 

I don’t want to disappoint my readers. Hence I will tell you clearly what I am going to do and why I am doing it. I normally don’t write theory, because you can find pretty good documents on those. What I am going to do is, telling you two practical situations where I used above mentioned design patterns when developing iOS applications. If you can remember, I mentioned about MVC pattern in my previous post but I haven’t explained it. Let me explain it to you now.

MVC stands for ModelViewController. With the simplest iOS application that you are creating in Xcode, you basically apply MVC pattern. Surprised… ? Don’t be, walk through with me to clarify your doubts.

Having said that I’m not going to explain theory, now going to describe Model, View and Controller using my words. But first, let me show you how I categorised my previous post related project files according to MVC.

Figure One

Figure 1

Model is your application data and it tells what is your application all about. As you can see in the Figure 1, my all file operations, which means check file availability, create, read and delete file functions written inside Model. Model cannot communicate with View directly and also the vice versa. The communication between Model and Controller happens in two ways;

  1. Notifications
  2. Key-Value Observing

View is the interface that end user use. With the storyboard concept it is easy to identify your view components. But, don’t think that storyboard is the only component in View. All the UIViews and it’s subclasses are included to it. If I give you a practical example, assume you wanted to create a custom table cell class. There you have to create a class which is a subclass of UITableViewCell. Then that whole three files, implementation, interface and XIB file act as View components. As I mentioned you earlier, View cannot communicate with Model but can interact with Controller in following ways.

  1. IBAction
  2. Delegate
  3. Data Source

Controller is the mediator that access the data from Model and passes to the View. You can easily identify those controller classes from Figure 1 because file names ended with Controller.

I think that is enough with MVC. Let’s discuss about Singleton design pattern. Speaking about MVC is reasonable because in my earlier post I have mentioned it. But why Singleton? :-/ That is because, I’m going to use it in one of my upcoming post related to SQLite database connection with iOS.

Simply, Singleton is, having just one instance of a particular class for the whole application process. We create a new instance if and only if the existing instance is set to nil. The other major attribute of the Singleton class is, it can be globally accessible.

Okay… before I show you one of my own code, you can study this pattern with inbuilt Apple classes such as [NSUserDefaults standardUserDefaults][NSFileManager defaultManager]. Yes, absolutely… you have used Singleton pattern without knowing already. 🙂

This is the header file of the DBManager class.

@interface DBManager : NSObject

+ (DBManager *)getSharedInstance;


As you can see, we have create a class method to globally access the DBManager class and it returns a DBManager instance. Following is the related implementation file that create the Singleton object.

#import "DBManager.h"

static DBManager *sharedInstance = nil;

@implementation DBManager

+ (DBManager *)getSharedInstance {
 if (!sharedInstance) {
 sharedInstance = [[super alloc] init];
 [sharedInstance createDB];
 return sharedInstance;


As you can see with the above code snippet, we create a new database only if there is no instance exists, otherwise we return the existing instance. Okay, that all about the code sample, but before we end  the discussion, just think practically how valuable this Singleton pattern is. What happens, if you able to delete a particular data entry from the database and retrieve the same entry from the database at the same time? 😮

With that question, I’m going to end my todays’ blog post. It has extended than I thought. 🙂 See you with a new post and ready for a SQLite series very soon. Take care…!!!

About AnujAroshA

Working as an Associate Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in iOS and tagged , , , . Bookmark the permalink.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s