Re: Command Pattern used in Frameworks
From: Daniel T. (postmaster_at_eathlink.net)
Date: 06/24/04
- Next message: Daniel T.: "Re: Fans of Template Method with protected variable?"
- Previous message: Phlip: "Re: Fans of Template Method with protected variable?"
- In reply to: Posting Cow: "Command Pattern used in Frameworks"
- Next in thread: Posting Cow: "Re: Command Pattern used in Frameworks"
- Reply: Posting Cow: "Re: Command Pattern used in Frameworks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Jun 2004 02:18:16 GMT
In article <ecb3ca5c.0406190139.4cd8a47c@posting.google.com>,
postingcow@yahoo.com (Posting Cow) wrote:
>I am using the command pattern(as described in the GoF book) in a
>project, where I'm also using MFC as the window manager framework. The
>implementation follows the pattern very closely, with macroCommands
>and undo/redo mechanism just like in the book(it evolved naturally,
>then I changed the function names to fit the literature). I have found
>a problem in interfacing one of the Command classes which changes the
>state of the document being edited with MFC in a way that would make
>it portable to other frameworks later. Here's a code simplification of
>what I mean:
>
>class CMyDocument : public CDocument{ //the MFC document class
>public:
> std::list<int> theList;
>};
>
>class AddNumberCommand : public command{ //the GoF command class
> void execute(){
> CMyDocument* doc = GetActiveDocument(); //here is the hack
> doc->theList.push_back(2);
> }
>};
>
>What I would like is to make the command class unaware of any
>framework classes, so porting from one framework to another will not
>involve recompiling my library. Also, adding another member function
>to AddNumberCommand(like getNumberToAdd() for example) would break the
>undo/redo mechanism, which only knows about execute(), undo() and
>redo() (it would have to test all command derived classes to see if it
>is an AddNumberCommand from it's RTTI and call the extra function,
>which is not the solution I'm looking for).
>
>Does anybody know a clean way to solve this problem?
class CMyDocument : public CDocument {
std::list<int> itsList;
public:
static CMyDocument* activeDocument() {
return GetActiveDocument();
}
void add(int i) {
itsList.push_back(i);
}
};
class AddNumberCommand : public Command {
public:
void execute() {
CMyDocument::activeDocument()->add(2);
}
};
Now if you change to a different window system other than MFC, you only
need to change CMyDocument (which you would have had to change anyway,)
instead of both CMyDocument and AddNumberCommand.
Of course, this pattern assumes that there will always be one, and only
one, active document when the Command::execute message is sent. That may
or may not be a valid constraint.
One is also left to wonder; if I create an AddNumberCommand while one
document is active, then change the active document, then execute the
command, which document is supposed to change? Something you might want
to seriously think about before discounting RCM's idea...
- Next message: Daniel T.: "Re: Fans of Template Method with protected variable?"
- Previous message: Phlip: "Re: Fans of Template Method with protected variable?"
- In reply to: Posting Cow: "Command Pattern used in Frameworks"
- Next in thread: Posting Cow: "Re: Command Pattern used in Frameworks"
- Reply: Posting Cow: "Re: Command Pattern used in Frameworks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|