c#典型工廠化實現執行個體

來源:互聯網
上載者:User

工廠介面定義

複製代碼 代碼如下:/// <summary>
/// 工廠介面定義
/// </summary>
/// <remarks>
/// TTarget : abstract product type
/// TSource: concrete product type
/// </remarks>
public interface IFactory
{
#region config and register type mapping

/// <summary>
/// 如果需要同時載入設定檔中定義的映射關係,可以按照SRP的原則定義獨立的配置類型。
/// 由該配置類型調用這兩個介面為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
}

註冊類

複製代碼 代碼如下: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
}

調用

複製代碼 代碼如下:[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));
}

其實精髓還是在於註冊類的一個類似assembly的功能,通過字典的方式,封裝,然後通過泛型來比對實現,或者通過設定檔傳參數過來實現出一個新的執行個體化

裡面注意連貫介面,泛型,等操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.