_5_ responsibility chain model for Java implementation of 23 classical design patterns

Source: Internet
Author: User

This article turns from: http://blog.csdn.net/kthq/article/details/1713720

For a chain of responsibility pattern, this is generally defined by making multiple objects have the opportunity to process the request, thus avoiding the coupling between the sender and receiver of the request, that is, connecting the objects to a (process) chain and passing the request along this chain, Until the end of the chain there is an object to process it.

The most classical application of the duty chain pattern is the Web.xml filter in the Java EE specification, which in turn filters the Web request (response)/response (request) filtered according to its mapping conditions, enabling you to implement such code conversion, Transaction encapsulation, page caching , and many other "responsibilities."

The application scope of the responsibility chain model is given:

1, there are multiple objects can handle a request, which object processing the request is automatically determined when the runtime.

2. To submit a request to one of multiple objects without specifying the recipient explicitly.

3. A collection of objects that can handle a request needs to be dynamically specified.

The following example simulates a common project processing process in a software industry, constructs a process chain for the responsibility of one project (Project object), and then issues a processing instruction to the project--thereby triggering the handling of the responsible person on the chain. The code is as follows:

The interface definition of responsibility, all duty personnel must have these functions: Join next Responsibility object, process message (that is, exercise responsibility), get next responsibility object. Package com.alex.designpattern.chainofresponsibility;

/**
* The interface of the chain can use Addchain function to modify the chain
* Dynamically.
*
* @author <a href= "mailto:huqiyes@gmail.com" >huqi</a>
* @serialData 2007
*/
Public interface Chain {
public void Addchain (Chain c);

public void Sendtochain (String mesg);

Public Chain getchain ();
}

The following are the implementations of all the responsibilities in our process, followed by managers, project managers, programmers, testers, and others, which handle their own messages based on MESG parameters-that is, to exerciseSpecific Responsibilities。 Package com.alex.designpattern.chainofresponsibility;

/**
* A Beginner of the chain the resposibility of manager is to get A project
*/
public class Manager implements Chain {
Private Chain nextchain = null;

Private static final String responsibility = "getting Project";

public void Addchain (Chain c) {
Nextchain = C;
}

Public Chain GetChain () {
return nextchain;
}

public void Sendtochain (String mesg) {
if (mesg.equals (responsibility)) {
System.out.println ("A Manager-->" + MESG);
} else {
if (Nextchain!= null) {
Nextchain.sendtochain (MESG);
}
}
}

}

Package com.alex.designpattern.chainofresponsibility;

/**
* A member of the chain the resposibility of PM are to design the project
*/
public class Projectmanager implements Chain {
Private Chain nextchain = null;

Private static final String responsibility = "Design";

public void Addchain (Chain c) {
Nextchain = C;
}

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.