command.h
#ifndef COMMAND_H
#define COMMAND_H
#include "receiver.h"
class Command
{
public:
Command(Receiver *receiver)
{
m_receiver = receiver;
}
virtual void execute(){}
protected:
Receiver *m_receiver;
};
class ConcreteCommand : public Command
{
public:
ConcreteCommand(Receiver *receiver):Command(receiver) {}
void execute(){ m_receiver->action(); }
};
#endif // COMMAND_H
invoker.h
#ifndef INVOKER_H
#define INVOKER_H
#include "command.h"
class Invoker
{
public:
Invoker() {}
void setCommand(Command *command)
{
m_command = command;
}
void executeCommand()
{
m_command->execute();
}
private:
Command *m_command;
};
#endif // INVOKER_H
receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QtDebug>
class Receiver
{
public:
Receiver() {}
void action()
{
qDebug() << "執行請求";
}
};
#endif // RECEIVER_H
UML