I am creating a text editor. My text-box control is a subclass of TextBox called Editor. My main form, MainForm, has an instance of Editor.
When MainForm wants to, say, load a text document, it calls Editor.LoadDocument(string path). Editor.LoadDocument calls Document.Load(string path).
The same kind of thing happens with save: MainForm --> Editor.SaveDocument --> Document.Save.
It seems that Editor is acting as a middleman unnecessarily here, so I’m thinking of just letting MainForm access Document directly: editor.Document.Load(path). Editor would still create and maintain Document; it would just provide direct access to it.
Note that this would create a bidirectional association: Editor would have a Document and Document would have an Editor (Document uses Editor.Text and subscribes to Editor.TextChanged).
I have two questions:
-
Is this good design?
-
Do bidirectional associations create any slowdown pertaining to garbage collection when the app exits?
You need to read about patterns such as Model-View-Controller.
Generally, the UI should activate operations directly on the model (
Documentin your case), and then the view (Editor) should be subscribed to events and act as an observer.