Download presentation
Presentation is loading. Please wait.
1
Command-Processor Pattern
Separate the request for a service from its execution 발표자 김지원
2
Agenda Introduction Context Problem Solution Structure Behavior
Implementation Alternatives Conclusion References
3
Introduction Separate a request from a execution
or separate a UI from a execution Manage a request as separate objects Schedules a execution Additionally provides “UNDO”
4
Context What if? Application that
we want to make powerful editor supporting “UNDO” support multi-level undo support multiple UI ex) keyboard, mouse, tablet, stick, gun, fingertip, … Application that need flexible and extensible UI provide services related to the execution of user functions scheduling, undo, …
5
Problem many features and many internal functions
many mode and many user and many service All of them should be mapped, and added we need User favorites. different users like to work with an application in different ways. Code conservation Enhancements of the application should not break existing code Service Consistency “UNDO” works everywhere in application consistently
6
Solution Like command design pattern but more specific Command Object
how command objects are managed Command Object encapsulate requests into objects delegates the execution of its task to supplier components Command Processor takes care of all command objects schedules the execution of commands may store them for later undo may provide other services such as logging
7
Structure Class Abstract Command Collaborators Responsibility
Defines a uniform interface to execute commands. Extends the interface for services of the command processor, such as undo and logging.
8
Structure Class Command Collaborators Supplier Responsibility
Encapsulates a function request. Implements interface of abstract command. Uses suppliers to perform a request.
9
Structure Class Controller Collaborators Command processor Command
Responsibility Accepts service requests. Translates requests into commands. Transfers commands to command processor.
10
Structure Class Command Processor Collaborators Abstract Command
Responsibility Activates command execution. Maintains command objects. Provides additional services related to command execution.
11
Structure Class Supplier Collaborators Responsibility
Provides application specific functionality
12
Structure
13
Behavior
14
Implemenatation Define the interface of the abstract command
class AbstractCommand { public: enum CmdType { no_change, normal, no_undo }; virtual ~AbstractCommand(); virtual void doit(); virtual void undo(); CmdType getType() const { return type; } virtual String getName() const { return “NONAME”;} // gives name of command for selection // in undo/redo menu protected: CmdType type; AbstractCommand(CmdType t=no_change) : type(t) {} };
15
Implemenatation Design the command components (Use Memento Pattern)
class DeleteCmd : public AbstractCommand { public: DeleteCmd(TEDDI_Text *t, int start, int end) : AbstractCommand(normal), mytext(t), from(start), to(end) {/* … */ } virtual ~DeleteCmd(); virtual void doit(); // delete characters in mytext // between from end to and save them in delstr virtual void undo(); // insert delstr again at position from String getName() const { return “DELETE “ + delstr;} protected: TEDDI_Text *mytext; // plan for multiple text buffers int from, to; // range of characters to delete String delstr; // save deleted text for undo };
16
Implemenatation Increase flexibility by providing macro commands (use Composite pattern) class MacroCmd : public AbstractCommand { public: MacroCmd(String name, AbstractCommand *first) : AbstractCommand( first->getType() ), macroname(name) { /* … */ } virtual ~MacroCmd(); virtual void doit(); // do every command in cmdlist virtual void undo(); // undo all commands in cmdlist in reverse order virtual void finish(); // delete commands in cmdlist void add(AbstractCommand *next) { cmdlist.append(next); if (next->getType) no_undo) type = no_undo; /* … */ } String getName() const { return macroname;} protected: String macroname; OrderedCollection<AbstractCommand *> cmdlist; };
17
Implemenatation Implement the controller component. (Use creational pattern, Abstract Factory or Prototype) class TEDDI_controller::deleteButtonPressed() { AbstractCommand *delcmd = new DeleteWordCommand( this->getCursor(), // pass cursor position this->getText(); // pass text theCP->perform(delcmd); };
18
Implemenatation Implement access to the additional services of the command processor. class UndoCommand : public AbstractCommand { public: UndoCommand() : AbstractCommand(no_change) { } virtual ~UndoCommand(); virtual void doit() { theCP->undo_lastcmd(); } };
19
Implemenatation Implement the command processor component.
class CommandProcessor { public: CommandProcessor() virtual ~CommandProcessor(); virtual void do_cmd(AbstractCommand *cmd) { // do cmd and push it on donestack cmd->doit(); switch(cmd->getTyep()) { case AbstractCommand::normal: donestack.push(cmd); break; case AbstractCommand::no_undo: donestack.make_empty(); undonestack.make_empty(); // Fall through; case AbstractCommand::no_change: // Take responsibility for command objects: delete cmd; break; }
20
Implemenatation Implement the command processor component.
friend class UndoCommand; // special relationship friend class RedoCommand; // special relationship private: // this method is only used by UndoCommand virtual void undo_lastcmd(); // pop cmd from donestack, // undo it, and push it on undonestack // this method is only used by RedoCommand virtual void redo_lastundone(){ AbstractCommand *last = undonestack.pop(); if(last) this->do_cmd(last); } Stack<AbstractCommand *> donestack, undonestack; };
21
Alternatives Spread controller functionality
Menu group, Button group, … but not restricted to components of the GUI Combination with Interpreter pattern scripting language Used in ET++, MacApp, InterViews, ATM-P, SICAT
22
Conclusion Benefits Flexibility Programming execution-related services
in the way requests are activated in the number and functionality of requests Programming execution-related services Testability at application level play role with entry point Concurrency
23
Conclusion Liabilities Efficiency loss
many decouple, many indirection controller needs more touch. Potential for an excessive number of command classes many command, many class Solution 1 : By grouping commands around abstractions Solution 2 : By unifying very simple command classes by passing the supplier object as a parameter Solution 3 : By pre-programmed macro-command objects that rely on the combination of few low-level commands Complexity in acquiring command parameters
24
References GoF Command design pattern
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.