Click here to Skip to main content
15,912,932 members
Home / Discussions / C#
   

C#

 
AnswerRe: Urgent??? Pin
Arjun "Mjolnir" Bahree28-Mar-07 10:12
Arjun "Mjolnir" Bahree28-Mar-07 10:12 
GeneralUrgent???? Pin
Tkml23628-Mar-07 10:16
Tkml23628-Mar-07 10:16 
Questionhow to listen windows net send service Pin
Nkuttynasi28-Mar-07 8:15
Nkuttynasi28-Mar-07 8:15 
AnswerRe: how to listen windows net send service Pin
Arjun "Mjolnir" Bahree28-Mar-07 10:08
Arjun "Mjolnir" Bahree28-Mar-07 10:08 
GeneralRe: how to listen windows net send service Pin
Nkuttynasi2-Apr-07 2:34
Nkuttynasi2-Apr-07 2:34 
QuestionProblem populating ListView by a CSV file? Pin
Khoramdin28-Mar-07 7:45
Khoramdin28-Mar-07 7:45 
AnswerRe: Problem populating ListView by a CSV file? Pin
J$28-Mar-07 9:24
J$28-Mar-07 9:24 
QuestionHe only had a toothache? Um... Dog.Euthanize.Undo()... Pin
Edmundisme28-Mar-07 7:29
Edmundisme28-Mar-07 7:29 
OK, forget the Dog/Vet metaphor I posted yesterday.[^]. It was the easiest way to describe my original question which was general in nature and not specific to my problem. Here is the actual scenario.

I have an editor (type of data being edited is irrelevant). In this editor, I want to support undo/redo operations. I have employed the Command pattern for this purpose. All changes to underlying data must be encapsulated in a command object. For example, to set text you would create a SetText command and call its “Execute” method. To undo this operation, you would call the SetText object’s “Unexecute” method.

This architecture is working really well but it has one flaw. Every time an engineer executes a command he is responsible for managing command history (i.e. the undo/redo stacks). If the execution is successful, he MUST add the command to the undo stack. If the execution fails, he MUST NOT add the command to the undo stack. If the execution throws an exception, he MUST CLEAR THE STACK as we cannot allow the undo stack at any time to be out of sync with the application state (this logic is somewhat simplified for the purposes of this post).

If the engineer forgets to do any of these things he poses a risk of undefined behavior in the application. Furthermore, the logic will be the same for every command that gets executed, so there’s no need to write this command-history-management-logic every time a command is executed.

In steps the factory pattern. The factory encapsulates the creation of commands. Because the factory controls the creation of the commands, it can setup a relationship between the UndoManager class (manages command history) and each command that gets created. Voila!

Now here’s my problem (and perhaps it’s not a problem at all. Perhaps this is where I’m rubegoldberging). Any engineer can create a command and execute it. If the UndoManager class has no knowledge of this command, he cannot manage it and the undo history could get out of sync with the application state. Obviously this is really bad.

To solve this problem, I have nested a private UndoManager class in the command factory. This class implements the IUndoManager interface. All commands require an IUndoManager at construction. So, because the UndoManager is private to the factory, only the factory can create commands. For example, to create a generic command:

public ICommand GetGenericCommand()
{ return new GenericCommand(this.undoManager); }

It is true that any engineer could implement IUndoManager and then begin creating rogue commands, but this is unlikely.

I would love to hear comments on this solution. Am I being overly cautious? Is this a hack? Or have I implemented a reasonable safeguard in a reasonable fashion? Is there a pattern that matches this solution? (Hopefully not an antipattern… )

Thanks!
AnswerRe: He only had a toothache? Um... Dog.Euthanize.Undo()... Pin
led mike28-Mar-07 8:06
led mike28-Mar-07 8:06 
AnswerRe: He only had a toothache? Um... Dog.Euthanize.Undo()... Pin
Leslie Sanford28-Mar-07 8:14
Leslie Sanford28-Mar-07 8:14 
GeneralRe: He only had a toothache? Um... Dog.Euthanize.Undo()... Pin
Edmundisme28-Mar-07 8:26
Edmundisme28-Mar-07 8:26 
QuestionGNU license question (version 2) Pin
kamPooh28-Mar-07 6:14
kamPooh28-Mar-07 6:14 
AnswerRe: GNU license question (version 2) Pin
Stanciu Vlad28-Mar-07 8:20
Stanciu Vlad28-Mar-07 8:20 
GeneralRe: GNU license question (version 2) Pin
Wayne Phipps28-Mar-07 9:25
Wayne Phipps28-Mar-07 9:25 
GeneralRe: GNU license question (version 2) Pin
kamPooh28-Mar-07 15:34
kamPooh28-Mar-07 15:34 
GeneralRe: GNU license question (version 2) Pin
Nathan Addy28-Mar-07 15:52
Nathan Addy28-Mar-07 15:52 
QuestionHow to delete PDF files after viewing Pin
AndrusM28-Mar-07 5:50
AndrusM28-Mar-07 5:50 
QuestionAdapting form size to content size Pin
sinosoidal28-Mar-07 5:45
sinosoidal28-Mar-07 5:45 
AnswerRe: Adapting form size to content size Pin
Obaid ur Rehman28-Mar-07 7:43
Obaid ur Rehman28-Mar-07 7:43 
AnswerRe: Adapting form size to content size Pin
Arjun "Mjolnir" Bahree28-Mar-07 10:20
Arjun "Mjolnir" Bahree28-Mar-07 10:20 
AnswerRe: Adapting form size to content size Pin
Arjun "Mjolnir" Bahree28-Mar-07 10:21
Arjun "Mjolnir" Bahree28-Mar-07 10:21 
GeneralRe: Adapting form size to content size Pin
sinosoidal28-Mar-07 23:25
sinosoidal28-Mar-07 23:25 
QuestionMaximized child MDI form Pin
sinosoidal28-Mar-07 5:39
sinosoidal28-Mar-07 5:39 
Questiondisplaying images from a xml file Pin
diducmy28-Mar-07 5:35
diducmy28-Mar-07 5:35 
AnswerRe: displaying images from a xml file Pin
Martin#28-Mar-07 5:56
Martin#28-Mar-07 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.