在產(chǎn)品項目進(jìn)行中我們難免會(huì )遇到因為項目服務(wù)對象不同,實(shí)現也不同的功能。
這樣每次修改后重新編譯整個(gè)解決方案后更新耗時(shí)耗力。
其實(shí)使用靜態(tài)工廠(chǎng),我們可以很容易的引入額外的dll來(lái)動(dòng)態(tài)的配置這些功能的實(shí)現
首先下面是所有工廠(chǎng)的基類(lèi),其中包含了一個(gè)讀取配置文件的方法,和一個(gè)生成對象的靜態(tài)方法

/// <summary>
/// 所有工廠(chǎng)基類(lèi)
///<author name="Jan.David"></author>
///<![CDATA[2010-11-05]]>
/// </summary>public abstract class AbstractFactory
{
/// <summary>
/// 讀取設置文件內的值
/// </summary>
/// <param name="parentNode"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string ReadSettring(string parentNode,string key)
{
string FilePath = AppDomain.CurrentDomain.BaseDirectory;//項目路徑
//新建xml文檔對象
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
//讀取文檔
doc.Load(FilePath + "/Setting.xml");
//獲取根節點(diǎn)
System.Xml.XmlNodeList Root = doc.FirstChild.NextSibling.ChildNodes;
System.Xml.XmlNodeList nodes = default(System.Xml.XmlNodeList);
foreach (System.Xml.XmlNode node in Root)
{
if (node.Name.ToLower() == parentNode.ToLower())
{
nodes = node.ChildNodes;
break;
}
}
if (nodes != null && nodes.Count > 0)
{
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Attributes["name"].Value.ToLower() == key.ToLower())
{
return node.Attributes["value"].Value;
}
}
}
return null;
}
/// <summary>
/// 實(shí)例化一個(gè)對象
/// </summary>
/// <typeparam name="T">要實(shí)例化的類(lèi)型</typeparam>
/// <param name="ClassName">需要實(shí)例化的類(lèi)名</param>
/// <param name="obj">實(shí)例化對象存儲的變量</param>
public abstract void CreateInstanc<T>(string className,ref T obj);
}
下面是具體的實(shí)現

配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<BillCode>
<add name="Returns" value ="IBillCodeImplt.Returns"></add>
</BillCode>
</configuration>
聯(lián)系客服