设计模式之: 责任链模式(chain of responsibility)

责任链模式


责任链模式,强调的是一种转发处理机制,业务处理对象由某种属性(如,级别高低)而相互关联在一起,而业务本身也由某种属性(如,级别高低)来标识,当业务到达业务处理对象时,该处理对象会将业务中的属性与自己的属性进行对比,如果是业务属性范围内的任务,则予以处理,否则抛出到更高级别的业务处理对象中;比如:

战场情报 => 师指挥部(根据情报级别,决定处理或发出) => 集团军指挥部(根据情报级别,决定处理或发出) => 总参谋部(处理)


c++实现

/*************************************************************************
	> File Name: ChainOfResponsibility.cpp
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Wed 19 Sep 2018 05:52:20 PM CST
 ************************************************************************/

#include<iostream>
using namespace std;

enum class Level
{
	TOP_SECRET,
	CONFIDENTIAL,
	SECRET
};

class Intelligence {
public:
	Intelligence(const std::string& info, Level lv) 
		: info_(info),
		  lv_(lv) {}

	virtual ~Intelligence() {}

	std::string GetInfo() const {
		return info_;
	}

	Level GetLevel() const {
		return lv_;
	}

private:
	std::string info_;
	Level lv_;
};

class Command {
public:
	Command(const std::string& name, Command* directly_superior, Level lv)
		: name_(name),
		  superior_(directly_superior),
		  lv_(lv) {}

	void Process(Intelligence& itl) {
		if(itl.GetLevel() < lv_ && superior_) {
			std::cout << this->GetName() << ": insufficient level, pass to superior." << std::endl;
			superior_->Process(itl);
		}
		else 
			std::cout << this->name_ << " Processing intelligence: " << itl.GetInfo() << std::endl;
	}

	std::string GetName() {
		return name_;
	}

	~Command(){};

private:
	std::string name_;
	Level lv_;
	Command* superior_;
};

int main()
{
	Level lv = Level::TOP_SECRET;
	std::string info("Fall Blau.");
	Intelligence itl_top(info, lv);

	lv = Level::CONFIDENTIAL;
	info = "Arriving at the designated location.";
	Intelligence itl_cfd(info, lv);

	Command* GeneralStaff = new Command("GeneralStaff", nullptr, Level::TOP_SECRET);
	Command* GroupArmyCommand = new Command("GroupArmyCommand", GeneralStaff, Level::CONFIDENTIAL);
	Command* DevisionCommand = new Command("DevisionCommand", GroupArmyCommand, Level::SECRET);
		
	DevisionCommand->Process(itl_top);
	std::cout << std::endl;
	DevisionCommand->Process(itl_cfd);

	return 0;
}

c实现

/*************************************************************************
	> File Name: ResponsibilityChain.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 20 Sep 2018 10:31:17 AM CST
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

enum Level {TOP_SECRET, CONFIDENTIAL, SECRET};

typedef struct _Intelligence
{
	char info_[64];	
	enum Level lv_;
}Intelligence;

typedef struct _Command
{
	char name_[64];
	enum Level lv_;
	void (*Process)(struct _Command* this, Intelligence* itl);
	struct _Command* superior_;
}Command;

void Process(Command* this, Intelligence* itl)
{
	if(itl->lv_ < this->lv_ && this->superior_){
		printf("%s: insufficient level, pass to superior.\n", this->name_); 
		this->superior_->Process(this->superior_, itl);
	}
	else 
		printf("%s Processing intelligence: %s\n", this->name_, itl->info_);
}

Command GeneralStaff = {
	.name_ = "GeneralStaff",
	.lv_ = CONFIDENTIAL,
	.Process = Process,
	.superior_ = NULL
};

Command GroupArmyCommand = {
	.name_ = "GroupArmyCommand",
	.lv_ = CONFIDENTIAL,
	.Process = Process,
	.superior_ = &GeneralStaff
};

Command DevisionCommand = {
	.name_ = "DevisionCommand",
	.lv_ = SECRET,
	.Process = Process,
	.superior_ = &GroupArmyCommand
};

int main()
{	
	Intelligence *itl = (Intelligence*)malloc(sizeof(Intelligence));
	itl->lv_ = TOP_SECRET;
	strcpy(itl->info_, "Fall Blau.");
	DevisionCommand.Process(&DevisionCommand, itl);

	printf("\n");
	memset(itl, 0, sizeof(Intelligence));
	itl->lv_ = CONFIDENTIAL;
	strcpy(itl->info_, "Arriving at the designated location.");
	DevisionCommand.Process(&DevisionCommand, itl);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值