設計模式之: 責任鏈模式(chain of responsibility)
阿新 • • 發佈:2018-12-10
責任鏈模式
責任鏈模式,強調的是一種轉發處理機制,業務處理物件由某種屬性(如,級別高低)而相互關聯在一起,而業務本身也由某種屬性(如,級別高低)來標識,當業務到達業務處理物件時,該處理物件會將業務中的屬性與自己的屬性進行對比,如果是業務屬性範圍內的任務,則予以處理,否則丟擲到更高級別的業務處理物件中;比如:
戰場情報 => 師指揮部(根據情報級別,決定處理或發出) => 集團軍指揮部(根據情報級別,決定處理或發出) => 總參謀部(處理)
c++實現
/*************************************************************************
> File Name: ChainOfResponsibility.cpp
> Author: XXDK
> Email: [email protected]
> 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: [email protected]
> 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);
}