The responsibility chain model of the big talk Design Model

Source: Internet
Author: User

What is the responsibility chain model? China of Responsibility: Enables multiple objects to process requests to avoid coupling between request senders and recipients. Connect the object to a chain and pass the request along the chain until an object processes it. This situation can be seen everywhere around us. For example, if you want to apply for a classroom to hold a New Year's Day Gala in the class, the literary and art committee members cannot claim to use a classroom, so they can find the class leader. What the class leader has to do is write an application, apply to the person in charge, and the person in charge can use the classroom only after the person in charge approves the application. This step-by-step process is like a chain, which starts from a certain point. If it fails, it will go backwards, if you find something that can solve the problem, it will be terminated. As long as the problem is solved, the goal is achieved. You do not need to consider the number of people involved in the process, that is, the motivation of the students in this class can be expressed as the responsibility chain model structure: Role: Client: client Handler: Abstract Handler: defines an interface for processing requests. If necessary, the interface can define a method to set and return references to the next house. This role is usually implemented by an abstract class or interface www.2cto. comConcreteHandler: The specific handler: after receiving the request, the specific handler can choose to process the request or send the request to the next house. Because the specific Handler holds a reference to the next house, if you need a specific handler, you can access the code implementation of the next house: [html] <span style = "font-family: KaiTi_GB2312; font-size: 18px; "> using System; using System. collections. generic; using System. linq; using System. text; namespace responsibility chain mode {class Program {static void Main (string [] args) {// set the responsibility chain home and next Handler h1 = new ConcreteHandler1 (); handler h2 = new ConcreteHandler2 (); Handler h3 = new ConcreteHandler3 (); h1.SetSuccessor (h2); h 2. setSuccessor (h3); int [] requests = {2, 5, 14, 22, 18, 3, 27, 20}; foreach (int request in requests) {// the minimum number of submitted requests processed in a loop. Different data handlers process h1.HandleRequest (request);} Console. read () ;}// Handler class, which defines an interface for Request Handling abstract class Handler {protected Handler successor; // sets the successor public void SetSuccessor (Handler successor) {this. successor = successor;} // request abstraction method public abstract void HandleRequest (int re Quest);} // ConcreteHandler class, the specific handler class, which processes the request it is responsible for, and can access its successor. If it can process the request, it will process it, otherwise, the request will be forwarded to its successor // ConcreteHandler1 class. When the number of requests is between 0 and 10, the request has the right to process it; otherwise, the request will be transferred to the next 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) {// transfer to the next successor. handleRequest (request) ;}}// ConcreteHandler2 class. If the number of requests is between 10 and 20, you have the right to process the requests. Otherwise, you can go to the next 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) ;}}// ConcreteHandler3 class. If the number of requests is between 20 and 30, you have the right to process the requests. Otherwise, you can transfer the requests to the next 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) ;}}</span> running result:

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.