Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Command pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example == <!-- Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language under the sun. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Command --> This C++14 implementation is based on the pre C++98 implementation in the book. <syntaxhighlight lang="c++"> #include <iostream> #include <memory> class Command { public: // declares an interface for executing an operation. virtual void execute() = 0; virtual ~Command() = default; protected: Command() = default; }; template <typename Receiver> class SimpleCommand : public Command { // ConcreteCommand public: typedef void (Receiver::* Action)(); // defines a binding between a Receiver object and an action. SimpleCommand(std::shared_ptr<Receiver> receiver_, Action action_) : receiver(receiver_.get()), action(action_) { } SimpleCommand(const SimpleCommand&) = delete; // rule of three const SimpleCommand& operator=(const SimpleCommand&) = delete; // implements execute by invoking the corresponding operation(s) on Receiver. virtual void execute() { (receiver->*action)(); } private: Receiver* receiver; Action action; }; class MyClass { // Receiver public: // knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver. void action() { std::cout << "MyClass::action\n"; } }; int main() { // The smart pointers prevent memory leaks. std::shared_ptr<MyClass> receiver = std::make_shared<MyClass>(); // ... std::unique_ptr<Command> command = std::make_unique<SimpleCommand<MyClass> >(receiver, &MyClass::action); // ... command->execute(); } </syntaxhighlight> The program output is <syntaxhighlight lang="c++"> MyClass::action </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Command pattern
(section)
Add topic