Demand
To achieve a company in the absence of salary increases and other process approval procedures.
Realize
Level 1
public class Request {private string requesttype;private int number;public string Getrequesttype () {return requesttype;} public void Setrequesttype (String requesttype) {this.requesttype=requesttype;} public int GetNumber () {return number;} public void Setnumber (int number) {This.number=number;}}
public Class Manager {protected string Name;public Manager (string name) {This.name=name;} public void GetResult (String managerlevel,request Request) {if (managerlevel== "manager") {if (Request.getrequesttype () = = "Leave" &&request.getnumber () <=2) {System.out.println ("approval!");} else {System.out.println ("permission denied!");}} else if (managerlevel== "director") {if (request.getrequesttype () = = "Leave" &&request.getnumber () <=5) { System.out.println ("approval!");} else {System.out.println ("permission denied!");}} else if (managerlevel== "Generalmanager") {if (request.getrequesttype () = = "Leave") {System.out.println ("approval!");} if (request.getrequesttype () = = "Salary Increase" &&request.getnumber () <=500) {System.out.println (" Approval! ");} else if (request.getrequesttype () = = "Salary Increase" &&request.getnumber () >500) {System.out.println (" Refuse! ");}}}
public class main{public static void Main (string[] args) { Manager manager1=new Manager ("Bob"); Manager director1=new Manager ("Alice"); Manager generalmanager1=new Manager ("Jennifer"); Request Request=new request (); Request.setrequesttype ("salary increase"); Request.setnumber ($); Manager1. GetResult ("manager", request); Director1. GetResult ("director", request); Generalmanager1. GetResult ("Generalmanager", request); } }
The method is very long and has a lot of branches to judge, is very bad design.
Level 2
public class Request {private string requesttype;private int number;public string Getrequesttype () {return requesttype;} public void Setrequesttype (String requesttype) {this.requesttype=requesttype;} public int GetNumber () {return number;} public void Setnumber (int number) {This.number=number;}}
Public abstract class Manager {protected string name;protected Manager superior;public Manager (string name) {This.name= Name;} public void Setsuperior (Manager superior) {This.superior=superior;} Abstract public void Requestapplications (Request request);}
public class Commonmanager extends Manager{public Commonmanager (String name) {super (name);} public void Requestapplications (Request request) {if (request.getrequesttype () = = "Leave" &&request.getnumber ( ) {<=2) {System.out.println ("approval!");} else {if (superior!=null) {Superior. Requestapplications (Request);}}}
Public class Director extends Manager{public director (String name) {super (name);} public void Requestapplications (Request request) {if (request.getrequesttype () = = "Leave" &&request.getnumber ( ) {<=5) {System.out.println ("approval!");} else {if (superior!=null) {Superior. Requestapplications (Request);}}}
public Class Generalmanager extends Manager{public Generalmanager (String name) {super (name);} public void Requestapplications (Request request) {if (request.getrequesttype () = = "Leave") {System.out.println (" Approval! ");} if (request.getrequesttype () = = "Salary Increase" &&request.getnumber () <=500) {System.out.println (" Approval! ");} else if (request.getrequesttype () = = "Salary Increase" &&request.getnumber () >500) {System.out.println (" Refuse! ");}}}
public class main{public static void Main (string[] args) { Commonmanager commonmanager1=new Commonmanager ("Bob"); Director Director1=new Director ("Alice"); Generalmanager generalmanager1=new Generalmanager ("Jennifer"); Commonmanager1.setsuperior (Director1); Director1.setsuperior (generalmanager1); Request Request=new request (); Request.setrequesttype ("salary increase"); Request.setnumber ($); Commonmanager1. Requestapplications (request); } }
The responsibility chain pattern allows multiple objects to have the opportunity to process the request, thereby avoiding the coupling between the sender and receiver of the request, linking the object to a chain, and passing the request along the chain, knowing that an object is handling it. The responsibility chain model reduces coupling, simplifies the connection of objects, enhances the flexibility of assigning responsibilities to objects, and facilitates the addition of new classes. However, sometimes the request arrives at the end and may not be processed, and the code is not easy to debug, which may cause a circular call. If there are multiple objects that can handle the same request, which object handles the request is determined automatically by the run time, a request is submitted to one of the multiple objects without explicitly specifying the recipient, and we can use the responsibility chain mode when you can dynamically specify a set of object processing requests. The exception handling mechanism in Java and the early AWT event model both employ a chain of responsibility model, and familiarity with these two parts should soon be able to think of why it should be applied.
Liar design mode-responsibility chain mode