Java Reference
In-Depth Information
Forward to default handler - More complex than the base pattern, this approach uses a default event handler.
Any message not explicitly handled at the component level, or forwarded to some other handler, will be sent to
the default handler.
Ignore by default - Any message that is not explicitly handled or forwarded is discarded. If the classes in the
chain produce events that are not used in the application, this can be an acceptable way to reduce chatter.
However, you must be careful in this approach to avoid inadvertently discarding messages that the system should
handle.
Related Patterns
Related patterns include the Composite (page 157). Chain of Responsibility is often used with the Composite
pattern. When both are used together, the Composite pattern provides support for a tree-based structure and basic
message propagation, and the Chain of Responsibility provides rules for how some of the messages are
propagated.
In addition, Composite tends to send messages “down” the tree (from the root to the branches) while Chain of
Responsibility usually sends messages “up” the tree (from branches to the root).
Example
Note:
For a full working example of this code example, with additional supporting classes and/or a RunPattern class,
see “ Chain of Responsibility ” on page 366 of the “ Full Code Examples ” appendix.
The PIM can act as a project manager as well as a contact manager. This code example shows how to use the
Chain of Responsibility pattern to retrieve information from within a project hierarchy.
The ProjectItem interface defines common methods for anything that can be part of a project.
Example 2.1 ProjectItem.java
1. import java.io.Serializable;
2. import java.util.ArrayList;
3. public interface ProjectItem extends Serializable{
4. public static final String EOL_STRING = System.getProperty("line.separator");
5. public ProjectItem getParent();
6. public Contact getOwner();
7. public String getDetails();
8. public ArrayList getProjectItems();
9. }
The interface defines the methods getParent , getOwner , getDetails , and getProjectItems . Two classes
implement ProjectItem in this example — Project and Task . The Project class is the base of a project, so its
getParent method returns null. The getOwner and getDetails methods return the overall owner and details for
the project, and the getProjectItems method returns all of the project's immediate children.
Example 2.2 Project.java
1. import java.util.ArrayList;
2. public class Project implements ProjectItem{
3. private String name;
4. private Contact owner;
5. private String details;
6. private ArrayList projectItems = new ArrayList();
7.
8. public Project(){ }
9. public Project(String newName, String newDetails, Contact newOwner){
10. name = newName;
11. owner = newOwner;
12. details = newDetails;
13. }
14.
15. public String getName(){ return name; }
16. public String getDetails(){ return details; }
17. public Contact getOwner(){ return owner; }
18. public ProjectItem getParent(){ return null; }
Search WWH ::




Custom Search