Chain of Responsibility mode in Behavioral mode

Source: Internet
Author: User

Chain of Responsibility mode in Behavioral mode
1. Intention

So that multiple objects have the opportunity to process the request, so as to avoid the coupling relationship between the request sender and the receiver, connect these objects into a chain, and pass the modification request along this chain, until an object is processed.

2. Alias

None

3. Motivation

Consider the context-related Help Mechanism in a graphical user interface. You can click on any part of the interface to get help information. The help provided depends on the part of the interface and its context.

4. Applicability

The Responsibility mode is used in the following cases:

Multiple objects can process one request, and the object that processes the request is automatically determined at runtime. You want to submit a request to one of multiple objects without specifying the receiver explicitly. The object set that can process a request should be dynamically specified. 5. Structure

Next, we will talk about the responsibility chain model. There are multiple objects, each of which holds a reference to the next object. In this way, a chain is formed and requests are transmitted on this chain, until an object decides to process the request. However, the sender does not know whether the final object will process the request. Therefore, the responsible chain mode can be implemented to dynamically adjust the system when the client is concealed. First look at the relationship diagram:

The Abstracthandler class provides get and set methods to facilitate the MyHandle class to set and modify reference objects. The MyHandle class is the core. after instantiation, a series of mutually held objects are generated to form a chain. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoMiBpZD0 = "6 sample code"> 6. Sample Code

Implementation Code:
One interface:

public interface Handler {    public void operator();}

Implementation:

public abstract class AbstractHandler {    private Handler handler;    public Handler getHandler() {        return handler;    }    public void setHandler(Handler handler) {        this.handler = handler;    }}public class MyHandler extends AbstractHandler implements Handler {    private String name;    public MyHandler(String name) {        this.name = name;    }    @Override    public void operator() {        System.out.println(name+deal!);        if(getHandler()!=null){            getHandler().operator();        }    }}
Test class:

Public class Test {

public static void main(String[] args) {    MyHandler h1 = new MyHandler(h1);    MyHandler h2 = new MyHandler(h2);    MyHandler h3 = new MyHandler(h3);    h1.setHandler(h2);    h2.setHandler(h3);    h1.operator();}

}
''
Output:
H1deal!
H2deal!
H3deal!
Here, we emphasize that the request on the link can be a chain, a tree, or a ring, and the pattern itself does not constrain this. We need to implement it ourselves. At the same time, at one time point, a command can only pass one object to another, rather than multiple objects.

7. Related ModesThe responsibility chain is often used with Composite. In this case, the parent component of a component can be used as its successor.

 

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.