.NET工廠方法模式講解
工廠方法模式介紹:
工廠方法(Factory Method)模式的意義是定義一個創(chuàng)建產(chǎn)品對象的工廠接口,將實際創(chuàng)建工作推遲到子類當中。核心工廠類不再負責產(chǎn)品的創(chuàng)建,這樣核心類成為一個抽象工廠角色,僅負責具體工廠子類必須實現(xiàn)的接口,這樣進一步抽象化的好處是使得工廠方法模式可以使系統(tǒng)在不修改具體工廠角色的情況下引進新的產(chǎn)品。
工廠方法模式結(jié)構圖:
角色分類:
抽象工廠角色:是工廠方法模式的核心,與應用程序無關。任何在模式中創(chuàng)建的對象的工廠類必須實現(xiàn)這個接口。
具體工廠角色:這是實現(xiàn)抽象工廠接口的具體工廠類,包含與應用程序密切相關的邏輯,并且受到應用程序調(diào)用以創(chuàng)建產(chǎn)品對象
抽象產(chǎn)品角色:工廠方法模式所創(chuàng)建的對象的超類型,也就是產(chǎn)品對象的共同父類或共同擁有的接口。在上圖中,這個角色是Light。
具體產(chǎn)品角色:這個角色實現(xiàn)了抽象產(chǎn)品角色所定義的接口。某具體產(chǎn)品有專門的具體工廠創(chuàng)建,它們之間往往一一對應。
引入實際例子:
在上一篇博文簡單工廠模式中,使用簡單工廠模式進行了以下實現(xiàn):如果有一個住戶管理系統(tǒng),里面的住戶類型是可變的,每一種租戶類型的租金計算公式都存在差異例如:A類型的住戶租金額=天數(shù)*單價+績效*0.005;B類型的住戶租金額=月份*(每月價格+performance*0.001)這里我們雖然實現(xiàn)了客戶的需求,但是如果客戶后期需要增加了C類型商店和D類型商店,而它們的算法要求又不一樣,這個時候我們就需要進行C,D類型商店的創(chuàng)建,并繼承Ishop接口,實現(xiàn)里面的方法,同時還得繼續(xù)修改工廠類在switc中增加case進行捕捉創(chuàng)建相應的商店對象,一旦出現(xiàn)這樣的情況,是不利于程序的擴展性和項目后期的維護性的。
1.分析:商店有共同的行為特征,都要進行店鋪租金計算行為,我們抽象了Ishop ,里面有待實現(xiàn)的計算商店租金方法行為。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FactoryEntiy { public interface Ishop { double Getrent(int days, double dayprice, double performance); } }
2.我們實現(xiàn)Isho接口里面的方法,創(chuàng)建A,B類型店鋪。
using FactoryEntiy; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProductEnity { /// <summary> /// 繼承商店接口,實現(xiàn)里面的行為方法,即算法 /// </summary> public class Ashop:Ishop { /// <summary> /// /// A類型商店租金額,天數(shù)*單價+績效*0.005 /// </summary> /// <param name="days">天數(shù)</param> /// <param name="dayprice">每天單價</param> /// <param name="performance">日平均績效</param> /// <returns></returns> public double Getrent(int days, double dayprice, double performance) { Console.WriteLine("A商店的租金算法"); return days * dayprice + performance * 0.01; } } }
using FactoryEntiy; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProductEnity { /// <summary> /// 繼承商店接口,實現(xiàn)里面的行為方法,即算法 /// </summary> public class Bshop:Ishop { /// <summary> /// B類型商店的租金=月份*(每月價格+performance*0.001) /// </summary> /// <param name="month">月數(shù)</param> /// <param name="monthprice">月單價</param> /// <param name="performance">月平均績效</param> /// <returns></returns> public double Getrent(int month, double monthprice, double performance) { Console.WriteLine("B商店的租金算法"); return month * (monthprice + performance * 0.001); } } }
3.現(xiàn)在考慮在什么情況下創(chuàng)建商店的實體,對不同的商店進行租金計算,并且方便以后的增加和修改。于是我們創(chuàng)建IFactroy接口,里面有待實現(xiàn)的創(chuàng)建商店對象的方法。
using FactoryEntiy; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FactoryMethod { /// <summary> /// 工廠類,創(chuàng)建商店類型實體 /// </summary> public interface IFactory { Ishop CreateShop(); } }
4.現(xiàn)在我們就可以繼承自IFactory,實現(xiàn)里面創(chuàng)建對應的商店對象了。
using FactoryEntiy; using FactoryMethod; using ProductEnity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProductEnity { /// <summary> /// 繼承工廠類,創(chuàng)建A類型商店實體 /// </summary> public class CreateBshop : IFactory { public Ishop CreateShop() { return new Ashop(); } } }
using FactoryEntiy; using FactoryMethod; using ProductEnity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProductEnity { /// <summary> /// 繼承工廠類,創(chuàng)建B類型商店實體 /// </summary> public class CreateAshop : IFactory { public Ishop CreateShop() { return new Bshop(); } } }
5.之后根據(jù)當前的商店類型進行判斷,該類型的商店應該進行哪一種算法:
using FactoryEntiy; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Reflection; using System.Text; namespace FactoryMethod.App { class Program { static void Main(string[] args) { string shopname = ConfigurationManager.AppSettings["CreateShopClassName"]; //shopname為創(chuàng)建商店類名稱,此處=CreateAshop IFactory af = (IFactory)Assembly.Load("ProductEnity").CreateInstance("ProductEnity." + shopname); //第一個ProductEnity是dll的名稱,第二個ProductEnity是項目的命名空間。 Ishop As = af.CreateShop(); double total = As.Getrent(30, 300, 2000); //30 天/100元 日平均績效為2000 Console.WriteLine("該A類型商店的租金為:" + total); Console.WriteLine("============="); IFactory bf = (IFactory)Assembly.Load("ProductEnity").CreateInstance("ProductEnity." + "CreateBshop"); //CreateBshop可以保存為配置或者存在數(shù)據(jù)庫中, //注意該保存字符串應該與項目中創(chuàng)建的類名一樣, //否則反射的方式會找不到該項目下的類。 Ishop Bs = bf.CreateShop(); total = Bs.Getrent(30, 300, 2000); //30 天/100元 日平均績效為2000 Console.WriteLine("該A類型商店的租金為:" + total); } } }
這里我們使用反射的方式創(chuàng)建對象,替換了之前的工廠類通過switch創(chuàng)建對象的方式,有利于后面的新類型商店增加和算法修改增加和維護以后在項目需求在有變革,我們只需要重新在項目中增加C,D類型商店,繼承Ishop實現(xiàn)里面的方法,同時,添加繼承IFactroy接口,創(chuàng)建對應的商店對象編譯該項目的ProductEnity.dll,以后再進行計算該C,D類型的商店算法就可以通過反射的方式進行計算,不需要修改原來的工程類代碼。
整個項目的結(jié)構圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
.NET讀寫Excel工具Spire.Xls使用 Excel單元格控制(3)
這篇文章主要為大家詳細介紹了.NET讀寫Excel工具Spire.Xls使用,Excel單元格控制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11淺談.net core 注入中的三種模式:Singleton、Scoped 和 Transient
這篇文章主要介紹了淺談.net core 注入中的三種模式:Singleton、Scoped 和 Transient,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04Asp.net 圖片文件防盜鏈(尊重勞動成果)及BeginRequest事件學習
關于圖片盜鏈這個問題,畢竟是自己的勞動成功,很多人不希望別人就那么輕易地偷走了;反盜鏈的程序其實很簡單,熟悉ASP.NET 應用程序生命周期的話很容易就可以寫一個,運用HttpModule在BeginRequest事件中攔截請求就ok了2013-01-01