C# Coding Solutions—Editing Text Using the Command Pattern
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
Editing Text Using the Command Pattern
To illustrate the minimalism of the ICommand interface, consider the following implementation of a Command pattern that saves a file to the disk:
public class FileSaveImplementation : ICommand { private string _filename; private string _buffer; public void Execute() { StreamWriter writer = new StreamWriter( _filename); writer.Write( _buffer); writer.Close(); } }
The class FileSaveImplementation implements the method ICommand.Execute to save the string contents _buffer to the file with the identifier _filename. The method implementation of execute is an example action and says that one of the application actions is to save the text at a specific location. Notice that the ICommand interface is completely implemented, but the class is not completely implemented. Not shown is how the data referenced by _buffer, and _filename is created.
The FileSaveImplementation in conjunction with other ICommand interface instances could be used in the following application context:
- The application is started and a class that manages the
ICommandinterface instances is emptied and reset to contain no actions. - The user enables a log that enables the application’s ability to rollback commands.
- The user performs some text operations that involve adding, removing, and formatting text.
- Each text operation is saved as an
ICommandinterface instance. - The user decides to save the document, thus creating an instance of
FileSaveImplementationthat contains the state of the document. The state of the document is the result of applying all of the previous text operations. - The user decides to exit the application.
- Another user loads the document and looks at the log information generated by the
ICommandinterface instances. - The user decides that some text edits executed before saving the file need to be deleted; this creates a chain effect of updating every
ICommandinterface instance stored after the affectedICommandinterface instances. - The other user saves the document again.
- The users exit the application.
- The application is started and a class that manages the
From a high level that does not include the nice GUI code, steps 1 through 5 could be implemented as follows:
CommandImplementationsManager cmdMgr = NewDocument(); // Other code cmdMgr.Add(new TextAddImplementation().Execute()); // Other code cmdMgr.Add(new TextDeleteImplementation().Execute()); // Other code cmdMgr.Add(new TextAddImplementation().Execute()); // Other code cmdMgr.Add(new FileSaveImplementation().Execute());
The class CommandImplementationsManager manages all ICommand interface instances that are created during the application’s execution. Calling the method NewDocument creates a new CommandImplementationsManager instance representing an empty document. As the user adds and deletes text to the document, TextAddImplementation and TextDeleteImplementation interface instances are added to the CommandImplementationsManager instance. For the context of the program the application logic exists in the implementation of the ICommand interface instances. What is not shown is what text to add or delete because it is not the responsibility of CommandImplementationsManager. CommandImplementationsManager expects the ICommand interface instances to figure out where to add or delete text. CommandImplementationsManager is only responsible for putting everything together in a robust, repeatable sequential manner. After the command FileSaveImplementation is executed and added to CommandImplementationsManager, the document is considered saved and the application can be exited.
The following source illustrates steps 6 through 10 carried out by the other user:
CommandImplementationsManager cmdMgr = LoadDocument(); // Other code cmdMgr.Remove( IterateAndFindCommand( cmdMgr.GetEnumerator())); // Other code cmdMgr.Execute();
When a document is loaded, so is the list of ICommand interface instances. The document content loads when the ICommand instances do. To delete a command from the list, the method Remove is used. Changing the contents of the list will require the list of ICommand interface instances to be executed again, as there are changes in the state of the individual object instances. The ICommand instances are executed using the command Execute, which that will iterate the individual ICommand interface instances and execute them.
|

