Chapter 21: Some Things We Ought to Do

Over the weekend , I did a little studying . I was reading Charles Petzold s Programming Microsoft Windows with C# (Microsoft Press, 2001) and James W. Cooper s C# Design Patterns: A Tutorial (Addison-Wesley, 2002). They re both wonderful resources, and I got a lot of ideas. Then today, I took a break from most immediate things and spent a little time at lunch writing ideas on cards about how the XML Notepad should be improved. Here are a few of those ideas, with a little explanation of each one, and some technical discussion.

Command Pattern

The Command pattern is one where we represent a request or command as an object. It offers the advantage that the Command object carries all the information about what has to be done, and the main line of the code can handle all the commands in a uniform way. (This pattern, and all those mentioned in this chapter, can be found in the excellent book Design Patterns, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides [Addison-Wesley, 1995], or in the aforementioned Cooper book.)

The Command pattern is a natural for the XML Notepad. I spent some time thinking about building a subclass of MenuItem that would allow attachment of a Command object, which would then be used to dispatch the action to the TextModel. It should be possible to have all the menu items go to one method, which would init the TextModel, execute the command, and read the TextModel back into the TextBox. This would eliminate the duplication of the init and de-init that we re seeing now in code like this:

 void MenuInsertSection(object obj, EventArgs ea) { 
model.SelectionStart = textbox.SelectionStart;
PutText(textbox, model.LinesArray(), model.SelectionStart);

Each of the menu items that we have now, and all the future menu items that update the model, have those lines passing stuff back and forth between the model and the TextBox. Use of the Command pattern looks to me like a good way to do this.

Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: