设计模式

Apache Commons Chain用法

2019-05-07  本文已影响0人  勤劳的熊熊

简介

先从一个例子开始

public abstract class SellVehicleTemplate {  
   // 销售汽车 
   public void sellVehicle() {  
        testDriveVehicle();  
        negotiateSale();  
        arrangeFinancing();  
        closeSale();  
    }  
    // 试车
    public abstract void testDriveVehicle();  
    // 销售谈判
    public abstract void negotiateSale();  
    // 安排财务
    public abstract void arrangeFinancing();  
    // 结束销售
    public abstract void closeSale();  
}  
import org.apache.commons.chain.Command;  
import org.apache.commons.chain.Context;  
// 试车(继承Command)
public class TestDriveVehicle implements Command {  
    public boolean execute(Context ctx) throws Exception {  
        System.out.println("Test drive the vehicle");  
        return false;  
    }  
}  
// 销售谈判
public class NegotiateSale implements Command {  
    public boolean execute(Context ctx) throws Exception {  
        System.out.println("Negotiate sale");  
        return false;  
    }    
}  
// 安排财务
public class ArrangeFinancing implements Command {  
    public boolean execute(Context ctx) throws Exception {  
        System.out.println("Arrange financing");  
        return false;  
    }    
}  
// 结束销售
public class CloseSale implements Command {  
      public boolean execute(Context ctx) throws Exception {  
        System.out.println("Congratulations " + ctx.get("customerName") +", you bought a new car!");  
        return false;  
    }  
}  

// 定义责任链并测试
import org.apache.commons.chain.impl.ChainBase;  
import org.apache.commons.chain.Command;  
import org.apache.commons.chain.Context;  
import org.apache.commons.chain.impl.ContextBase;  
// 继承ChainBase
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();  
        process.execute(ctx);  
    }  

// 运行结果:
Test drive the vehicle 
Negotiate sale 
Arrange financing 
Congratulations George Burdell, you bought a new car! 
<catalog>  
  <chain name="sell-vehicle">  
    <command id="GetCustomerInfo" className="com.jadecove.chain.sample.GetCustomerInfo"/>  
    <command id="TestDriveVehicle" className="com.jadecove.chain.sample.TestDriveVehicle"/>  
    <command id="NegotiateSale" className="com.jadecove.chain.sample.NegotiateSale"/>  
    <command id="ArrangeFinancing" className="com.jadecove.chain.sample.ArrangeFinancing"/>  
    <command id="CloseSale" className="com.jadecove.chain.sample.CloseSale"/>  
  </chain>  
</catalog>

// 从xml配置中读取
public class CatalogLoader {  
  
    private static final String CONFIG_FILE = "/com/jadecove/chain/sample/chain-config.xml";  
  
    private ConfigParser parser;  
  
    private Catalog catalog;  
  
    public CatalogLoader() {  
        parser = new ConfigParser();  
    }  
  
    public Catalog getCatalog() throws Exception {  
        if (catalog == null) {  
            parser.parse(this.getClass().getResource(CONFIG_FILE));  
        }  
        catalog = CatalogFactoryBase.getInstance().getCatalog();  
        return catalog;  
    }  
  
    public static void main(String[] args) throws Exception {  
        CatalogLoader loader = new CatalogLoader();  
        Catalog sampleCatalog = loader.getCatalog();  
        Command command = sampleCatalog.getCommand("sell-vehicle");  
        Context ctx = new SellVehicleContext();  
        command.execute(ctx);  
    }  
}  

API详解

Context显式调用

public class SellVehicleContext extends ContextBase {  
  
    private String customerName;  

    public String getCustomerName() {  
        return customerName;  
    }  

    public void setCustomerName(String name) {  
        this.customerName = name;  
    }    
}  
// 进行类型转换
public boolean execute(Context ctx) throws Exception {  
    SellVehicleContext myCtx = (SellVehicleContext) ctx;  
    System.out.println("Congratulations " + myCtx.getCustomerName() + ", you bought a new car!");  
    return false;  
}  

// 调用
public static void main(String[] args) throws Exception {  
    Command process = new SellVehicleChain();  
    Context ctx = new SellVehicleContext();  
    process.execute(ctx);  
}  

参考:

  1. Commons-chain:责任链框架
  2. 使用Apache Commons Chain
上一篇 下一篇

猜你喜欢

热点阅读