jtchainofresponsibility.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 121 行
JAVA
121 行
package Jt;
import Jt.examples.HelloWorld;
/**
* Jt Implementation of the Chain of Responsibility pattern.
*/
public class JtChainOfResponsibility extends JtObject {
public static final String JtCLASS_NAME = JtChainOfResponsibility.class.getName();
private static final long serialVersionUID = 1L;
private Object successor;
public JtChainOfResponsibility() {
}
/**
* Specifies the successor in the chain.
*
* @param successor successor
*/
public void setSuccessor (Object successor) {
this.successor = successor;
}
/**
* Returns the successor in the chain.
*/
public Object getSuccessor () {
return (successor);
}
/**
* Process object messages. If unable to process a particular message, forward it to the successor.
* @param event Jt Message
*/
public Object processMessage (Object event) {
String msgid = null;
JtMessage e = (JtMessage) event;
//Object content;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
// Remove this object
if (msgid.equals (JtObject.JtREMOVE)) {
return (null);
}
// Unable to handle the request, let the successor process the request
if (successor == null) {
handleError
("JtChainOfResposibility.processMessage: last in the chain was unable to process message ID:" + msgid);
return (null);
}
return (sendMessage (successor, event));
}
/**
* Demonstrates the messages processed by JtChainOfResposibilty.
*/
public static void main(String[] args) {
JtObject main = new JtFactory ();
JtMessage msg;
Jt.examples.HelloWorld helloWorld;
JtChainOfResponsibility chain;
// Create an instance of JtChainOfResponsibilty
chain = (JtChainOfResponsibility)
main.createObject (JtChainOfResponsibility.JtCLASS_NAME,
"chain");
helloWorld = (Jt.examples.HelloWorld) main.createObject (HelloWorld.JtCLASS_NAME,
"helloWorld");
main.setValue (chain, "successor", helloWorld);
msg = new JtMessage(HelloWorld.JtHELLO);
// Send a message ("JtHello") to the chain
System.out.println (main.sendMessage (chain, msg));
main.removeObject ("chain");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?