class design issue
class design issue
- Subject: class design issue
- From: email@hidden
- Date: Tue, 29 Jan 2002 20:47:17 -0800
I feel dumb asking this, but here I go...
I'm writing a fairly complex application, currently around 10k lines of
code, around 40 classes, 13 nib files.
I'm trying to keep things pretty modular, keep the classes encapsulated
and not have much knowledge about each other.
I have a class called MainController that is instantiated out of
MainMenu.nib. It is the controller, datasource and delegate for the
main window, which just has a tableview in it.
Each row of the table represents another window that could be opened,
but since I don't want to actually load the nib for each row until it's
clicked on, I have another class called Book that describes the contents
of each subwindow, but has no window controller code in it at all.
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.
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.
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.
How do I refactor this to make more sense?
MainMenu.nib
MainController instance
a window with a tableview with the above instance as its datasource
and delegate.
MainController.m
instantiates an array of Book
tells Book to "show" itself.
Book.m
has a show method that instantiates a singleton BookController for
itself
and tells it to "show" which calls [self window]
makeKeyAndOrderFront:self
therefore, it needs a pointer to the BookController that
corresponds to it.
has fields that will be shown in a tableview by BookController
has no reference back to MainController
Book.nib
has BookController as File's Owner
has a window tableview that has File's Owner as the datasource
BookController.m (NSWindowController subclass)
needs to know about the Book that instantiated it so that it can
render its tableview and make changes to the Book object.
needs a reference to MainController to adjust some global indices,
but it feels gross to pass that address around so much.
Thank you, anyone who hung in long enough to read this far...
I hope I get a reply, because it feels like there must be some
OO/Cocoa way to do this better than my current design.
-tw