Jakarta-Common-Chain使用笔记
时间:2011-01-28 csdn博客 沈斌
可以在你需要定义和执行一些顺序操作的时候采用Commons Chain。
下载地址:http://commons.apache.org/downloads/download_chain.cgi
五个工作类:
GetCustomerInfo.jar
package demo.chain;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
public class GetCustomerInfo implements Command ...{
public boolean execute(Context context) throws Exception ...{
System.out.println("Get customer info");
context.put("customerName","George Burdell");
return false;
}
}
TestDriveVehicle.jar
package demo.chain;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
public class TestDriveVehicle implements Command ...{
public boolean execute(Context context) throws Exception ...{
System.out.println("Test drive the vehicle");
return false;
}
}
NegotiateSale.jar
package demo.chain;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
public class NegotiateSale implements Command ...{
public boolean execute(Context context) throws Exception ...{
System.out.println("Negotiate sale");
return false;
}
}
Jakarta-Common-Chain使用笔记(2)
时间:2011-01-28 csdn博客 沈斌
ArrangeFinancing.jar
package demo.chain;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
public class ArrangeFinancing implements Command ...{
public boolean execute(Context context) throws Exception ...{
System.out.println("Arrange financing");
return false;
}
}
CloseSale.jar
package demo.chain;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
public class CloseSale implements Command ...{
public boolean execute(Context context) throws Exception ...{
System.out.println("Congratulations " +context.get("customerName") +", you bought a new car!");
return false;
}
}
测试代码:
package demo;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;
import org.apache.commons.chain.impl.ContextBase;
import demo.chain.ArrangeFinancing;
import demo.chain.CloseSale;
import demo.chain.GetCustomerInfo;
import demo.chain.NegotiateSale;
import demo.chain.TestDriveVehicle;
public class SellVehicleChain extends ChainBase ...{
public SellVehicleChain() ...{
super();
addCommand(new GetCustomerInfo());
addCommand(new TestDriveVehicle());
addCommand(new NegotiateSale());
addCommand(new ArrangeFinancing());
addCommand(new CloseSale());
}
public static void main(String[] args) throws Exception ...{
Command process = new SellVehicleChain();
Context ctx = new ContextBase();
pro
|