Responsibility chain model (behavioral type)

Source: Internet
Author: User

Thinking Question: (Simulation Class tour collective leave), the school stipulates: For the class tour, the student submits the application form, must pass through the counselor, the Department director, the dean, the Headmaster signature confirmation can travel, now by you to design this business model simulates this demand, how should design?

Answer: A very intuitive idea: I directly design a student class, an application for single class Requestnote, a counselor class, a Department director class, a Dean class, a Headmaster class, and then add an audit method for each class verify (Requestnote RN);
Then set the rules:
1. When the counselor approves the Verify method that calls the Dean, the return is no longer called down.
2. The Director of the Department of Audit through the call to the Dean verify method, otherwise no longer use;
3. and so on;
Well, this is already close to the chain of responsibility mode, because it has formed a chain;
Can be optimized, you will find that the above-mentioned counselors, department heads, deans and principals have a common verify method and a reference to the next processing class, should we design an abstract class, add verify abstract methods and a reference to the next processing class in order to standardize and structure clearly, and let subclasses inherit?
But how does the next processing class say? You will find that besides the students, the other person's identity is similar to the leader leader, so design a leader class, the constructor to pass in the next handler object, add a verify method and a leader reference (representing the next processing Class)

The student class's Submitreq method calls a leader (Counselor) Verify method, indicates the student submits the request to the Counselor (constructs the counselor to need first to pass the Department Director Object, constructs the Department director to need to pass in the Dean object, constructs the Dean to pass the principal object);

Instance code:
Leader Abstract class:

package com.shusheng.chain.responsibility;publicabstractclass Leader {    null;//下一个申请表审核者    //审核方法    publicabstractbooleanverify(RequestNote rn);}

Instructor Class:

package  com.shusheng.chain.responsibility; /** Instructor class */ public  class  instructor     Span class= "Hljs-keyword" >extends  leader  { public  instructor  (Leader Leader) {this . Leader = leader;    //Next application reviewer }     @Override  public  boolean  verify  (Requestnote RN) {System.out.println ( "I am a counselor, I audit through!"        I submit the application form to the next processor "");    return  leader.verify (RN); }}

Department Director class:

package  com.shusheng.chain.responsibility; /** Department Director */ public  class  departmenthead  extends  leader         { public  departmenthead  (Leader Leader) { this . Leader = leader;    //Next application reviewer }     @Override  public  boolean  verify  (Requestnote RN) {System.out.println ( "I am the dean of the department and I pass the application.        I submit the application form to the next processor "");    return  leader.verify (RN); }}
package  com.shusheng.chain.responsibility; /** College Dean */ public  class  dean  extends  leader  { public< /span> dean  (Leader Leader) {this . Leader = Leade R    //Next application reviewer }     @Override  public  boolean  verify  (Requestnote RN)        {System.out.println ( "I am the Dean, I agree to the application, I submit the application form to the next processor" );    return  leader.verify (RN); }}
package  Com.shusheng.chain.responsibility;/** principal class */ public  class  president  extends  Span class= "Hljs-title" >leader  { public   President  (Leader Leader) {this . Leader = Leader;//Next application reviewer }  @Override  public   boolean verify  (requestnote rn) {System.out.println (  "I am the headmaster, I approve it!" I submit the application form to the next processor ""); return  true ; }} 
package  com.shusheng.chain.responsibility; /** simulation Request form */ public  class  requestnote  { }
package com.shusheng.chain.responsibility;/**学生把申请表提交给辅导员*/publicclass Student {    /**提交申请表*/    publicbooleansubmitRequestNote(RequestNote rNote,Instructor instructor){        return instructor.verify(rNote);    }}
Package com.shusheng.chain.responsibility; Public classresponsibilitychaintest { Public Static void Main(string[] args) {President president =NewPresident (NULL);//PrincipalDean Dean =NewDean (President);//DeanDepartmenthead Departmenthead =NewDepartmenthead (Dean);//Department headInstructor Instructor =NewInstructor (Departmenthead);//CounselorRequestnote rn =NewRequestnote ();//Application FormStudent Student =NewStudent ();    Student.submitrequestnote (RN, instructor); }}

Directory structure:

Output Result:

Responsibility chain model (behavioral type)

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.