Responsibility chain mode analysis, structure diagram and basic code

Source: Internet
Author: User
Tags class manager

Responsibility chain mode analysis, structure diagram and basic code
Zookeeper

Definition: Enables multiple objects to process requests to avoid coupling between request senders and receivers. Connect the object to a chain and pass the request along the chain until an object processes it.
Benefit: when a customer submits a request, the request is passed along the chain until a ConcreteHandler object is responsible for processing it. This makes the receiver and the sender do not have the explicit information of the other party, and the objects in the chain do not themselves know the structure of the chain. The result is that the responsibility chain simplifies the interconnectivity of objects. They only need to maintain a reference pointing to its successor, rather than retaining the reference of all its candidate recipients. This greatly reduces the coupling degree. In other words, I can add or modify the structure of a request at any time. This enhances the flexibility of assigning roles to objects.
Structure:


Basic code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace responsibility chain mode
{
Class Program
{
Static void Main (string [] args)
{
Handler h1 = new ConcreteHandler1 ();
Handler h2 = new ConcreteHandler2 ();
Handler h3 = new ConcreteHandler3 ();
H1.SetSuccessor (h2 );
H2.SetSuccessor (h3 );

Int [] requests = {2, 5, 14, 22, 18, 3, 27, 20 };

Foreach (int request in requests)
{
H1.HandleRequest (request );
}

Console. Read ();

}
}

Abstract class Handler
{
Protected Handler successor;

Public void SetSuccessor (Handler successor)
{
This. successor = successor;
}

Public abstract void HandleRequest (int request );
}

Class ConcreteHandler1: Handler
{
Public override void HandleRequest (int request)
{
If (request> = 0 & request <10)
{
Console. WriteLine ("{0} Processing request {1 }",
This. GetType (). Name, request );
}
Else if (successor! = Null)
{
Successor. HandleRequest (request );
}
}
}

Class ConcreteHandler2: Handler
{
Public override void HandleRequest (int request)
{
If (request >=10 & request <20)
{
Console. WriteLine ("{0} Processing request {1 }",
This. GetType (). Name, request );
}
Else if (successor! = Null)
{
Successor. HandleRequest (request );
}
}
}

Class ConcreteHandler3: Handler
{
Public override void HandleRequest (int request)
{
If (request >=20 & request <30)
{
Console. WriteLine ("{0} Processing request {1 }",
This. GetType (). Name, request );
}
Else if (successor! = Null)
{
Successor. HandleRequest (request );
}
}
}

}

Example:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace responsibility chain mode
{
Class Program
{
Static void Main (string [] args)
{

CommonManager jinli = new CommonManager ("Jin Li ");
Majordomo zongjian = new Majordomo ("zongjian ");
GeneralManager zhongjingli = new GeneralManager ("Zhong jingli ");
Jinli. SetSuperior (zongjian );
Zongjian. SetSuperior (zhongjingli );

Request request = new Request ();
Request. RequestType = "leave ";
Request. RequestContent = "Food leave ";
Request. Number = 1;
Jinli. RequestApplications (request );

Request request2 = new Request ();
Request2.RequestType = "leave ";
Request2.RequestContent = "Food leave ";
Request2.Number = 4;
Jinli. RequestApplications (request2 );

Request request3 = new Request ();
Request3.RequestType = "salary increase ";
Request3.RequestContent = "Food increase request ";
Request3.Number = 500;
Jinli. RequestApplications (request3 );

Request request4 = new Request ();
Request4.RequestType = "salary increase ";
Request4.RequestContent = "Food increase request ";
Request4.Number = 1000;
Jinli. RequestApplications (request4 );

Console. Read ();

}
}

// Manager
Abstract class Manager
{
Protected string name;
// Superior of the Manager
Protected Manager superior;

Public Manager (string name)
{
This. name = name;
}

// Set the manager's superiors
Public void SetSuperior (Manager superior)
{
This. superior = superior;
}

// Request
Abstract public void RequestApplications (Request request );
}

// Manager
Class CommonManager: Manager
{
Public CommonManager (string name)
: Base (name)
{}
Public override void RequestApplications (Request request)
{

If (request. RequestType = "leave" & request. Number <= 2)
{
Console. WriteLine ("{0 }:{ 1} quantity {2} approved", name, request. RequestContent, request. Number );
}
Else
{
If (superior! = Null)
Superior. RequestApplications (request );
}

}
}

// Director
Class Majordomo: Manager
{
Public Majordomo (string name)
: Base (name)
{}
Public override void RequestApplications (Request request)
{

If (request. RequestType = "leave" & request. Number <= 5)
{
Console. WriteLine ("{0 }:{ 1} quantity {2} approved", name, request. RequestContent, request. Number );
}
Else
{
If (superior! = Null)
Superior. RequestApplications (request );
}

}
}

// General Manager
Class GeneralManager: Manager
{
Public GeneralManager (string name)
: Base (name)
{}
Public override void RequestApplications (Request request)
{

If (request. RequestType = "leave ")
{
Console. WriteLine ("{0 }:{ 1} quantity {2} approved", name, request. RequestContent, request. Number );
}
Else if (request. RequestType = "salary increase" & request. Number <= 500)
{
Console. WriteLine ("{0 }:{ 1} quantity {2} approved", name, request. RequestContent, request. Number );
}
Else if (request. RequestType = "salary increase" & request. Number> 500)
{
Console. WriteLine ("{0 }:{ 1} quantity {2}", name, request. RequestContent, request. Number );
}
}
}

// Apply
Class Request
{
// Application Type
Private string requestType;
Public string RequestType
{
Get {return requestType ;}
Set {requestType = value ;}
}

// Application content
Private string requestContent;
Public string RequestContent
{
Get {return requestContent ;}
Set {requestContent = value ;}
}

// Quantity
Private int number;
Public int Number
{
Get {return number ;}
Set {number = value ;}
}
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.