Click here to Skip to main content
16,013,747 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Custom Word toolbars Pin
dlhson2-Apr-02 5:16
dlhson2-Apr-02 5:16 
GeneralCComboBox Font Pin
Stew2-Apr-02 4:25
Stew2-Apr-02 4:25 
GeneralRe: CComboBox Font Pin
Mazdak2-Apr-02 4:36
Mazdak2-Apr-02 4:36 
GeneralRe: CComboBox Font Pin
Stew2-Apr-02 4:40
Stew2-Apr-02 4:40 
GeneralRe: CComboBox Font Pin
dlhson2-Apr-02 5:08
dlhson2-Apr-02 5:08 
QuestionHow would you approach this situation? Pin
User 98852-Apr-02 4:17
User 98852-Apr-02 4:17 
AnswerRe: How would you approach this situation? Pin
Mukkie2-Apr-02 5:12
Mukkie2-Apr-02 5:12 
AnswerRe: How would you approach this situation? Pin
Joaquín M López Muñoz2-Apr-02 5:52
Joaquín M López Muñoz2-Apr-02 5:52 
If your set of commands is relatively fixed, you can take advantage of a design pattern called visitor. Define an abstract class CommandVisitor like this:
class CommandVisitor
{
  // as many overloads for visits as there are commands 
  virtual void visit(Command1&);
  virtual void visit(Command2&);
  ...
  virtual void visit(Commandn&);
}
Change the Command base class to match the visit methods with a corresponding accept method:
class Command
{
  ... // as before
  virtual void accept(CommandVisitor&)=0;
}
Now, each Command, when issued an accept by some generic visitor, simply derives to the corresponding visit overload:
class Command2: public Command
{
  void accept(CommandVisitor& visitor)
  {
    visitor.visit(*this); // calls the overload of CommandVisitor::visit for Command2
  }
}
So far so good. Now you can turn protocol #1 into a visitor by moving there the code previously found on the Commands themselves:
class Protocol1:public Visitor
{
  string result;
  string toString(Command& command)
  {
    // let the visitor magic do the matching
    command.accept(*this);
    // take the result
    return result;
  }
  void visit(Command1 &command)
  {
    result=...; //stuff for Command1.
  }
  ...
  void visit(Commandn &command)
  {
    result=...; //stuff for Commandn.
  }
}
Got the idea? You moved the protocol to string stuff to a loosely coupled visitor. To implement a second protocol just create a different visitor in the same way. A little hard to grasp at first, but it can greatly improve your design in the long term. This code is just shown as an indication, it surely won't even compile. Make a search on "Visitor pattern" on the web for lots of further info.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
GeneralRe: How would you approach this situation? Pin
User 98852-Apr-02 5:56
User 98852-Apr-02 5:56 
GeneralRe: How would you approach this situation? Pin
Joaquín M López Muñoz2-Apr-02 6:05
Joaquín M López Muñoz2-Apr-02 6:05 
GeneralRe: How would you approach this situation? Pin
Tim Smith2-Apr-02 6:15
Tim Smith2-Apr-02 6:15 
GeneralExcel to PDF Pin
Giles2-Apr-02 4:07
Giles2-Apr-02 4:07 
GeneralRe: Excel to PDF Pin
dlhson2-Apr-02 6:36
dlhson2-Apr-02 6:36 
GeneralRe: Excel to PDF Pin
Giles2-Apr-02 8:26
Giles2-Apr-02 8:26 
GeneralMessageBox Question Pin
sriram_mr2-Apr-02 4:13
sriram_mr2-Apr-02 4:13 
GeneralRe: MessageBox Question Pin
Navin2-Apr-02 4:24
Navin2-Apr-02 4:24 
GeneralRe: MessageBox Question Pin
Navin2-Apr-02 4:29
Navin2-Apr-02 4:29 
GeneralRe: MessageBox Question Pin
sriram_mr2-Apr-02 4:34
sriram_mr2-Apr-02 4:34 
GeneralRe: MessageBox Question Pin
Jeremy Falcon2-Apr-02 5:11
professionalJeremy Falcon2-Apr-02 5:11 
GeneralThere is a very simple solution.. Pin
SeNs2-Apr-02 8:56
SeNs2-Apr-02 8:56 
GeneralOFNHookProc Pin
Anthony98872-Apr-02 4:07
Anthony98872-Apr-02 4:07 
GeneralDialogs in MDI app have gray titlebar Pin
Jack Handy2-Apr-02 3:55
Jack Handy2-Apr-02 3:55 
GeneralRe: Dialogs in MDI app have gray titlebar Pin
dlhson2-Apr-02 5:50
dlhson2-Apr-02 5:50 
GeneralRe: Dialogs in MDI app have gray titlebar Pin
Jack Handy2-Apr-02 11:21
Jack Handy2-Apr-02 11:21 
GeneralMainFrame variabel access in worker thread Pin
Merle Pittman2-Apr-02 3:16
Merle Pittman2-Apr-02 3:16 

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.