程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Jakarta-Common-Chain使用筆記

Jakarta-Common-Chain使用筆記

編輯:關於JAVA

可以在你需要定義和執行一些順序操作的時候采用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;
   }
}

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();
     process.execute(ctx);
   }
}

也可以使用xml文件進行配置:新建chain-config.xml文件如下:

<catalog>
   <chain name="sell-vehicle">
     <command id="GetCustomerInfo" className="demo.chain.GetCustomerInfo" />
     <command id="TestDriveVehicle" className="demo.chain.TestDriveVehicle" />
     <command id="NegotiateSale" className="demo.chain.NegotiateSale" />
     <command id="ArrangeFinancing" className="demo.chain.ArrangeFinancing" />
     <command id="CloseSale" className="demo.chain.CloseSale" />
   </chain>
</catalog>

新建SellVehicleContext.jar文件

package demo;

import org.apache.commons.chain.impl.ContextBase;

public class SellVehicleContext extends ContextBase ...{

   private static final long serialVersionUID = 6954127190729021870L;

   private String customerName;

   public String getCustomerName() ...{
     return customerName;
   }

   public void setCustomerName(String name) ...{
     this.customerName = name;
   }
}

測試類如下:

package demo;

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.config.ConfigParser;
import org.apache.commons.chain.impl.CatalogFactoryBase;

public class CatalogLoader ...{
   private static final String CONFIG_FILE = "/demo/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);
   }
}

Chain簡明手冊:http://blog.donews.com/foxgem/archive/2005/08/05/495938.aspx

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved