Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I find singletons a bad idea and this looks about four times as horrible. The code is quite complex, you can be sure of spending a nice few hours chasing subtle bugs in it and you will probably never feel comfortable about it. That’s no good. You should throw this abomination away and wire your objects together in some other way that doesn’t require so much thinking.</p> <p>If you like patterns, you can use something akin to Factory pattern to wire your objects. The Factory will take care of creating those two instances and passing them wherever needed. And the Factory will be a lot more simple than Multiton:</p> <pre><code>@interface Factory : NSObject { Foo *foo1, *foo2; } @end @implementation Factory - (id) init { [super init]; foo1 = [[Foo alloc] init]; foo2 = [[Foo alloc] init]; return self; } </code></pre> <p>Of course you don’t have to create both instances at once. You can do anything you like there – cache, lazy load, anything. The point is leaving the <code>Foo</code> lifetime management up to the Factory, separate from the <code>Foo</code> code. Then it gets much easier. ¶ All the other objects that need <code>Foo</code> will be created and wired through Factory and will receive their <code>Foo</code> through a setter:</p> <pre><code>@implementation Factory - (id) wireSomeClass { id instance = [[SomeClass alloc] init]; [instance setFoo:foo1]; [instance setAnotherDependency:bar]; return [instance autorelease]; } </code></pre> <p>This is all much more straightforward then the code from your question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload