设计模式(六)————命令模式(Command Pattern)

定义:将请求封装成一个对象,从而让你是用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

UML类图:

C++实现:

#include<iostream>
#include<string>
#include<mutex>
#include<stack>
using namespace std;

class Command
{
public:
	virtual void execute() = 0;
	virtual ~Command() = default;
};
class AutoBusiness
{
public:
	void buy()
	{
		cout << "Buy" << endl;
	}
	void Sale()
	{
		cout << "Sale" << endl;
	}
};
class BuyCommand :public Command
{
public:
	AutoBusiness* ab;
	BuyCommand(AutoBusiness* ab)
	{
		this->ab = ab;
	}
	// 通过 Command 继承
	virtual void execute() override
	{
		ab->buy();
	}
	virtual ~BuyCommand() {}
};
class SaleCommand :public Command
{
public:
	AutoBusiness* ab;
	SaleCommand(AutoBusiness* ab)
	{
		this->ab = ab;
	}
	// 通过 Command 继承
	virtual void execute() override
	{
		ab->Sale();
	}
	virtual ~SaleCommand() {}
};
class Invoker
{
	stack<Command*> commandList;
	Command* eCommand;
public:
	void tackCommand(Command* command)
	{
		commandList.push(command);
	}
	void action()
	{
		if (!commandList.empty())
		{
			eCommand = commandList.top();
			commandList.pop();
			eCommand->execute();
		}
		else
		{
			cout << "is not command!" << endl;
		}
	}
};

int main()
{
	Invoker* iv = new Invoker();
	AutoBusiness* ab = new AutoBusiness();
	SaleCommand* saleCommand = new SaleCommand(ab);
	BuyCommand* buyCommand = new BuyCommand(ab);
	iv->tackCommand(saleCommand);
	iv->tackCommand(buyCommand);

	iv->action();
	iv->action();
	iv->action();
	return 0;
}

C#实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static System.Console;

namespace ConsoleApp1
{

    interface Command
    {
        void execute();
    }
    class BuyCommand : Command
    {
        AutoBusiness ab;
        public BuyCommand(AutoBusiness ab)
        {
            this.ab = ab;
        }
        public void execute()
        {
            ab.Buy();
        }
    }
    class SaleCommand : Command
    {
        AutoBusiness ab;
        public SaleCommand(AutoBusiness ab)
        {
            this.ab = ab;
        }
        public void execute()
        {
            ab.Sale();   
        }
    }
    class AutoBusiness
    {
        public void Sale()
        {
            WriteLine("Sale");
        }
        public void Buy()
        {
            WriteLine("Buy");
        }
    }
    class Invoker
    {
        private Stack<Command> commandList = new Stack<Command>();
        private Command eCommand;
        public void tackCommand(Command command)
        {
            commandList.Push(command);
        }
        public void action()
        {
            if (commandList.Count > 0)
            {
                eCommand = commandList.Pop();
                eCommand.execute();
            }
            else 
                WriteLine("is not Command!");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Invoker iv = new Invoker();
            AutoBusiness ab = new AutoBusiness();
            SaleCommand saleCommand = new SaleCommand(ab);
            BuyCommand buyCommand = new BuyCommand(ab);

            iv.tackCommand(saleCommand);
            iv.tackCommand(buyCommand);

            iv.action();
            iv.action();
            iv.action();
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值