I have a situation where I have several layers of nested objects in a document-based application that need to know information about a certain model.
Specifically, I have an NSTextView, NSLayoutManager, and NSTypesetter, each of which need to know certain properties about my document model in order to correctly render it.
Currently, I have subclassed each of the classes, and given them a property that has a reference to my model. The reference is passed down from my document controller, to the subclassed NSLayoutManager, and then handed off to the subclassed NSTypesetter:
@implementation MyLayoutManager : NSLayoutManager {
@private
MyModel *model;
}
@property (retain) MyModel *model;
-(id)initWithModel:(MyModel *)model;
@end
@implementation MyTypesetter : NSATSTypesetter {
@private
MyModel *model;
}
@property (retain) MyModel *model;
-(id)initWithModel:(MyModel *)model;
@end
The model is passed from Controller->MyLayoutManager->MyTypesetter. This seems a little redundant a perhaps too tightly coupled.
Would the delegate pattern be better? If so, what is the best way to set that up? Typesetter has no delegate, but LayoutManager already has one. Should add a delegate to Typesetter, then override the delegate on LayoutManager? Or is it better to just stick with passing down my model's interface?
Any insight would be appreciated. Thanks.
Generally it's a good idea to isolate communication with the model exclusively to the controller, which then delegates out tasks (providing relevant information as necessary) to other objects involved.
While more OO and correct, it's not always the prettiest. It is generally the easiest to maintain however.
Model ===> Controller ===> View
||
||
\/
Other Objects