23种设计模式之(十四)命令模式(python_c++实现)

23种设计模式之(十四)命令模式(Command)

本文主要介绍23种设计模式之命令模式,附详细python/c++示例代码。
- 概念
- 应用场景
- 注意事项
- 代码示例
- 总结
- 代码链接


命令模式(Command)

概念

命令模式是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。

在面向对象的程序设计中,一个对象调用另一个对象,一般情况下的调用过程是,创建目标对象实例,设置调用参数,调用目标对象的方法。但在有些情况下有必要使用一个专门的类对这种调用过程加以封装,我们把这种专门的类称作command类,整个调用过程比较繁杂,或者存在多处这种调用。这时,使用Command类对该调用加以封装,便于功能的再利用。

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

应用场景

(1)、认为是命令的地方都可以使用命令模式,比如GUI 中每一个按钮都是一条命令。

( 2)、模拟 CMD。

代码示例

C++代码示例

/************************************************************************/
/* 设计模式专题
/*
/* 命令模式
/*
/* Author  : zzl
/*
/* 编程环境: window10 vs2010
/*
/* Date    : 20180917
/************************************************************************/

#include <iostream>
#include "list"

class Doctor
{
public:
    void treat_eye()
    {
        printf("treat eye \n");
    }

    void treat_nose()
    {
        printf("treat nose \n");
    }
};

class Command
{
public:
    virtual void treat() = 0;
};
class CommandTreatEye : public Command
{
public:
    CommandTreatEye(Doctor *doctor)
    {
        m_doctor = doctor;
    }
    void treat()
    {
        m_doctor->treat_eye();
    }
private:
    Doctor *m_doctor;
};


class CommandTreatNose : public Command
{
public:
    CommandTreatNose(Doctor *doctor)
    {
        m_doctor = doctor;
    }
    void treat()
    {
        m_doctor->treat_nose();
    }
private:
    Doctor *m_doctor;
};


class BeautyNurse
{
public:
    BeautyNurse(Command *command)
    {
        this->command = command;
    }
public:
    void SubmittedCase() //提交病例 下单命令
    {
        command->treat();
    }
protected:
private:
    Command *command;
};

//护士长
class HeadNurse 
{
public:
    HeadNurse()
    {
        m_list.clear();
    }

public:
    void setCommand(Command *command)
    {
        m_list.push_back(command);
    }
    void SubmittedCase() //提交病例 下单命令
    {
        for (std::list<Command *>::iterator it=m_list.begin(); it!=m_list.end(); it++)
        {
            (*it)->treat();
        }
    }
private:
    std::list<Command *> m_list;
};

void main()
{

    //护士提交简历 给以上看病
    HeadNurse       *headnurse = NULL;
    Doctor          *doctor = NULL;
    Command         *command1  = NULL;
    Command         *command2  = NULL;

    doctor = new Doctor ;

    command1 = new CommandTreatEye(doctor); 
    command2 = new CommandTreatNose(doctor); 

    headnurse = new HeadNurse(); //new 护士长
    headnurse->setCommand(command1);
    headnurse->setCommand(command2);

    headnurse->SubmittedCase(); // 护士长 批量下单命令

    delete doctor;
    delete command1;
    delete command2;
    delete headnurse;

}

python代码示例

# -*- coding: utf-8 -*-
###################################################################
# 设计模式专题
# 
# 命令模式
# 
# Author  : zzl
# 
# 编程环境: window10 python2.7
# 
# Date    : 20180917
##################################################################

class Doctor(object):

    def treat_eye(self):
        print("treat eye")

    def treat_nose(self):
        print("treat nose")


class Command(object):
    def treat(self):
        pass


class CommandTreatEye(Command):

    def __init__(self, doctor):
        self.m_doctor = doctor

    def treat(self):
        self.m_doctor.treat_eye()


class CommandTreatNose(Command):
    def __init__(self, doctor):
        self.m_doctor = doctor

    def treat(self):
        self.m_doctor.treat_nose()


class BeautyNurse(object):
    def __init__(self, command):
        self.m_command = command

    def submitted_case(self):
        self.m_command.treat()


class HeadNurse(object):
    def __init__(self):
        self.m_list = []

    def add_command(self, command):
        self.m_list.append(command)

    def submitted_case(self):
        for command in self.m_list:
            command.treat()


if __name__ == "__main__":
    # 护士提交简历给医生看病

    doctor = Doctor()
    command1 = CommandTreatEye(doctor)
    command2 = CommandTreatNose(doctor)

    head_nurse = HeadNurse()

    head_nurse.add_command(command1)
    head_nurse.add_command(command2)

    head_nurse.submitted_case()

源码链接

设计模式(十四)命令模式示例代码(python–c++)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值