You are on page 1of 4

DaveOnCode

Fundamental iOS
design patterns:
SharedInstance
(Singleton in
Objective C)
8

code, patterns, programming languages and experiments

Would you like to read


more about Python,
Django, JavaScript and
CSS?
Take a look at my
survey!

Follow @daveoncode

Brief introduction
Singleton is one of the (if not THE) most commonly
used/abused design pattern adopted in software
development.
Basically its used to restrict the instantiation of a class to
one object only that can be used safely across application
architecture. Its implementation is pretty simple compared
to other patterns and its very similar for the most of
programming languages, but in Objective C it has a slightly
different approach due to the lack of normal constructor
that are instead replaced by alloc/init paradigm.

How it works in Objective C

Me on
stackoverflow:

Me on Github:
angular-ngkit
django-easy-currencies
no-css

Categories
actionscript (13)

In Objective C, we can speak more appropriately about the


SharedInstance pattern rather than traditional Singleton.
This one is often used in Apple frameworks and differently
from its twin brother, it does not explicitly forbid the
instantiation of multiple class instances, but it simply
provide a class method that will return a shared instance.
During the first call to that method the instance will be
allocated and the next calls will return the previously object
created. So this is a sort of hybrid approach.
A common class that use this pattern is UIApplication
which implements a sharedApplication method (which
returns the current running application). Differently from
Singleton which use getInstance(), in Objective C the
corresponding method has not a fixed name but it should
start with shared followed by the type of class (so for
UIApplication is sharedApplication for
MYAmazingManager will be sharedManager). Anyway
there are several exceptions, for example NSFileManager
has a defaultManager acting as a sharedInstance
accessor that could be renamed sharedManager.

Pattern implementation
In my singleton implementations I simply call the accessor
sharedInstance, in this way I can use a simple precompiler macro that automatically synthesize this methods
for me.
Even implementation of this pattern may vary and
personally I adopted one which is thread-safe and rely on
CGD (Grand Central Dispatch), which is an affective C level
API provided by Apple in order to handle concurrency.

air (4)
ajax (1)
apache (1)
aws (1)
books (2)
browsers (4)
css (4)
django (7)
flex (14)
google closure (6)
ide (5)
javascript (14)
jxt (1)
mac (2)
mobile (3)
Objective-c and Cocoa
(13)
osx (1)
phonegap (1)
php (1)
python (7)
Uncategorized (2)
wordpress (1)
xcode (3)

Recent comments
Davide Zanotti on
Generate random dates
in python using
datetime and random

The implementation of my sharedInstance method is the


following (comments explain how it works):

modules
Jason Green on
Generate random dates

+ (id)sharedInstance

in python using

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

{
datetime and random
// structure used to test whether the block has completed
modules
static dispatch_once_t p = 0;

BalRanj on Eclipse:
// initialize sharedObject as nil (first call only)
convert upper case text
__strong static id _sharedObject = nil;

to lower case and
// executes a block object once and only once for the lif
viceversa with a simple
dispatch_once(&p, ^{
_sharedObject = [[self alloc] init]; shortcut
});
Ryan S on Adapting

iframe height to its
// returns the same object each time
return _sharedObject;
content with 2 lines of
}
javascript

Radek on Creating a

Pattern in action!

custom AMI with Postgis

SharedInstance pattern is IMO the best way to share data


across multiple viewControllers in iOS applications.
One practical example is a data manager to share Core Data
stuff such managed object context. I realized my own
manager so that I can access this objects anywhere with
ease like:
1

and its dependencies in


order to deploy Django
+ GeoDjango on
Amazon Elastic
Beanstalk

[[MYDataManager sharedInstance] managedObjectContext];


8

DECEMBER 19, 2011

DAVIDE ZANOTTI

DESIGN PATTERNS, IOS, OBJECTIVE-C

PROUDLY POWERED BY WORDPRESS | THEME: SORBET BY WORDPRESS.COM.

You might also like