最近打算学习设计模式,刚开始总觉得有点难,貌似挺难想到的。于是本想从网上找C++设计模式视频,可是很失望的没找到。。。。
很不情愿的看了一集马士兵的Java设计模式,讲的挺好,就是废话有点多,还有Java没学过。。。。
于是听了一下,顺便用C++实现一下。
标准的责任链模式类图如下,还是看一下把
一:
下面实现的是一个过滤器,比如,当你发贴时,需要检测内容的安全性,如有没有敏感词汇,会不会有脚本攻击......
为了方便和简洁,没有接口与实现分离。
#include <string> #include <vector> #include <iostream> using namespace std; class Filter { public: virtual string do_Filter(string& msg)=0; }; class SymbolFilter:public Filter { public: string do_Filter(string& msg) { return msg+"^^^SymbolFilter^^^"; } }; class ContentFilter:public Filter { public: string do_Filter(string& msg) { return msg+"$$$ContentFilter$$$"; } }; class FilterChain:public Filter { private: vector<Filter*> filter; public: void addFilter(Filter *f) { filter.push_back(f); } string do_Filter(string& msg) { vector<Filter*>::iterator beg=filter.begin(); string s(msg); while(beg!=filter.end()) { s=(*beg)->do_Filter(s); ++beg; } return s; } }; class MsgHandler { private: string msg; FilterChain filterchain; public: void setMsg(string& message) { msg=message; } void setFilterChain(FilterChain &fc) { filterchain=fc; } string do_Handler() { return filterchain.do_Filter(msg); } }; int main() { string msg("yhb****lwy"); MsgHandler handler; handler.setMsg(msg); FilterChain fc; fc.addFilter(new SymbolFilter()); FilterChain *fc2=new FilterChain(); fc2->addFilter(new ContentFilter()); fc.addFilter(fc2); handler.setFilterChain(fc); cout<<handler.do_Handler(); }
运行结果: yhb****lwy^^^SymbolFilter^^^$$$ContentFilter$$$
这个实现还是很巧的,FilterChain即可以添加普通Filter,也可以添加别的FilterChain。当需要新规则时,只要添加新的规则就好了。
二:
下面这个处理了如下问题
有一个请求和一个响应,过滤器既要处理请求也要处理响应,所有过滤器先处理请求,请求处理完后,按处理请求的反顺序处理响应。
这个运行顺序很像堆栈,下面的实现就是靠的函数调用。
时序图如下:
#include <string> #include <vector> #include <iostream> using namespace std; class Request{ public: string requestmsg; }; class Response{ public: string reponsemsg; }; class FilterChain; class Filter{ public: virtual void doFilter(Request& request,Response& response,FilterChain *fc)=0; virtual ~Filter() { } }; class FilterChain { public: vector<Filter*> filterChain; unsigned index; public: FilterChain():index(0){} void doFilter(Request& request,Response& response) { if(index==filterChain.size()) return; ++index; filterChain[index-1]->doFilter(request,response,this); } FilterChain& addFilter(Filter* f) { filterChain.push_back(f); return *this; } ~FilterChain() { vector<Filter*>::iterator iter=filterChain.begin(); while(iter!=filterChain.end()) { delete *iter++; } } }; class ContentFilter:public Filter { public: void doFilter(Request& request,Response& response,FilterChain *fc) { request.requestmsg+="_^ContentFilter_^"; fc->doFilter(request,response); response.reponsemsg+="...ContentFilter_Response"; } ~ContentFilter() {} }; class NameFilter:public Filter { public: void doFilter(Request& request,Response& response,FilterChain *fc) { request.requestmsg+="_*NameFilter*_"; fc->doFilter(request,response); response.reponsemsg+="...NameFilter_Response"; } ~NameFilter() {} }; int main(int argc,char** argv) { Request request; Response response; request.requestmsg="yhb@@lwy"; response.reponsemsg="..OK!.."; FilterChain fc; fc.addFilter(new ContentFilter()); fc.addFilter(new NameFilter()); fc.doFilter(request,response); cout<<"requstString:"<<request.requestmsg<<endl; cout<<"responseString:"<<response.reponsemsg<<endl; }
运行结果: requstString:yhb@@lwy_^ContentFilter_^_*NameFilter*_ responseString:..OK!.....NameFilter_Response...ContentFilter_Response
实现这个主要靠的是堆栈(通过函数调用的方式).
函数调用如下: