Design Pattern Learning Notes (19)-chain of responsibility duty chain pattern

Source: Internet
Author: User
Tags abstract

Because of my limited level, the written things may be helpful to beginners. If you are not careful which warrior looked not laughed at, where there is not correct place also please criticize correct. Well, don't talk nonsense.

Chain of responsibility Mode definition:

To avoid coupling between the sender and receiver of the request, multiple accept objects have the opportunity to process the request. Connect the objects into a chain and pass the request along the chain until an object handles it.

My understanding:

When more than one object can handle a client request, in order for each object to have the opportunity to process the request, the objects are concatenated in sequence to form a chain, each object being concatenated has a pointer to the next object, and when the request arrives, the first object in order to process the request. This object has two choices: either process or pass the request to the next object (each object has two choices), and so until an object has processed the request, it stops passing on the request once an object has processed the request. Of course, it is possible to the end of the object chain, there is no object to handle the request. I think this is If...else if...else with our usual writing. The functionality to be done is too similar. The above is only a case of chain of responsibility, some books called it the Pure duty chain mode (I can deal with it, can not deal with the other people to deal with), there is another case is not pure duty chain mode (I only deal with the part I can handle, The part that can't be handled is let others deal with it.

The Chain of responsibility model mainly involves two roles:

1 Abstract Processor Role (Handler): It defines an interface for processing requests. Of course, for the different implementations of the chain, you can also implement the successor chain in this role.

2) The specific processor role (concrete Handler): Implements the interface defined in the abstract role and handles the requests it is responsible for. If it cannot be processed, it accesses its successor.

Because the UML of this model is simpler, I don't draw it any more. Let me give you an example: a pure and an impure one. Let's start with a pure one:

Now the girl to find a boyfriend basically have three requirements: have a car, have a room, have a sense of responsibility, if you have none of these three, on the risk. I have no car and no room, but I have a sense of responsibility. ^_^

Class boy{
Private Boolean Hascar; Is there a car
Private Boolean hashouse; Do you have a room
Private Boolean hasresponsibility; Whether there is a sense of responsibility
Public Boy () {
}
Public Boy (Boolean Hascar, Boolean Hashouse, Boolean hasresponsibility) {
This.hascar = Hascar;
This.hashouse = Hashouse;
this.hasresponsibility = hasresponsibility;
}
public Boolean Ishascar () {
return hascar;
}
public void Sethascar (Boolean hascar) {
This.hascar = Hascar;
}
public Boolean ishashouse () {
return hashouse;
}
public void Sethashouse (Boolean hashouse) {
This.hashouse = Hashouse;
}
public Boolean ishasresponsibility () {
return hasresponsibility;
}
public void Sethasresponsibility (Boolean hasresponsibility) {
this.hasresponsibility = hasresponsibility;
}
}
Interface handler{
public void HandleRequest (Boy Boy);
}
Class Carhandler implements handler{//check if there is a car
Private Handler handler;public Carhandler (Handler Handler) {
This.handler = handler;
}

Public Handler GetHandler () {
return handler;
}

public void SetHandler (Handler Handler) {
This.handler = handler;
}

public void HandleRequest (Boy Boy) {
if (Boy.ishascar ()) {
System.out.println ("Oh, I have a car");
}else{
System.out.println ("I don't have a car");
Handler.handlerequest (boy);
}

}
}
Class Househandler implements handler{/check if there is room
Private Handler Handler;

Public Househandler (Handler Handler) {

This.handler = handler;
}

Public Handler GetHandler () {
return handler;
}

public void SetHandler (Handler Handler) {
This.handler = handler;
}

public void HandleRequest (Boy Boy) {
if (Boy.ishashouse ()) {
System.out.println ("I have no idea, I still have a house");
}else{
System.out.println ("I also have no room");
Handler.handlerequest (boy);
}

}
}
Class Responsibilityhandler implements handler{//check for Responsibility
Private Handler Handler;

Public Responsibilityhandler (Handler Handler) {
This.handler = handler;
}

Public Handler GetHandler () {
return handler;
}

public void SetHandler (Handler Handler) {
This.handler = handler;
}

public void HandleRequest (Boy Boy) {
if (boy.ishasresponsibility ()) {
System.out.println ("I have only one heart with responsibility");
}else{
System.out.println ("more irresponsible");
Handler.handlerequest (boy);
}

}
}
Class girl{
public static void Main (string[] args) {
Boy boy=new Boy (false,false,true); This Boy has no car, no room, but a sense of responsibility.
Handler handler=new Carhandler (new Househandler (null));//You can also use the Responsibilityhandler method
Handler.handlerequest (boy);
}
}
In order to make this example, I died a lot of brain cells ...
Here's another impure one:
In order to reduce the number of brain cells killed, this example I do not make up, with a hero on the internet wrote.
This example simulates the process of car assembly: Suppose a car has to go through the following four processes: Assembling the front, the body, the rear, and coloring.
Abstract class Carhandler {
public static final int step_handle_head = 0;
public static final int step_handle_body = 0;
public static final int step_handle_tail = 0;
public static final int step_handle_color = 3;

protected Carhandler Carhandler;

Public Carhandler Setnextcarhandler (Carhandler carhandler) {
This.carhandler = Carhandler;

return this.carhandler;
}

Abstract public void Handlecar (int laststep);
}

Class Carheadhandler extends Carhandler {

@Override
public void Handlecar (int laststep) {
if (Step_handle_head <= laststep) {
System.out.println ("Handle car's head.");
}

if (Carhandler!= null) {
Carhandler.handlecar (Laststep);
}
}
}

Class Carbodyhandler extends Carhandler {

@Override
public void Handlecar (int laststep) {
if (step_handle_body <= laststep) {
System.out.println ("Handle car's body.");
}

if (Carhandler!= null) {
Carhandler.handlecar (Laststep);
}
}
}

Class Cartailhandler extends Carhandler {

@Override
public void Handlecar (int laststep) {
if (Step_handle_tail <= laststep) {
System.out.println ("Handle car ' s tail.");
}

if (Carhandler!= null) {
Carhandler.handlecar (Laststep);
}
}
}

Class Carcolorhandler extends Carhandler {

@Override
public void Handlecar (int laststep) {
if (Step_handle_color = = Laststep) {
System.out.println ("Handle car ' s color.");
}

if (Carhandler!= null) {
Carhandler.handlecar (Laststep);
}
}
}
public class Client {

public static void Main (string[] args) {
Work Flow 1: First assemble the front, then the car body, tail, and finally color
SYSTEM.OUT.PRINTLN ("---workfolow1----");
Carhandler carHandler1 = new Carheadhandler ();
Carhandler1.setnextcarhandler (
New Carbodyhandler ()). Setnextcarhandler (
New Cartailhandler ()). Setnextcarhandler (
New Carcolorhandler ());

Carhandler1.handlecar (Carhandler.step_handle_color);

Workflow 2: For some reason, we need to assemble the rear, then the body, the front, and finally the color.
SYSTEM.OUT.PRINTLN ("---workfolow2---");
Carhandler carHandler2 = new Cartailhandler ();
Carhandler2.setnextcarhandler (
New Carbodyhandler ()). Setnextcarhandler (
New Carheadhandler ()). Setnextcarhandler (
New Carcolorhandler ());

Carhandler2.handlecar (Carhandler.step_handle_color);
}
}

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.