行为型模式-命令(command)

命令

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作.

实例

main.cc:

#include 
#include "club_manager.h"
#include "sing_command.h"
#include "dance_command.h"
#include "chicken_command.h"
#include 
using namespace std;

/*
design_pattern:"command"
Running club needs funding, funding from sponsors, of course, 
the sponsor is not the volunteer, to give you the money, 
there will be a corresponding request,These requests will be handed out by the Club manager.
request contains : sing,dance,chicken joy.
*/
int main(){
    RunnerClub *runner_club = new RunnerClub();
    Command *sing_command = new SingCommand(runner_club);
    Command *dance_command = new DanceCommand(runner_club);
    Command *chicken_command = new ChickenCommand(runner_club);
    ClubManager *club_manager = new ClubManager();
    club_manager->AddCommand(sing_command);
    club_manager->AddCommand(dance_command);
    club_manager->AddCommand(chicken_command);
    //After a while, the sponsor thought that the scene was too noisy and called 
    //the club manager to cancel the singing
    club_manager->DeleteCommand(sing_command);
    club_manager->Action();

    //clear
    delete runner_club;
    delete sing_command;
    delete dance_command;
    delete chicken_command;
    delete club_manager;
    system("Pause");
    return 0;
}

ClubManager:

//club_manager.h
#ifndef HELENDP_SOURCE_CLUB_MANAGER_H_
#define HELENDP_SOURCE_CLUB_MANAGER_H_
#include "command.h"
#include 
using namespace std;

class ClubManager{
public:
    ClubManager();
    ~ClubManager();
    void AddCommand(Command *command);
    void DeleteCommand(Command *command);
    void Action();
private:
    list command_list_;
};
#endif


//club_manager.cc:
#include "club_manager.h"
#include 
using namespace std;

ClubManager::ClubManager(){

}

ClubManager::~ClubManager(){

}

void ClubManager::AddCommand(Command * command){
    command_list_.push_back(command);
}

void ClubManager::DeleteCommand(Command * command){
    command_list_.remove(command);
}

void ClubManager::Action(){
    list::iterator iterator;
    for (iterator = command_list_.begin(); iterator != command_list_.end(); ++iterator){
        (*iterator)->ExcuteCommand();
    }
}

Command:

//command.h
#ifndef HELENDP_SOURCE_COMMAND_H_
#define HELENDP_SOURCE_COMMAND_H_
#include "runner_club.h"

class Command{
public:
    Command(RunnerClub *runner_club);
    virtual ~Command();
    virtual void ExcuteCommand();
protected:
    RunnerClub *runner_club_;
};
#endif


//command.cc
#include "command.h"

Command::Command(RunnerClub * runner_club){
    runner_club_ = runner_club;
}

Command::~Command(){

}

void Command::ExcuteCommand(){

}

ChickenCommand:

//chicken_command.h
#ifndef HELENDP_SOURCE_CHICKEN_COMMAND_H_
#define HELENDP_SOURCE_CHICKEN_COMMAND_H_
#include "command.h"

class ChickenCommand : public Command{
public:
    ChickenCommand(RunnerClub *runner_club);
    virtual ~ChickenCommand();
    virtual void ExcuteCommand();
protected:
    RunnerClub *runner_club_;
};

#endif


//chicken_command.cc
#include "chicken_command.h"

ChickenCommand::ChickenCommand(RunnerClub *runner_club)
    :Command(runner_club){
}

ChickenCommand::~ChickenCommand(){

}

void ChickenCommand::ExcuteCommand(){
    runner_club_->CosplayChicken();
}

DanceCommand:

//dance_command.h
#ifndef HELENDP_SOURCE_DANCE_COMMAND_H_
#define HELENDP_SOURCE_DANCE_COMMAND_H_
#include "command.h"

class DanceCommand : public Command{
public:
    DanceCommand(RunnerClub *runner_club);
    virtual ~DanceCommand();
    virtual void ExcuteCommand();
protected:
    RunnerClub *runner_club_;
};

#endif


//dance_command.cc
#include "dance_command.h"

DanceCommand::DanceCommand(RunnerClub *runner_club)
    :Command(runner_club){
}

DanceCommand::~DanceCommand(){

}

void DanceCommand::ExcuteCommand(){
    runner_club_->Dance();
}

SingCommand:

//sing_command.h
#ifndef HELENDP_SOURCE_SING_COMMAND_H_
#define HELENDP_SOURCE_SING_COMMAND_H_
#include "command.h"

class SingCommand : public Command{
public:
    SingCommand(RunnerClub *runner_club);
    virtual ~SingCommand();
    virtual void ExcuteCommand();
protected:
    RunnerClub *runner_club_;
};
#endif


//sing_command.cc
#include "sing_command.h"

SingCommand::SingCommand(RunnerClub *runner_club)
    :Command(runner_club){
}

SingCommand::~SingCommand(){

}

void SingCommand::ExcuteCommand(){
    runner_club_->Sing();
}

RunnerClub:

//runner_club.h
#ifndef HELENDP_SOURCE_RUNNER_CLUB_H_
#define HELENDP_SOURCE_RUNNER_CLUB_H_

class RunnerClub{
public:
    RunnerClub();
    ~RunnerClub();
    void Sing();
    void Dance();
    void CosplayChicken();
};
#endif


//runner_club.cc
#include "runner_club.h"
#include 
using namespace std;

RunnerClub::RunnerClub(){

}

RunnerClub::~RunnerClub(){

}

void RunnerClub::Sing(){
    cout << "RunnerClub Sing!" << endl;
}

void RunnerClub::Dance(){
    cout << "RunnerClub Dance!" << endl;
}

void RunnerClub::CosplayChicken(){
    cout << "RunnerClub CosplayChicken!" << endl;
}

代码和UML图(EA)工程文件,最后会整理打包上传.

UML类图

行为型模式-命令(command)_第1张图片

结构

  • Command(Command):执行命令的接口.
  • ConcreteCommand(ChickenCommand,DanceCommand,SingCommand):具体执行命令的类,将接收者对象与命令绑定.
  • Invoker(ClubManager):下发命令的类.
  • Receiver(RunnerClub):命令的接收者.

优点

  • 降低系统的耦合度.
  • 新的命令可以很容易地加入到系统中.
  • 可以比较容易地设计一个命令队列和宏命令(组合命令).
  • 可以方便地实现对请求的Undo和Redo.

缺点

  • 使用命令模式可能会导致某些系统有过多的具体命令类。因为针对每一个命令都需要设计一个具体命令类,因此某些系统可能需要大量具体命令类,这将影响命令模式的使用.

你可能感兴趣的:(设计模式,设计模式,命令模式)