Fundamental iOS Design Patterns: SharedInstance (Singleton in Objective C) - DaveOnCode
Fundamental iOS Design Patterns: SharedInstance (Singleton in Objective C) - DaveOnCode
Fundamental iOS
design patterns:
SharedInstance
(Singleton in
Objective C)
8
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.
Me on
stackoverflow:
Me on Github:
angular-ngkit
django-easy-currencies
no-css
Categories
actionscript (13)
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
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!
DAVIDE ZANOTTI