Oysio
Oysio

Reputation: 3318

Design: Passing on class instances or using singletons?

My app project contains several helper classes that serve all kind of different purposes (eg time/date/calculation, db access, ..). Initiating these classes is quit expensive since they contain some properties that need to be filled up from the database or need to be recalculated each time an new instance is created. To avoid performance problems I tend to initiate each of these classes in the application delegate and pass these on from viewController to viewController.

This has worked for me for some time but I'm finding now that the more complicated the app is getting the more problems I'm bumping into. Mostly problems related to classes getting entangled in a circular reference. I would want to know how I could solve this properly, I alread thought about turning each helper class into a singleton, and use the singleton instead of passing a class instance around. But since some helper classes are depended on each other I'll have singletons that call other singletons, I can't seem to figure out if this would lead to other problems in the end. Anyone any advice on this?

Upvotes: 3

Views: 261

Answers (5)

JeremyP
JeremyP

Reputation: 86661

Why don't you just make your app delegate a factory for the expensive-to-create instances? Each time a view controller needs an instance of the helper class it ask the appDelegate for it.

Upvotes: 1

Alex
Alex

Reputation: 26859

Singletons are basically global variables, and it's a bad idea to create a global variable just to avoid passing things around. Then again, often the right thing to do is simply to pass objects around from one class to another. The trick is figuring out the minimum amount of data you can pass to minimize the coupling. This is where well-designed classes are important. For example, you rarely need to pass an NSManagedObjectContext because you can get it from any NSManagedObject.

Now, let me address the specific case of your expensive-to-create objects. You might try pooling those objects instead of creating them every time one is needed. Database access is a good example of this. Rather than allocating a database connection every time you ask for one, you grab one out of a cache. Of course, when the cache is empty, you need to allocate one. And, for memory reasons, you should be willing and able to empty out the cache when the system asks you to.

That the object is expensive to create shouldn't matter to the user. That's an implementation detail, but it's one that you can design around. You do have to be careful because only objects that don't have mutable state can be handled this way, so you may have to rethink the design of your classes if you want to go this route.

Upvotes: 1

Benoît
Benoît

Reputation: 7427

Personally, I usually using singleton.
In my opinion, it make the code cleaner...

And I am sure that class instance is unique. I use it to have single point access to resource

Edit : Seems I'm wrong !
what about the flexible implementation ?

static Singleton *sharedSingleton = nil;

+ (Singleton*)sharedManager
{
  if (sharedSingleton == nil) {
    sharedSingleton = [[super alloc] init];
  }
  return sharedSingleton;
}

Upvotes: 0

Jeff Sternal
Jeff Sternal

Reputation: 48623

Whenever I'm tempted to use a singleton, I re-read Global Variables are Bad and consider whether the convenience is really worth putting up with this (slightly) paraphrased list of problems:

  • Non-locality of methods and variables
  • No access control or constraint checking
  • Implicit coupling
  • Concurrency issues
  • Namespace pollution
  • Memory allocation issues
  • Unduly Complicated Testing

Upvotes: 1

George Stocker
George Stocker

Reputation: 57907

The problem with singletons is that they make it harder to mock and unit test your application. You should decouple your dependencies; and if you do somehow need a singleton (which should be very, very rare) then consider having the singleton implement an interface that you can mock for testing purposes.

Upvotes: 4

Related Questions