Design Pattern _ responsibility chain pattern

Source: Internet
Author: User

Design Pattern _ responsibility chain pattern

DefinitionAvoid coupling the sender of a request to its own er by giving more than one object a chance to handle the request. chain the processing objects and pass the request along the chain until an object handles it. (so that multiple objects have the opportunity to process the request, thus avoiding the coupling relationship between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it)
Unfamiliar WordsCoupling: coupling
Sender: sender

The focus of the responsibility chain model is that on the "chain", a chain is used to process similar requests. In the chain, the chain determines who will process the request and returns the corresponding results, the generic class diagram is shown in Figure 16-4.

 

The core of the responsibility chain model is "chain!
ExampleIn ancient China, "three slaves and Four Virtues" were set up for women. "Three slaves" refer to "not married from the father, married from the husband, and dead from the child ". That is to say, a female needs to obey her father before getting married and her husband after getting married. If her husband dies, she must obey her son. For example, if a female wants to go out shopping, she must obtain her father's consent before going out to marry and get her husband's permission after going out to marry, what should she do if she dies? Ask your son if he is allowed to go shopping.

 

Core things★

 

Public abstract class Handler {public final static int FATHER = 1; public final static int HUSBAND = 2; public final static int SON = 3; private int level = 0; private Handler nextHandler; // this is the key public Handler (int _ level) {this. level = _ level;} public final void HandlerMessage (IWomen women) {if (women. getType () = this. level) {this. response (women);} else {if (this. nextHandler! = Null) {this. nextHandler. handlerMessage (women);} else {System. out. println (not followed, but disagree); }}public void setNext (Handler _ nextHandler) {this. nextHandler = _ nextHandler;} protected abstract void response (IWomen women );}

Handler Class AnalysisAbstract class, which contains three static constants, one current level variable and one member. Let me say this, so that we can call it recursively and concatenate the Handler subclass to form a chain. The following is a repair and makeup task, which is carried out in full accordance with the above constraints.

 

Three subclasses

 

 

Public class Father extends Handler {public Father () {super (Handler. FATHER) ;}@ Override protected void response (IWomen women) {System. out. println (the daughter asked her father: + women. getRequest (); System. out. println (promised by the father );}}
Public class Husband extends Handler {public Husband () {super (Handler. HUSBAND) ;}@ Override protected void response (IWomen women) {System. out. println (wife's request: + women. getRequest (); System. out. println (permitted by the husband );}}

Public class Son extends Handler {public Son () {super (Handler. SON);} @ Override protected void response (IWomen women) {System. out. println (the mother's request is + women. getRequest (); System. out. println (son allowed );}}

Design female interfaces and Instances

 

 

public interface IWomen { public int getType(); public String getRequest();}
Public class Women implements IWomen {/* 0: Not married * 1: Married * 2: widowed */private int type = 0; private String request =; public Women (int type, string request) {this. type = type; this. request = request;} public String getRequest () {return this. request;} public int getType () {return this. type ;}}

Test

 

 

Public class Client {public static void main (String [] args) {Random random = new Random (); ArrayList
 
  
List = new ArrayList
  
   
(); For (int I = 0; I <5; I ++) {list. add (new Women (random. nextInt (4), I want to go shopping); // generate a random type} Handler father = new Father (); Handler husband = new Husband (); handler son = new Son (); father. setNext (husband); // sets the next husband. setNext (son); // set the next for (IWomen women: list) {father. handlerMessage (women); // Of course, starting from the highest }}/ *** the chain is important, A chain is used to process similar requests. The chain determines who will process the request */
  
 

 

Wu

The setNext () method is the biggest feature of this design pattern. It can be passed in the future to implement recursive loops. If this is not the case, traverse the next chained structure. Chain, similar to if-else. Obviously, this design is more advanced and neat. If you want to talk about scalability, it's okay. After all, a system cannot have a long chain of responsibility. It can be used together with other models. The key is the scenario and requirement.

 


 

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.