本文主要是介绍模式中的chain of responsibility,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//chain.h
//example for chain of responsibility
#ifndef _CHAIN_H_
#define _CHAIN_H_
#ifndef _CHAIN_H_
#define _CHAIN_H_
#include
typedef int Topic;
const Topic NO_HELP_TOPIC = -1;
const Topic NO_HELP_TOPIC = -1;
class HelpHandler
{
public:
HelpHandler( HelpHandler* = 0, Topic = NO_HELP_TOPIC);
virtual bool HasHelp();
virtual void SetHandler(HelpHandler*, Topic);
virtual void HandleHelp();
private:
HelpHandler* _successor;
Topic _topic;
};
{
public:
HelpHandler( HelpHandler* = 0, Topic = NO_HELP_TOPIC);
virtual bool HasHelp();
virtual void SetHandler(HelpHandler*, Topic);
virtual void HandleHelp();
private:
HelpHandler* _successor;
Topic _topic;
};
class Widget : public HelpHandler
{
protected:
Widget( Widget* parent, Topic t = NO_HELP_TOPIC);
private:
Widget* _parent;
};
{
protected:
Widget( Widget* parent, Topic t = NO_HELP_TOPIC);
private:
Widget* _parent;
};
class Button : public Widget
{
public:
Button( Widget* d, Topic t = NO_HELP_TOPIC) : Widget(d,t){}
{
public:
Button( Widget* d, Topic t = NO_HELP_TOPIC) : Widget(d,t){}
virtual void HandleHelp();
};
};
class Dialog : public Widget
{
public:
Dialog( HelpHandler* h, Topic t = NO_HELP_TOPIC) : Widget(0){}
{
public:
Dialog( HelpHandler* h, Topic t = NO_HELP_TOPIC) : Widget(0){}
virtual void HandleHelp();
};
};
class Application : public HelpHandler
{
public:
Application(Topic t = NO_HELP_TOPIC) : HelpHandler(0,t) {}
{
public:
Application(Topic t = NO_HELP_TOPIC) : HelpHandler(0,t) {}
virtual void HandleHelp();
};
};
#endif
//chain.cpp
#include "chain.h"
HelpHandler::HelpHandler(HelpHandler* h, Topic t) :
_successor(h),_topic(t){}
bool HelpHandler::HasHelp()
{
return _topic != NO_HELP_TOPIC;
}
{
return _topic != NO_HELP_TOPIC;
}
void HelpHandler::HandleHelp()
{
if(_successor != 0)
{
_successor->HandleHelp();
}
}
{
if(_successor != 0)
{
_successor->HandleHelp();
}
}
Widget::Widget(Widget* w, Topic t) : HelpHandler(w,t)
{
_parent = w;
}
{
_parent = w;
}
void Button::HandleHelp()
{
if(HasHelp())
{
}
else
{
HelpHandler::HandleHelp();
}
}
{
if(HasHelp())
{
}
else
{
HelpHandler::HandleHelp();
}
}
void Dialog::HandleHelp()
{
if(HasHelp)
{
//offer help on the dialog
}
else
HelpHandler::HandleHelp();
}
{
if(HasHelp)
{
//offer help on the dialog
}
else
HelpHandler::HandleHelp();
}
void Application::HandleHelp()
{
//show a list of help topics
}
{
//show a list of help topics
}
//main.cpp
#include "chain.h"
void main()
{
const Topic PRINT_TOPIC = 1;
const Topic PAPER_ORIENTATION_TOPIC = 2;
const Topic APPLICATION_TOPIC = 3;
{
const Topic PRINT_TOPIC = 1;
const Topic PAPER_ORIENTATION_TOPIC = 2;
const Topic APPLICATION_TOPIC = 3;
Application* application = new Application(APPLICATION_TOPIC);
Dialog* dialog = new Dialog( application,PRINT_TOPIC);
Button* button = new Button( dialog,PAPER_ORIENTATION_TOPIC);
}
Dialog* dialog = new Dialog( application,PRINT_TOPIC);
Button* button = new Button( dialog,PAPER_ORIENTATION_TOPIC);
}
模式中的关键在于
1 各个类的类继承关系、构造函数的参数设置、和主程序中的创建顺序(关键的地方已经用红色标识。)
2 HelpHandler类中包含了自身类的成员变量_successor,是为了可以定义缺省的帮助处理,保证每个在链上的对象都有一致的处理请求和访问链上后继者的接口。
注意 任何HelpHandler类都可以作为Dialog的后继者,他的后继者可以被动态的改变。因此不管对话框被用在何处,都可以得到他正确的上下文相关的帮助信息。
这篇关于模式中的chain of responsibility的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!