The Mediator pattern defines a separate object that encapsulates how two or more objects interact. This can break the coupling between them, making all the objects involved simpler and more clear.
Right now, the Form understands the mapping between the keyboard/ menu operations and the specific operations of the TextModel. For example, we have this code:
if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.None) {
model.Enter();
kea.Handled = true;
}
else if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.Shift) {
model.InsertReturn();
kea.Handled = true;
}
In the second of these cases, the Form knows that Shift+Enter means Insert Return. In the first of the cases, the Model is told Enter(), and it contains the code to decide what to do:
public void Enter() {
InsertParagraphTag();
}
I ve been concerned about this for a while. There has been discussion of which way is right, and we have an interaction with the Customer Acceptance Tests as well. We ve talked about that before. The Mediator pattern would allow me to break out the translation from form operations like keystrokes and menu actions into a separate class to encapsulate the idea. This could be really good.