命令模式(C++实现)

(本博客旨在个人总结回顾)

1、详情:

命令模式:将一个请求封装成对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

说明:

优点:①降低了系统耦合度。 ②新的命令可以很容易添加到系统中去。

缺点:使用命令模式可能会导致某些系统有过多的具体命令类。

使用场景:认为是命令的地方都可以使用命令模式,比如: ①GUI 中每一个按钮都是一条命令。 ②模拟 CMD。

注意事项:系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作,也可以考虑使用命令模式,见命令模式的扩展。

2.1、UML类图:

 

2.2、例子源码

stdafx.h

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <iostream>
using namespace std;

// TODO:  在此处引用程序需要的其他头文件

Receiver.h

#pragma once

class Receiver
{
public:
    Receiver();
    ~Receiver();

public:
    void Operation(int nState);
};

Receiver.cpp

#include "stdafx.h"
#include "Receiver.h"

Receiver::Receiver()
{
}

Receiver::~Receiver()
{
}

void Receiver::Operation(int nState)
{
    cout << "执行请求:" << nState << endl;
}

Command.h

#pragma once
#include "Receiver.h"

class Command
{
public:
    Command(Receiver* pReceiver, int nState);
    virtual ~Command();

public:
    virtual void Execute() = 0;

protected:
    Receiver* m_pReceiver;
    int m_nState;
};

Command.cpp

#include "stdafx.h"
#include "Command.h"

Command::Command(Receiver* pReceiver, int nState)
    : m_pReceiver(pReceiver)
    , m_nState(nState)
{
}

Command::~Command()
{
}

CommandX.h

#pragma once
#include "Command.h"

class CommandX :
    public Command
{
public:
    CommandX(Receiver* pReceiver, int nState);
    ~CommandX();

public:
    void Execute();
};

CommandX.cpp

#include "stdafx.h"
#include "CommandX.h"

CommandX::CommandX(Receiver* pReceiver, int nState) : Command(pReceiver, nState)
{
}

CommandX::~CommandX()
{
}

void CommandX::Execute()
{
    m_pReceiver->Operation(m_nState);
}

Sender.h

#pragma once
#include "Command.h"

class Sender
{
public:
    Sender();
    ~Sender();

public:
    void AddCommand(Command* pCommand);
    void RemoveCommand(Command* pCommand);
    void ExecuteCommand();

private:
    std::list<Command*>  m_listCommand;
};

Sender.cpp

#include "stdafx.h"
#include "Sender.h"

Sender::Sender()
{
}

Sender::~Sender()
{
    for (std::list<Command*>::iterator it = m_listCommand.begin(); it != m_listCommand.end(); it++)
    {
        delete *it;
    }
    m_listCommand.clear();
}

void Sender::AddCommand(Command* pCommand)
{
    m_listCommand.push_back(pCommand);
}

void Sender::ExecuteCommand()
{
    for (std::list<Command*>::iterator it = m_listCommand.begin(); it != m_listCommand.end(); it++)
    {
        (*it)->Execute();
    }
}

void Sender::RemoveCommand(Command* pCommand)
{
    for (std::list<Command*>::iterator it = m_listCommand.begin(); it != m_listCommand.end(); it++)
    {
        if (*it == pCommand)
        {
            delete *it;
            m_listCommand.erase(it);
            break;
        }
    }
}

调用代码

CommandPatternMemo.cpp

// CommandPatternMemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Receiver.h"
#include "CommandX.h"
#include "Sender.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Receiver* pReceiver = new Receiver();
    Command* pCommand = new CommandX(pReceiver, 1);
    Sender sender;
    sender.AddCommand(pCommand);
    pCommand = new CommandX(pReceiver, 2);
    sender.AddCommand(pCommand);
    sender.RemoveCommand(pCommand);
    pCommand = new CommandX(pReceiver, 3);
    sender.AddCommand(pCommand);
    pCommand = new CommandX(pReceiver, 4);
    sender.AddCommand(pCommand);
    
    sender.ExecuteCommand();

    delete pReceiver;
    pReceiver = NULL;

    system("pause");
	return 0;
}

2.3、运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值