// Chain of responsibility. cpp: defines the entry point of the console application.
//
# Include "stdafx. H"
# Include <iostream>
Using namespace STD;
Typedef int topic;
Class Handler
{
Public:
Handler (handler * h = 0, topic t =-1): successor (H), topic (t)
{
}
Virtual ~ Handler ()
{
}
Virtual void handlerequest (topic t) = 0;
Protected:
Handler * successor;
Topic topic;
};
//////////////////////////////////////// //////////////
Class concretehandler: Public Handler
{
Public:
Concretehandler (handler * h = 0, topic t =-1): handler (h, T)
{
}
Virtual ~ Concretehandler (){}
Virtual void handlerequest (topic T)
{
If (t = topic)
{
Cout <"done this request:" <t <Endl;
Return;
}
Else
{
If (null! = Successor)
{
Cout <"throw to successor" <Endl;
Successor-> handlerequest (t );
}
}
}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
Handler * P1 = new concretehandler (0, 3 );
Handler * P2 = new concretehandler (P1, 2 );
Handler * P3 = new concretehandler (P2, 3 );
Handler * PX = new concretehandler (P3 );
// The PX request topic is 2.
PX-> handlerequest (2 );
Delete P1;
Delete P2;
Delete P3;
Delete PX;
Return 0;
}