Design Mode note 13 (responsibility chain mode)

Source: Internet
Author: User

Responsibility Chain Model

The responsibility chain mode allows multiple objects to process requests in order according to their responsibilities, so that multiple objects have the opportunity to process requests according to their responsibilities.

It is not easy to enter our lab. We need to go through layer-by-layer interviews, and finally let the boss decide whether to hire. In the past, this practice was very good, because everyone did this, but the lab has been expanding a lot recently, and the personnel department is hiring people, so the boss is too busy.

Consultant Lin Abao proposed an improvement plan to allow the Personnel Department staff to renew for the first round, and the personnel department staff can decide whether to hire the interviewer with a salary of less than 3000; the personnel manager will interview for the second round, in addition, it can determine whether or not to hire an interviewer with a salary of less than 6000; the boss will interview the third round and decide whether to hire an interviewer with a salary of more than 6000:

Import Java. util. arraylist; Class interviewee {public int salary; Public String role; Public String gender; Public string name; Public interviewee (string name, string role, string gender, int salary) {This. name = Name; this. role = role; this. gender = gender; this. salary = salary;} abstract class interviewer {interviewer next; Abstract Public void interview (interviewee); Public void s Etnext (interviewer) {next = Interviewer;} public void movetonext (interviewee) {If (next! = NULL) {next. interview (interviewee) ;}} class renshiyuangong extends interviewer {@ override public void interview (interviewee) {If (interviewee. role = "secretary") {// although not said, The Secretary must be an IF (interviewee. gender = "male") {// The boss is not good at this system. out. println ("employee said:" + interviewee. name + "Sorry, we are not looking for a male secretary");} else {movetonext (interviewee) ;}} else {If (interviewee. salary <= 3000) {If (interviewee. gender = "") {// as long as it is a female, it is a good system. out. println ("employee said:" + interviewee. name + "you are very suitable for this position, welcome to join us");} else {If (interviewee. role = "sewer workers") {// men can only do physical work. Do you want to hire a rich handsome guy to grab beautiful resources? system. out. println ("employee said:" + interviewee. name + "you are very suitable for this position, welcome to join us");} else {system. out. println ("employee said:" + interviewee. name + "go back, we basically do not provide positions for male") ;}} else {movetonext (interviewee );}}}} class renshijingli extends interviewer {@ override public void interview (interviewee) {If (interviewee. role = "secretary") {// although not said, The Secretary must be an IF (interviewee. gender = "male") {// The boss is not good at this system. out. println ("the personnel manager said:" + interviewee. name + "Sorry, we are not looking for a male secretary");} else {movetonext (interviewee) ;}} else {If (interviewee. salary <= 6000) {If (interviewee. gender = "") {// as long as it is a female, it is a good system. out. println ("the personnel manager said:" + interviewee. name + "you are very suitable for this position, welcome to join us");} else {If (interviewee. role = "sewer workers") {// men can only do physical work. Do you want to hire a rich handsome guy to grab beautiful resources? system. out. println ("the personnel manager said:" + interviewee. name + "you are very suitable for this position, welcome to join us");} else {system. out. println ("the personnel manager said:" + interviewee. name + "go back, we basically do not provide positions for male") ;}} else {movetonext (interviewee );}}}} class laozong extends interviewer {@ override public void interview (interviewee) {If (interviewee. role = "secretary") {system. out. println ("said the boss:" + interviewee. name + "sorry, you are not my dish");} else {If (interviewee. gender = "") {// as long as it is a female, it is a good system. out. println ("said the boss:" + interviewee. name + "you are very suitable for this position, welcome to join us");} else {If (interviewee. role = "sewer workers") {// men can only do physical work. Do you want to hire a rich handsome guy to grab beautiful resources? system. out. println ("said the boss:" + interviewee. name + "you are very suitable for this position, welcome to join us");} else {system. out. println ("said the boss:" + interviewee. name + "go back, we basically do not provide positions for male") ;}}} public class chainofresponsibility {public static void main (string [] ARGs) {// set responsibility chain, from personnel staff to Personnel Manager to boss interviewer laozong = new laozong (); interviewer renshiyuangong = new renshiyuangong (); interviewer renshijingli = new renshijingli (); renshiyuangong. setnext (renshijingli); renshijingli. setnext (laozong); arraylist <interviewee> interviewees = new arraylist <interviewee> (); interviewees. add (New interviewee ("Zhang San", "", "male", 9000); interviewees. add (New interviewee ("Li Si", "Sewer worker", "male", 2000); interviewees. add (New interviewee ("Wang Wu", "minister", "male", 7000); interviewees. add (New interviewee ("Zhao Liu", "consultant", "female", 5000); interviewees. add (New interviewee ("Ma Qi", "", "male", 6000); interviewees. add (New interviewee ("small dollar", "clerk", "female", 6000); interviewees. add (New interviewee ("tall tree Mary", "secretary", "female", 29999); interviewees. add (New interviewee ("Kato", "secretary", "male", 2999); For (interviewee Interviewee: interviewees) {renshiyuangong. interview (interviewee );}}}

The above is an example of the responsibility chain model. A chain structure consists of units. When a single unit cannot respond to a request, the request is sent to the next node in the chain structure, and the node can only be passed to the next node, because it only holds references for the next node.

This chain structure is similar to the storage method of the linked list. In addition to its own information, each node also stores the reference of the next node.

 

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.