On the responsibility chain model of five design principles in Java

Source: Internet
Author: User

First we need to define a chain of responsibility model:

The chain of responsibility pattern is a linear execution process where multiple objects have the opportunity to perform the same task, but in the process of execution,

Because the rights and scope of execution are different, you must throw this task to the next object when you cannot handle the task.

An analogy, a chain of execution is formed.

A typical example is a filter in Tomcat that, when configured with multiple filters, forms a filter chain that the client initiates

Request passes through each filter on the filter chain, and eventually only one filter is processed to handle the request.

Pure and impure chains of responsibility:

1. The pure chain of responsibility is the entire chain, and only one object can handle the task. Will not be processed halfway, and then transferred to another

Object.

2. In the entire chain of responsibility, an object must be able to handle the current task. Impossible to appear a task without an object to go

Handling of the situation.

In the actual development, the majority will appear the impure responsibility chain. In fact, this is a change in the chain of responsibility model, is to cater for business

Change.

Define an interface first:

Package org.demo2;/** * Created by Administrator on 2016/11/23. *///defines an interface public interface Abstracthandler {    //interface method public    String requesthandler (int money, Handlerchain Chain);}

Write two classes to implement this interface

Package org.demo2;/** * Created by Administrator on 2016/11/23. *///handler, implemented the Abstracthandlerpublic class Quadhandler implements Abstracthandler {    //rewrite interface method    @Override Public    String RequestHandler (int money, Handlerchain chain) {        //Determine if the condition is eligible        if (Money <=) {            return] The class committee passed the audit ";        }        If not, the task is transferred to the next object        return Chain.execute (Money);}    }
Package org.demo2;/** * Created by Administrator on 2016/11/23. *///Teacher handler, implemented Abstracthandlerpublic class Teacherhandler implements Abstracthandler {    //Override interface Method    @ Override public    String requesthandler (int money, Handlerchain chain) {        //Determine if the condition is eligible        if (Money <=) {
   
    return "The head teacher passes the audit";        }        If not, the task is transferred to the next object        return Chain.execute (Money);}    }
   

Write a class for the execution chain:

Package Org.demo2;import java.util.arraylist;import Java.util.iterator;import java.util.list;/** * Created by Administrator on 2016/11/23. * Execution Chain Manager * Put all the objects on the chain into this class to manage */public class Handlerchain {    //This collection holds all the performers    private list<abstracthandler> handlers = new arraylist<> ();    Declares an iterator used to traverse the list collection    private iterator<abstracthandler> Iterator;    When instantiating the object, add Quahandler and Teacherhandler to the collection public    Handlerchain () {        //Add the object to the list collection        Handlers.add (new Quadhandler ());        Handlers.add (New Teacherhandler ());        Returns an iterator        iterator = Handlers.iterator ();    }    Iterate through the collection at execution time, calling each performer public    String execute (int money) {        //traversing the list collection        if (Iterator.hasnext ()) {            // Returns the next handler object            return Iterator.next (). RequestHandler (Money, this);        }        If not present, returns null return        null;}    }

Finally write a Main method:

Package org.demo2;/** * Created by Administrator on 2016/11/23. */public class Main {public    static void Main (string[] args) {        //Instantiation Handlerchain object        handlerchain chain = new Ha Ndlerchain ();        Executes the Execute method, returning a string of        msg = Chain.execute (+);        SYSTEM.OUT.PRINTLN (msg);}    }

Run:

If you modify the parameters:

Package org.demo2;/** * Created by Administrator on 2016/11/23. */public class Main {public    static void Main (string[] args) {        //Instantiation Handlerchain object        handlerchain chain = new Ha Ndlerchain ();        Executes the Execute method, returning a string
Modifies the parameter to a String msg = Chain.execute; SYSTEM.OUT.PRINTLN (msg);} }

Run:

------------------------------------------------

On the responsibility chain model of five design principles in Java

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.