Re: class design issue
Re: class design issue
- Subject: Re: class design issue
- From: Drew McCormack <email@hidden>
- Date: Wed, 30 Jan 2002 18:39:09 +0100
When the user double-clicks in the tableview, the delegate in
MainController gets the Book instance at the clicked row and calls
[book show]. I didn't want MainController to know anything about the
actual nib and window controller called BookController. So [book show]
instantiates a single BookController calling initWithBook:self, which
means now the controller has a pointer back to the Book, which seems
ok. But of course the book now has a pointer to the BookController...
circular references always make me nervous.
This makes me nervous too. Is it possible you are mixing up your MVC
classes a bit? A Book seems to me as though it should be a model class,
holding some data, but should not have a 'show' method.
I don't like the idea of a Book instantiating a BookController at all.
One idea is that the main controller does instantiate a BookController,
then passes Book's to it. (I don't really understand why you don't want
MainController not to know about BookController.)
If Book does have a model and a view role, you might consider splitting
it into two separate classes.
The real problem occurs for me when the BookController needs to call
some method on the MainController instance. BookController has no
pointer to the MainController, and neither does the Book instance it
references. In other similar cases, I solved the problem by making the
class in question a singleton with a class method that instantiated the
static instance and returned it so i could call methods on it.
However, in this case, the class was loaded from MainMenu.nib and I
can't get a global handle on it.
I would just instantiate BookController from the MainController, and
pass it a reference to the MainController.
Do I pass the reference to MainController down to Book, then down to
BookController? That sounds awful. There would be pointers all over
the place... I can't use notifications because I need the return
value. I need access to the mainController because it is tracking a
global (across all books) index of some sub-element of a book, and I
need to check for the existence of that from within the BookController.
You can't use Notifications, but what about a delegate? You could make
mainController the delegate of a BookController.
Drew McCormack