Java design patterns explain the chain of responsibility pattern

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/LZHH_2008/article/details/93967841

Chain of Responsibility pattern is a common pattern of behavior.

A reality scenes chain of responsibility pattern

Xi Wei birthday KTV invite a lot of friends to celebrate together, in order to increase the atmosphere of joy, Xi Wei, I suggest that you play flower drum pass of the game. Drumming by the Xi Wei, in turn pass to spend down, when the drums stop, who spent the hands would have to drink and sing a song. 
We are very enthusiastic to participate in this game, to liven up the party atmosphere soon, Xi Wei also had a very happy birthday party.

In the above scenario, flower drum pass of the game is similar to the design mode of chain of responsibility pattern, the event is to continue to be passed until the event is executed.

II. Definition Chain of Responsibility pattern (Chain of Respinsibility Pattern) of


A plurality of object a chance to handle the request, thereby avoiding coupling between the sender and recipient of the request. These objects strung together a chain, sequentially pass the request along the chain until an object handles it so far.

 

IV. Chain of Responsibility pattern of two characters
1. Abstract handler (Handler) Role
of the abstract character request, to set and define a method and returns a reference to the next handler.

2. Specific handler (Concrte Handler) character
after character to the request, the request may choose to dispose of, or passes the request to the next handler. 
Due to the specific handler to hold the next handler reference, therefore, if necessary, specific treatment can access the next handler, the handler issue to the next, and so on.

V. chain of responsibility pattern of the advantages and disadvantages of
the advantages of the chain of responsibility pattern
1. chain of responsibility pattern request and dealt with separately, the requester does not know who is handling, processing requester can not know the whole picture.
2. The improved flexibility of the system
command mode drawback
1. reduce the performance. Each request is traversed from the end of the chain to the first chain, when faced with a long chain, performance will decline.
2. easy to debug, because the model uses a similar recursive manner, when debugging the logic is more complex.
The number of nodes in the chain of responsibility needs to be controlled to avoid the situation of very long chain, which need to set a maximum number of nodes, more than once is not allowed to add nodes to avoid unintentionally breaking the chain of responsibility of system performance.

VI. Chain of Responsibility pattern usage scenarios
chain of responsibility pattern is a common pattern, Struts2 core control FilterDispatcher a Servlet filter, the control mode is to use the chain of responsibility, the user may request layers of filtration process. Chain of Responsibility pattern used in the actual project is there, its typical application scenario is as follows:

1. A request requires a series of processing.
2. Business process flow, such as document approval.
3. expand the system.
VII. Examples of chain of responsibility pattern
sample code flower drum transfer, losers drink

 

Abstraction class Player

package com.chain.test;

public abstract class Player {

    // specific things people
    private Player dirnk;

    public Player getDirnk() {
        return dirnk;
    }

    // next lot of people passing a particular
    public void setDirnk (Player dirnk) {
        this.dirnk = dirnk;
    }
    
    // spread process
    public abstract void handle (I int);
    
    public void Next (int index) {
        IF ( ! dirnk = null) {
            dirnk.handle (index);
        } the else {
            System.out.println () "Game over!";
        }
    }
}

 

Class specific processing PlayerA

package com.chain.test;

/ **
 * game participant
 * @author PC
 *
 * /
public class PlayerA the extends Player {
    
    // constructor passing next game participant
    public PlayerA (Player Drink) {
        this.setDirnk (Drink);
    }

    //游戏过程
    @Override
    public void handle(int i) {
        
        if(i%5==1) {
            System.out.println("PlayerA drink!");
        }else {
            System.out.println("PlayerA next!");
            next(i);
        }

    }

}

Class specific processing PlayerB

package com.chain.test;

/ **
 * game participant
 * @author PC
 *
 * /
public class Player {the extends PlayerB

    // constructor passing next game participant
    public PlayerB (Player Drink) {
        this.setDirnk (Drink);
    }
    
    // game
    @Override
    public void handle (int I) {
        
        IF (I == 2. 5%) {
            System.out.println ( "PlayerB Drink!");
        } the else {
            System.out.println ( "PlayerB Next!");
            Next (I);
        }

    }

}
 

Class specific processing PlayerC

package com.chain.test;

public class PlayerC extends Player {

    // constructor passing next game participant
    public PlayerC (Player Drink) {
        this.setDirnk (Drink);
    }
    
    // game
    @Override
    public void handle (int I) {
        
        IF (I ==. 3. 5%) {
            System.out.println ( "PlayerC Drink!");
        } the else {
            System.out.println ( "PlayerC Next!");
            Next (I);
        }

    }

}
 

Class specific processing PlayerD

package com.chain.test;

public class PlayerD extends Player {

    // constructor passing next game participant
    public PlayerD (Player Drink) {
        this.setDirnk (Drink);
    }
    
    // game
    @Override
    public void handle (int I) {
        
        IF (I ==. 4. 5%) {
            System.out.println ( "PlayerD Drink!");
        } the else {
            System.out.println ( "PlayerD Next!");
            Next (I);
        }

    }

}
 

Class specific processing PlayerE

package com.chain.test;

public class PlayerE extends Player {

    // constructor passing next game participant
    public PlayerE (Player Drink) {
        this.setDirnk (Drink);
    }
    
    // game
    @Override
    public void handle (int I) {
        
        IF (I == 0. 5%) {
            System.out.println ( "PlayerE Drink!");
        } the else {
            System.out.println ( "PlayerE Next!");
            Next (I);
        }

    }

}
 

Test categories:

package com.chain.test;

/ **
 * Test
 * @author PC
 *
 * /
public class TestChain {

     public static void main(String args[]){
            //创建一个链
            PlayerA playerA = null;
            PlayerE playerE=new PlayerE(playerA);
            PlayerD playerD=new PlayerD(playerE);
            PlayerC playerC=new PlayerC(playerD);
            PlayerB playerB=new PlayerB(playerC);
            //PlayerA playerA=new PlayerA(playerB);
            playerA=new PlayerA(playerB);
            //击鼓下停下来
            playerA.handle(5);
        }
}

operation result:

PlayerA next!
PlayerB next!
PlayerC next!
PlayerD next!
PlayerE drink!

 

But the real-life game of pass the general circulation, which requires the transformation of each class if (i% N == 0) can be realized

Original: https: //blog.csdn.net/wenzhi20102321/article/details/79333899 
 

Guess you like

Origin blog.csdn.net/LZHH_2008/article/details/93967841