Java design Mode _ (Behavioral) _ Responsibility Chain mode __java

Source: Internet
Author: User

Quote Encyclopedia

Responsibility chain mode is a design pattern. In the mode of responsibility chain, many objects are connected by each object's reference to their house to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client who issued the request does not know which object on the chain ultimately handles the request, which allows the system to dynamically rearrange and distribute responsibilities without affecting the client.


Responsibility chain pattern involves the role:
Abstract processor (Handler) role: Defines an interface for processing requests. If necessary, the interface can define a method to set and return references to the home. This role is typically implemented by a Java abstract class or Java interface. Abstract method HandleRequest () regulates the operations that subclasses handle requests.
Specific processor (Realhandler) role: When a specific processor receives a request, he or she can choose to dispose of the request or pass the request to the other person. Because the specific processor holds the reference to the homes, therefore, if necessary, the specific processor can visit the homes.



Case:

such as: have a request for leave, according to different days of absence for different approvals

<=2 days, only project manager approval REALHANDLERC

>2,<=5 days, need to go to the department manager for approval Realhandlerb

>5 days, need general manager approval Realhandlera


As follows:



The specific code implementation is as follows:


1, abstract processing class role

Abstract processor role class public
abstract class Handler {

	/**
	 * @Description: Define processing Method
	 * @param date	days
	 off * * Public
	abstract void Requesthand (int date);

	Filter Object
	private Handler resource;

	Public Handler GetResource () {return
		resource;
	}

	public void Setresource (Handler Resource) {
		this.resource = resource;
	}

}


2, the specific implementation of the class

Specific processor role C (Project manager) public class Realhandlerc extends Handler {@Override public void Requesthand (int date) {System.out
		. println ("C conditional filtration ...");
		if (date <= 2) {System.out.println ("C Processing request!");
				else {if (This.getresource ()!= null) {System.out.println ("Days + Date +", C to next-level processing! ");
			This.getresource (). Requesthand (date);
		}}//Specific processor role B (Department manager) public class Realhandlerb extends Handler {@Override public void Requesthand (int date) {
		SYSTEM.OUT.PRINTLN ("B condition filter ...");
		if (date <= 5) {System.out.println ("B Processing request!");
				else {if (This.getresource ()!= null) {System.out.println ("Days + date +", B to the next level of processing! ");
			This.getresource (). Requesthand (date);
		}}//Specific processor role A (general manager) public class Realhandlera extends Handler {@Override public void Requesthand (int date) {
		SYSTEM.OUT.PRINTLN ("A condition filter ...");
		if (Date > 5) {System.out.println ("a process request!"); } else {if (This.getresource ()!= null) {System.out.println ("days are" + Date +", a to the next level of treatment!");
			This.getresource (). Requesthand (date); }
		}
	}
}



3. client-side test

public class Client {

	private static void vacation () {
		//assembly responsibility chain
		Handler Chaina = new Realhandlera ();
		Handler chainb = new Realhandlerb ();
		Handler chainc = new Realhandlerc ();

		Chainc => chainb =>chaina
		chainc.setresource (CHAINB);
		Chainb.setresource (Chaina);

		Test, simulate the days of absence for approval
		Chainc.requesthand (1);
		System.out.println ("------------Split Line------------");
		Chainc.requesthand (4);
		System.out.println ("------------Split Line------------");
		Chainc.requesthand (6);
	}

	public static void Main (string[] args) {
		vacation ();
	}
}

The above with the absence case simple simulation of the responsibility chain mode, output:

C Condition Filter ...
C Processing Request!
------------Split Line------------
C Condition Filter ...
Days for 4,c to the next level of processing!
B Condition Filter ...
b Processing Request!
------------Split Line------------
C Condition Filter ...
Days for 6,c to the next level of processing!
B Condition Filter ...
Days for 6,b to the next level of processing!
A Condition Filter ...
A processing request!



Use instructions: 1, so that multiple objects have the opportunity to process the request, so as to avoid the sender and the recipient of the coupling relationship between the request, 2, the object into a chain, and along this chain pass the request until an object to deal with him.




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.