Responsibility chain of design patterns

Source: Internet
Author: User

Responsibility chain of design patterns

Chain of Responsibility)
So that multiple objects have the opportunity to process requests, so as 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.


Leave conditions:


1. Top Professional Courses
If the courses in the professional and advanced courses conflict with each other (including the examination), you can ask for leave directly with the respective disciplinary committee director. (The person in charge of the Commission for Discipline Inspection checks attendance every day)
2. Bai shanxiao takes precedence
If your parents are ill and their relatives need to go home, you can ask for leave from Mr Mi and inform the Commission for Discipline Inspection. If your parents are not ill, you need to take care of and take care of the food for more than 10 days. You can ask for leave from Mr Mi, inform the Commission for Discipline Inspection.
3. The body is the revolutionary capital
If you are ill, you can ask for leave. If you are ill, you can get approval from the respective Commission for Discipline Inspection directors within half a day. If you are ill for more than half a day, you should ask the teacher for leave and inform the Commission for Discipline Inspection.
4. Urgent leave
Within the prescribed scope (within three hours), you can directly ask for leave from the respective disciplinary committee directors. If you have more than three hours or within the prescribed scope, you need to ask for leave from the teacher and then greet the respective disciplinary committee administrators. Others are determined by the Commission's Director. If the person in charge of the Commission's discipline inspection cannot decide, submit the decision to the teacher.


The above are the regulations on asking for leave for higher education. With these regulations, we will know who to ask for leave if one day we need to ask for leave and whether the conditions for asking for leave are met! Next let's look at the code implementation.
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chain_of_Responsibility {class Program {// student interface public interface IStudent {// student leave level: within 0-3 hours (handled by the Commission for Discipline Inspection), level 1: more than 3 hours (handled by the instructor) int getLevel (); // obtain the Student leave message string getRequestMessage ();} // Student implementation class public class Student: IStudent {// leave level private int level =-1; // leave message private string message; pu Blic Student (int level, String message) {this. level = level; this. message = message;} public int getLevel () {return this. level;} public string getRequestMessage () {return this. message ;}/// abstract handler interface public interface IHandler {// handle the request void handleRequest (IStudent student); // set the next processing void setHandler (IHandler handler );} // abstract handler public abstract class implements acthandler: IHandler {// The next handler private IH Andler handler; // leave level private int level =-1; // set level public encryption acthandler (int level) {this. level = level;} // process the request. The sub-class is responsible for processing the public abstract void process (IStudent student); public void handleRequest (IStudent student) {if (student! = Null) {if (this. level = student. getLevel () {// if the leave level is the same as the current level, the current object will process this. process (student);} else {if (this. handler! = Null) {Console. WriteLine (student. getRequestMessage () + "handle the request! "); // If the current object cannot be processed, it will be handed over to the next handler for processing this. handler. handleRequest (student) ;}}// set the next handler public void setHandler (IHandler handler) {this. handler = handler ;}}// Commission for Discipline Inspection public class DisciplineHandler: AbstractHandler {public DisciplineHandler (): base (0) {} public override void process (IStudent student) {Console. writeLine ("Commission for Discipline Inspection approval:" + student. getRequestMessage (); }}// instructor public class TeacherHandler: AB StractHandler {public TeacherHandler (): base (1) {} public override void process (IStudent student) {Console. writeLine ("approved by the Instructor:" + student. getRequestMessage () ;}}// set the responsibility chain to associate public class ProcessHandler {private IHandler disciplineHandler; private IHandler teacherHandler; // Singleton mode private static ProcessHandler processHandler = new ProcessHandler (); private ProcessHandler () {this. disciplineHandler = New DisciplineHandler (); this. teacherHandler = new TeacherHandler (); this. disciplineHandler. setHandler (this. teacherHandler);} public static ProcessHandler getInstance () {return processHandler;} // send the leave request public void sendMessage (IStudent student) {this. disciplineHandler. handleRequest (student) ;}} static void Main (string [] args) {ProcessHandler processHandler = ProcessHandler. getInstance (); IStudent student0 = new Student (0, "Student Zhang XX arrived. Leave for 20 minutes! "); IStudent student1 = new Student (1," Student Li XX broke his leg during the exercise and asked for a week off! "); Console. writeLine ("************************************* ***********************"); processHandler. sendMessage (student0); Console. writeLine ("************************************* ***********************"); processHandler. sendMessage (student1); Console. writeLine ("************************************* ***********************");}}}





Class diagram:

System coupling.
2. The request processing object only needs to maintain a reference pointing to its successor, which can simplify object interconnection.
3. When assigning responsibilities to objects, the responsibility chain can give us more flexibility.



Main disadvantages:
1. A request may not be processed because the responsibility chain is not correctly configured.
2. For a long responsibility chain, request processing may involve multiple processing objects, and the system performance will be affected.



Applicable scenarios:
1. There are multiple objects that process the same request. It is not clear which of the objects to process. Only the objects to be processed can be determined at runtime.
2. A message has multiple receivers, and the recipient is ambiguous. You only need to send a message to one of its objects for internal processing.
3. Multiple processing objects of the same message may be dynamically increased or reduced, which must be specified dynamically.




Related mode:
1. Appearance: The appearance may be used when setting the responsibility chain, so that the client can only deal with the appearance.
2. Command: The request transmission behavior is designed, and the request transmission uses the command mode.
3. Observer: when an event is triggered, messages are sent. Multiple message recipients can exist at the same time.
4. In the responsibility chain, a series of messages are processed according to the established rules, and only one receiver receives messages for transmission.




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.