回顾以前的价格系统,是一套比较经典的工厂模式,

每一类客户对应不同的价格类型,执行不同的价格。基本的结构如图:

老生常谈——工厂模式_设计模式、工厂模式

 

基本逻辑:定义了一个价格类型接口InterfacePriceType,所有的价格类型的继承这一接口,价格类型调用:通过xml配置了客户类型与价格类型的对应关系,从而确定了具体的价格算法。

代码如下:

1、定义价格类型接口:

public interface InterfacePriceType

{

  string getPriceType();

}

 

2、具体的价格类型,全都继承上面的接口:

比如:0809版价格:

public class Price0809:InterfacePriceType

{

  public string getPriceType()

  {

    //Doing SomeThing

  }

}

又如,2010版价格:

 

public class Price2010:InterfacePriceType

{

  public string getPriceType()

  {

    //Doing SomeThing

  }

}

上面建立了价格类型的工厂,下面就是根据客户类型对不同价格的实际调用

首先建立了一个XML的配置文件,xml内容如下:

<?xml version="1.0" encoding="utf-8"?>

<Root>

<Node text='2010版价格' namespace="Price.Price2010" index="2010">

<Node text='0908版价格' namespace="Price.Price0908" index="2009">

<Root>

</xml>

其中text表示价格类型的说明,namespace是价格类型的命名空间,index表示的是客户类型

通过xml我们建立了客户类型与价格类型的对应关系,下面主要通过反射来取得价格类型。

  public static InterfacePriceType loadXML(int CustomType)

  {

InterfacePriceType InterfacePriceType = null;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("XML路径");
XmlNode xmlNode = xmlDoc.DocumentElement;
XmlNodeList xmlNodeList = xmlNode.ChildNodes;
foreach (XmlNode xn in xmlNodeList)
{
XmlElement xe = (XmlElement)xn;
string NameSpaceClass = xe.GetAttribute("namespace");
int index = Convert.ToInt32(xe.GetAttribute("index"));

if (index == CustomType)
{
//采用反射方式实例化价格类
Assembly ass = Assembly.Load("pricetype");
InterfacePriceType = (InterfacePriceType)ass.CreateInstance(NameSpaceClass);
break;
}
}
return InterfacePriceType;

  }

 

这里通过xml反射取得价格类型,基本上就达到目的,现在只要来了一种新的价格只要继承价格类型接口,同时在xml配置一下就ok了。

这样的设计有哪些局限性呢?欢迎指正。