Liar design mode-responsibility chain mode

Source: Internet
Author: User
Tags class manager

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

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.