In my app I use a class (which is a singleton for now), called ClassA for the example, for managing stuff.
Because it’s a singleton, I used this behavior to get this unique instance in many classes that needs it.
But now, i can’t keep this class as a singleton anymore, I need to have one instance by NSDocument.
So I created an instance of the ClassA on my NSDocument subclass but the problem is for passing this object on classes that needs it.
I have this kind of structure :
Class1 <- Class2 <- Class3 <- Document -> ClassA
I need to have access of the instance of ClassA of Document in Class1.
I can use Dependency Injection to pass ClassA in Class1 by init or setter methods but because Document does not have direct access to Class1, I need to pass it to Class3 then Class2 then Class1.
Is this the only way to do that ? Or are there another better ways to handle it?
I would say go back to your singleton principle but use the
NSDocumentas the key into a dictionary ofClassAobjects; something like:This then only requires that
Class{1,2,3}know the document they belong to.