Design: passing instances of a class or using singlets?

My application project contains several helper classes that serve all kinds (e.g. time / date / calculation, access to db, ..). The initiation of these classes is terminated because they contain some properties that must be populated from the database or must be recounted each time a new instance is created. To avoid performance problems I tend to initiate each of these classes in the application deletion and pass them from viewController to viewController.

This has worked for me for a while, but now I find that the more complex the application gets the more problems that I encounter. Mainly problems related to the confusion of classes in a circular reference. I would like to know how I can solve this correctly, I thought about turning each auxiliary class into a singleton, and instead use a singleton instead of passing an instance of the class. But since some helper classes are dependent on each other, I will have singlets called other singletones, I can't seem to figure out if this will lead to other problems at the end. Any advice on this?

+3
source share
5 answers

, unit test . ; - ( , ), , , .

+4

, , " " , , () :

  • Concurrency
+1

- , , . , - . , , , . . , NSManagedObjectContext, NSManagedObject.

. , , . . , , , . , , . , , , .

, . , , . , , , , , .

+1

factory ? , -, appDelegate.

+1

singleton.
-, ...

And I'm sure the class instance is unique. I use it for single point access to a resource

Edit: It seems I'm wrong!
what about flexible implementation?

static Singleton *sharedSingleton = nil;

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

Source: https://habr.com/ru/post/1773039/


All Articles