Command Header

namespace command_pattern

Namespace for the Command pattern.

class Command
#include <command.hpp>

The Command interface declares methods for executing and undoing commands.

Subclassed by command_pattern::ConcreteCommand

Public Functions

virtual ~Command() = default
virtual void execute() const = 0

Execute the command.

virtual void undo() const = 0

Undo the command.

class ConcreteCommand : public command_pattern::Command
#include <command.hpp>

ConcreteCommand binds a Receiver object with an action.

Public Functions

inline ConcreteCommand(std::shared_ptr<Receiver> receiver, const std::string &operation)

Constructor to initialize the command.

Parameters:
  • receiver – The receiver that performs the operation.

  • operation – The operation to be executed.

inline virtual void execute() const override

Execute the command by invoking the receiver’s action.

inline virtual void undo() const override

Undo the command by invoking the receiver’s reverseAction.

Private Members

std::shared_ptr<Receiver> receiver

The receiver performing the action.

std::string operation

The operation to be performed.

class Invoker
#include <command.hpp>

The Invoker class stores a command and triggers its execution or undo.

Public Functions

inline void setCommand(std::shared_ptr<Command> command)

Sets the command to be executed.

Parameters:

command – The command to set.

inline void pressButton() const

Executes the stored command.

inline void pressUndo() const

Undoes the stored command.

Private Members

std::shared_ptr<Command> command

The command to be executed.

class Receiver
#include <command.hpp>

The Receiver class performs the actual operations associated with a request.

Public Functions

inline void action(const std::string &operation) const
inline void reverseAction(const std::string &operation) const

Inner Classes

Inner Namespaces