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)