現(xiàn)在我們在開發(fā)一些web項(xiàng)目或者WInform項(xiàng)目時(shí),我們都要數(shù)據(jù)庫來管理所有的信息,現(xiàn)在就以我開發(fā)的一個(gè)系統(tǒng)《隴原商城》為例,假如我的系統(tǒng)投入使用了,假如我用的數(shù)據(jù)庫就是Access數(shù)據(jù)庫,但是,過了一段時(shí)間,由于隴原商城貨買的非常好,Access數(shù)據(jù)庫已近不能滿足客戶的需求了,這時(shí)候客戶想換成Sql Server數(shù)據(jù)庫,這樣的話,我們又必須重新編寫代碼,重新?lián)Q成Sql Server數(shù)據(jù)庫來實(shí)現(xiàn),假如又過了一段時(shí)間,Sql Server數(shù)據(jù)庫也不能滿足用戶的的需求,這時(shí)候用戶又想換成Oracel數(shù)據(jù)庫來實(shí)現(xiàn)呢,到這里就不說了,可能我們開發(fā)人員就帶吐血啊,這樣就引起來我們的深思,我們怎么樣做一個(gè)系統(tǒng)可以讓我們不在那么麻煩的修改代碼呢?這就涉及到設(shè)計(jì)模式了,所以就出現(xiàn)了工廠方法模式,下面用工廠方法模式做一個(gè)小實(shí)驗(yàn)來實(shí)現(xiàn)這樣的一個(gè)過程。
新建一個(gè)控制臺(tái)應(yīng)用程序,命名為FactoryMethodPattern,在控制臺(tái)中添加一個(gè)IProductDAL接口,在里面定義一個(gè)方法,實(shí)現(xiàn)如下:
代碼如下:
namespace FactoryMethodPattern
{
public interface IProductDAL
{
void Insert();
}
}
然后新建接口實(shí)現(xiàn)工廠模式IProductDALFactory,實(shí)現(xiàn)如下:
代碼如下:
namespace FactoryMethodPattern
{
public interface IProductDALFactory
{
IProductDAL CreateProductDAL();
}
}
接下來在項(xiàng)目中添加類AccessProductDAL,繼承自接口IProductDAL,實(shí)現(xiàn)的方法是向控制臺(tái)
namespace FactoryMethodPattern
{
public class AccessProductDAL:IProductDAL
{
#region IProductDAL 成員
public void Insert()
{
Console.WriteLine("AccessProductDAL.Insert");
}
#endregion
}
}
然后創(chuàng)建一個(gè)AccessProductDAL的工廠類,使他繼承自IProductDALFactory接口,創(chuàng)建一個(gè)方法使其它的返回值為IProductDAL,最后在方法的實(shí)現(xiàn)里面返回實(shí)例化的AccessProductDAL,實(shí)現(xiàn)代碼如下:
代碼如下:
namespace FactoryMethodPattern
{
public class AccessProductDALFactory:IProductDALFactory
{
#region IProductDALFactory 成員
public IProductDAL CreateProductDAL()
{
return new AccessProductDAL();
}
#endregion
}
}
接下來寫一下:實(shí)現(xiàn)Sql Server數(shù)據(jù)庫的方法,添加一個(gè)類SqlProductDAL,使其方法
namespace FactoryMethodPattern
{
public class SqlProductDAL:IProductDAL
{
#region IProductDAL 成員
public void Insert()
{
Console.WriteLine("SqlProductDAL.Insert");
}
#endregion
}
}
再添加SqlProductDALFactory類,實(shí)現(xiàn)代碼如下:
代碼如下:
namespace FactoryMethodPattern
{
public class SqlProductDALFactory:IProductDALFactory
{
#region IProductDALFactory 成員
public IProductDAL CreateProductDAL()
{
return new SqlProductDAL();
}
#endregion
}
}
接下來添加App.config文件,來實(shí)現(xiàn)系統(tǒng)所選擇的數(shù)據(jù)庫是什么數(shù)據(jù)庫,代碼如下:
代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DALFactory" value="FactoryMethodPattern.SqlProductDALFactory"/>
</appSettings>
</configuration>
在寫業(yè)務(wù)邏輯層BLL,利用反射獲取app.config中所選擇的路徑是什么。讀取出來
代碼如下:
代碼如下:
namespace FactoryMethodPattern
{
public class BLL
{
public void Insert()
{
//利用反射實(shí)現(xiàn)功能
IProductDALFactory factory =(IProductDALFactory) Assembly.GetExecutingAssembly().CreateInstance(ConfigurationManager.AppSettings["DALFactory"]);
IProductDAL pro = factory.CreateProductDAL();
pro.Insert();
}
}
}
最后在program里面讀取BLL層信息,
namespace FactoryMethodPattern
{
class Program
{
static voidMain(string[] args)
{
BLL product = new BLL();
product.Insert();
Console.ReadKey();
}
}
}
最后單擊運(yùn)行顯示的
現(xiàn)在這個(gè)小系統(tǒng)整體就完成了,現(xiàn)在我要加入Oracel數(shù)據(jù)庫呢?我只要在寫兩個(gè)Oracel數(shù)據(jù)庫的類加到里面,再在app.config中修改一下路徑就OK了。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com