Design mode (12) Responsibility chain mode

Source: Internet
Author: User

First, the drum spread flowers

Drumming is a lively and tense drinking game. In the banquet of guests once seated position, by a person drumming, drumming place and spread the place is separate, to show just. When the drums begin, the bouquet begins to pass, the drums fall, and if the bouquet is in someone's hand, the person will have to drink.

For example, Jiamu, Jia-Pardon, Jia Zheng, Jia Baoyu and Baoyu are five of the flowers to participate in the drum-spreading game, they form a ring chain. The drum player passes the flowers to Jiamu and begins the flower-spreading game. Flowers from Jiamu to Jia pardon, Jia pardon passed to Jia Zheng, Jia Zheng to Jia Baoyu, Jia Baoyu passed to Baoyu, Baoyu again passed to Jiamu, and so on, as shown.

Drumming is a typical chain of responsibility.

Second, what is the responsibility chain model

The responsibility chain pattern is an object's behavior pattern. In the chain of responsibility, many objects are linked together by each object's reference to its next generation. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain is ultimately processing the request, which allows the system to reorganize and assign responsibilities dynamically without affecting the client.

Third, the structure of the responsibility chain model

Abstract processor (Handler) role: Defines an interface for processing requests. If necessary, the interface can define a method to set and return a reference to the home. This role is typically implemented by a Java abstract class or Java interface. The aggregation relationship of the handler class gives a reference to the sub-class, and the abstract Method HandleRequest () regulates the operation of the sub-class processing request.

Specific processor (Concretehandler) role: When a specific processor receives a request, it can choose to dispose of the request or pass the request to the other person. As the specific processor holds references to the homes, the specific processor can access the homes if necessary.

Iv. examples of the responsibility chain model

To see the structure of the chain of responsibility more clearly, let's take a look at the simplest implementation of the code.

 PackageCom.designpattern.pre1;/** * Abstract processor, defines the interface of the processor * * @author 98583 * */ Public Abstract  class Handler {    /** * Next processor * /    protectedHandler successor;/** * Processing method per processor * /     Public Abstract void HandleRequest();/** * Set Next processor * * @param successor */     Public void Setsuccessor(Handler successor) { This. successor = successor; }/** * Get Next processor * * @return  * *     PublicHandlerGetsuccessor() {returnsuccessor; }}
 PackageCom.designpattern.pre1;/** * Specific Processor * * @author 98583 * */ Public  class concretehandler extends Handler {    /** * Processing method * /     Public void HandleRequest() {/** * If the next processor is given to the next processor, the actual situation should be judged whether the handler can handle the problem and cannot process it to the next handler.        if(Getsuccessor ()! =NULL) {System.out.println ("The request is passed"+ getsuccessor ());        Getsuccessor (). HandleRequest (); }//Handling in this processor        Else{System.out.println ("The request is handled here."); }    }}
package com.designpattern.pre1;publicclass Client {    staticprivate Handler handler1, handler2;    publicstaticvoidmain(String[] args) {        /**         * 定义两个处理者         */        new ConcreteHandler();        new ConcreteHandler();        handler1.setSuccessor(handler2);        handler1.handleRequest();    }}

Now that we understand the basic structure of the responsibility chain model, we can realize the story of the beating of the drums in the dream of red mansions above.

 PackageCOM.DESIGNPATTERN.CHAINOFRESP;/** * Equivalent to abstract handler * @author 98583 * */AbstractClass Player {Abstract  Public void Handle(inti);/** * Next processor * /    PrivatePlayer successor; Public Player() {successor =NULL; }/** * Set Next processor * @param asuccessor */    protected void Setsuccessor(Player asuccessor)    {successor = Asuccessor; }/** * passed to the next processor, this method should not have been here, because each processor has this method, so put it in the parent class * @param index * /     Public void Next(intIndex) {if(Successor! =NULL) {Successor.handle (index); }Else{System.out.println ("program terminated."); }    }}
 PackageCOM.DESIGNPATTERN.CHAINOFRESP;/** * Jiamu class equivalent to Concretehandler * @author 98583 * */ class Jiamu extends Player {Public Jiamu (Player asuccessor) { This. Setsuccessor (Asuccessor); }/** * This processing and processing method */public void handle (int i) {if(i = =1) {System.out.println ("Jia Mu gotta drink!"); }Else{System.out.println ("Jia Mu passed!");/** * passed to the next processor * /Next (i); }    }}

Other people's class similar to the Jiamu class, no longer posted, and finally attached to the source code.

 PackageCOM.DESIGNPATTERN.CHAINOFRESP;/** * Drum, equivalent to client * @author 98583 * */ Public  class drumbeater {    Private StaticPlayer player;Static  Public void Main(string[] args) {Jiamu Jiamu =NewJiamu (NULL); Jiamu.setsuccessor (NewJiashe (NewJiazheng (NewJiabaoyu (NewJiahuan (Jiamu))));        Player = Jiamu; Player.handle (4); }}

Five, the responsibility chain model

Advantages:

    • Reduction of coupling
    • Simplifies the connection of objects to each other
    • Increased flexibility in assigning responsibilities to objects
    • It is convenient to add new request processing classes

Disadvantages:

    • There is no guarantee that the request must be received
    • System performance is affected and is inconvenient for code debugging, and may cause cyclic calls

Vi.. Applicable environment

1. There are multiple objects that can handle the same request, and which object handles the request automatically determined by the run time.
2. Submit a request to one of several objects without explicitly specifying the recipient.
3. Can dynamically specify a set of object processing requests

http://download.csdn.net/detail/xingjiarong/9329017

Design mode (12) 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.