Sunday, 29 September 2013

Is it good programming pracitce to assign Singleton to variables across multiple classes

Is it good programming pracitce to assign Singleton to variables across
multiple classes

In my app I'm using singleton class (as sharedInstance). Of course I need
to use data that is stored in that singleton in multiple classes (view
controllers). Because writing
[[[SingletonClass sharedInstance] arrayWithData] count] or
[[[SingletonClass sharedIntanse] arrayWithData] objectAtIndex:index] or
some other methods that you use on array is not comfortable I thought to,
in the begining of lifecycle of non-singleton class, assign property
(strong, nonatomic) of that non-singleton class to have the same address
as SingletonClass.
self.arrayPropertyOfOtherClassOne = [[SingletonClass sharedInstance]
arrayWithData] and in some other class
self.arrayPropertyOfOtherClassTwo = [[SingletonClass sharedInstance]
arrayWithData]
Is it good programming practice?
In my opinion there is nothing bad with it. Properties will point to the
same address as property in Singleton and after non-singleton class will
be destroyed also properties that where pointing to singleton so Reference
Count = Refrence count - 1.
Am I correct?

No comments:

Post a Comment