c#典型工廠化實(shí)現(xiàn)實(shí)例
工廠接口定義
/// <summary>
/// 工廠接口定義
/// </summary>
/// <remarks>
/// TTarget : abstract product type
/// TSource: concrete product type
/// </remarks>
public interface IFactory
{
#region config and register type mapping
/// <summary>
/// 如果需要同時(shí)加載配置文件中定義的映射關(guān)系,可以按照SRP的原則定義獨(dú)立的配置類型。
/// 由該配置類型調(diào)用這兩個(gè)接口為Factory加載配置信息
/// </summary>
IFactory RegisterType<TTarget, TSource>(); // fluent interface
IFactory RegisterType<TTarget, TSource>(string name); // fluent interface
#endregion
#region factory method
TTarget Create<TTarget>();
TTarget Create<TTarget>(string name);
#endregion
}
注冊(cè)類
public sealed class TypeRegistry
{
readonly string DefaultNmae = Guid.NewGuid().ToString();
IDictionary<Type, IDictionary<string, Type>> registry = new Dictionary<Type, IDictionary<string, Type>>();
public void RegisterType(Type targetType,Type sourceType)
{
RegisterType(targetType, sourceType, DefaultNmae);
}
public void RegisterType(Type targetType, Type sourceType,string name)
{
if (targetType == null) throw new ArgumentNullException("targetType");
if (sourceType == null) throw new ArgumentNullException("sourceType");
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
IDictionary<string, Type> subDictionary;
if (!registry.TryGetValue(targetType, out subDictionary))
{
subDictionary = new Dictionary<string, Type>();
subDictionary.Add(name, sourceType);
registry.Add(targetType, subDictionary);
}
else
{
if (subDictionary.ContainsKey(name))
throw new DuplicateKeyException(name);
subDictionary.Add(name, sourceType);
}
}
public Type this[Type targetType, string name]
{
get
{
if (targetType == null) throw new ArgumentNullException("targetType");
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
if (registry.Count() == 0)
return null;
return (registry
.Where(x => x.Key == targetType)).FirstOrDefault().Value
.Where(x => string.Equals(name, x.Key))
.FirstOrDefault().Value;
}
}
public Type this[Type targetType]
{
get { return this[targetType, DefaultNmae]; }
}
}
工廠類
public class Factory : IFactory
{
protected TypeRegistry registry = new TypeRegistry();
#region IFactory Members
public IFactory RegisterType<TTarget, TSource>()
{
registry.RegisterType(typeof(TTarget), typeof(TSource));
return this;
}
public IFactory RegisterType<TTarget, TSource>(string name)
{
registry.RegisterType(typeof(TTarget), typeof(TSource), name);
return this;
}
public TTarget Create<TTarget>()
{
return (TTarget)Activator.CreateInstance(registry[typeof(TTarget)]);
}
public TTarget Create<TTarget>(string name)
{
return (TTarget)Activator.CreateInstance(registry[typeof(TTarget), name]);
}
#endregion
}
調(diào)用
[TestMethod]
public void CreateInstance()
{
var factory = new Factory()
.RegisterType<IFruit, Apple>()
.RegisterType<IFruit, Orange>("o")
.RegisterType<IVehicle, Bicycle>()
.RegisterType<IVehicle, Bicycle>("a")
.RegisterType<IVehicle, Train>("b")
.RegisterType<IVehicle, Car>("c");
Assert.IsInstanceOfType(factory.Create<IFruit>(), typeof(Apple));
Assert.IsInstanceOfType(factory.Create<IFruit>("o"), typeof (Orange));
Assert.IsInstanceOfType(factory.Create<IVehicle>(), typeof(Bicycle));
Assert.IsInstanceOfType(factory.Create<IVehicle>("a"), typeof(Bicycle));
Assert.IsInstanceOfType(factory.Create<IVehicle>("b"), typeof(Train));
Assert.IsInstanceOfType(factory.Create<IVehicle>("c"), typeof(Car));
}
其實(shí)精髓還是在于注冊(cè)類的一個(gè)類似assembly的功能,通過字典的方式,封裝,然后通過泛型來比對(duì)實(shí)現(xiàn),或者通過配置文件傳參數(shù)過來實(shí)現(xiàn)出一個(gè)新的實(shí)例化
里面注意連貫接口,泛型,等操作
相關(guān)文章
C#獲取文件名和文件路徑的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了C#獲取文件名和文件路徑的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07C# Split分隔字符串的應(yīng)用(C#、split、分隔、字符串)
C# Split分隔字符串主要包括用字符串分隔,用多個(gè)字符來分隔,用單個(gè)字符來分隔等方法實(shí)現(xiàn),下面的具體的實(shí)現(xiàn)代碼2008-11-11C#實(shí)現(xiàn)變量交換、斐波那契數(shù)列、質(zhì)數(shù)、回文方法合集
這篇文章介紹了C#實(shí)現(xiàn)變量交換、斐波那契數(shù)列、質(zhì)數(shù)、回文的方法合集,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02C#實(shí)現(xiàn)支付寶沙箱支付的項(xiàng)目實(shí)踐
本文主要介紹了C#實(shí)現(xiàn)支付寶沙箱支付的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05c# for循環(huán)中創(chuàng)建線程執(zhí)行問題
這篇文章主要介紹了有關(guān)c# for循環(huán)中創(chuàng)建線程執(zhí)行問題,下面文章將將以舉例的方式展開for循環(huán)中創(chuàng)建線程執(zhí)行問題的內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-11-11