Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
/// Enable multiple objects to process requests, so as to avoid coupling between request senders and recipients.
/// Train this object into a chain and pass the request along this chain until an object processes it.
///
/// Example:
/// The employee sends the application documents directly to his/her supervisor;
/// As to who has the permission to audit, the sender does not have to worry about it.
///
Namespace ChainOfResponsibilityPattern
{
Class Program
{
Static void Main (string [] args)
{
Manager commonManager = new CommonManager ("Spike ");
Manager majorManager = new MajorManager ("kenny ");
Manager generalManager = new GeneralManager ("John ");
CommonManager. SetSuperior (majorManager );
MajorManager. SetSuperior (generalManager );
Request request = new Request ();
Request. RequestType = "leave ";
CommonManager. RequestApplications (request );
Request = new Request ();
Request. RequestType = "salary increase ";
CommonManager. RequestApplications (request );
Request = new Request ();
Request. RequestType = "resign ";
CommonManager. RequestApplications (request );
Console. Read ();
}
}
Public abstract class Manager
{
Public string name;
// Superior Manager
Protected Manager superior;
Public Manager (string name)
{
This. name = name;
}
// Set the manager superior
Public void SetSuperior (Manager superior)
{
This. superior = superior;
}
// Request
Public abstract void RequestApplications (Request request );
}
// Common Manager
Public class CommonManager: Manager
{
Public CommonManager (string name): base (name ){}
Public override void RequestApplications (Request request)
{
If (request. RequestType = "leave ")
{
Console. WriteLine ("your application form has been approved by Common Manager [" + name + ");
}
Else
{
If (superior! = Null)
{
Superior. RequestApplications (request );
}
Else
{
Console. WriteLine ("no one has permission to approve [" + request. RequestType + ");
}
}
}
}
// Major Manager
Public class MajorManager: Manager
{
Public MajorManager (string name): base (name ){}
Public override void RequestApplications (Request request)
{
If (request. RequestType = "Please take a long vacation ")
{
Console. WriteLine ("your long-term application form has been approved by Major Manager [" + name + ");
}
Else
{
If (superior! = Null)
{
Superior. RequestApplications (request );
}
Else
{
Console. WriteLine ("no one has permission to approve [" + request. RequestType + ");
}
}
}
}
// General Manager
Public class GeneralManager: Manager
{
Public GeneralManager (string name): base (name ){}
Public override void RequestApplications (Request request)
{
If (request. RequestType = "raise salary ")
{
Console. WriteLine ("your salary increase application has been approved by general Manager [" + name + ");
}
Else
{
If (superior! = Null)
{
Superior. RequestApplications (request );
}
Else
{
Console. WriteLine ("no one has permission to approve [" + request. RequestType + ");
}
}
}
}
// Apply
Public class Request
{
// Application Type
Public string RequestType {set; get ;}
// Application content
Public string RequestContent {set; get ;}
// Number
Public string Number {set; get ;}
}
}