需求:工廠類根據參數生成對應類的實例。示例:RoomParts.csnamespace ReflectionFactory{ /// /// 屋子產品的零件 /// public enum RoomParts { Roof, Window...
需求:工廠類根據參數生成對應類的實例。
示例:
RoomParts.cs
namespace ReflectionFactory { /// <summary> /// 屋子產品的零件 /// </summary> public enum RoomParts { Roof, Window, Pillar } }
ProductAttribute.cs
using System; namespace ReflectionFactory { /// <summary> /// 這個特性用來附加在產品類型之上, /// 來標註該類型代表哪個產品,方便反射使用 /// </summary> public class ProductAttribute : Attribute { /// <summary> /// 標註零件的成員 /// </summary> private RoomParts _myRoomPart; public ProductAttribute(RoomParts part) { _myRoomPart = part; } public RoomParts RoomPart { get { return _myRoomPart; } } } }
ProductListAttribute.cs
using System; namespace ReflectionFactory { /// <summary> /// 這個特性用來附加在產品介面之上 /// 用來標註一共實現了多少產品零件,方便反射使用 /// </summary> public class ProductListAttribute : Attribute { /// <summary> /// 產品類型集合 /// </summary> private Type[] _myList; public ProductListAttribute(Type[] products) { _myList = products; } public Type[] ProductList { get { return _myList; } } } }
IProduct.cs
using System; namespace ReflectionFactory { /// <summary> /// 產品零件介面, /// 需要添加所有實現該介面的列表 /// </summary> [ProductList(new Type[] { typeof(Roof), typeof(Window), typeof(Pillar) })] public interface IProduct { string GetName(); } }
Roof.cs
namespace ReflectionFactory { /// <summary> /// 屋頂類型 /// </summary> [Product(RoomParts.Roof)] public class Roof : IProduct { public string GetName() { return "屋頂"; } } }
Window.cs
namespace ReflectionFactory { /// <summary> /// 窗戶類型 /// </summary> [Product(RoomParts.Window)] public class Window : IProduct { public string GetName() { return "窗戶"; } } }
Pillar.cs
namespace ReflectionFactory { /// <summary> /// 柱子類型 /// </summary> [Product(RoomParts.Pillar)] public class Pillar : IProduct { public string GetName() { return "柱子"; } } }
IFactory.cs
namespace ReflectionFactory { public interface IFactory { IProduct Produce(RoomParts part); } }
Factory.cs
using System; using System.Reflection; namespace ReflectionFactory { /// <summary> /// 工廠類型 /// </summary> public class Factory : IFactory { public IProduct Produce(RoomParts part) { // 通過反射,從IProduct介面獲得屬性 // 從而獲得所有的產品零件列表 ProductListAttribute attr = Attribute.GetCustomAttribute(typeof(IProduct), typeof(ProductListAttribute)) as ProductListAttribute; // 遍歷所有的實現產品零件類型 foreach (Type type in attr.ProductList) { // 利用發射查找其屬性 ProductAttribute pa = Attribute.GetCustomAttribute(type, typeof(ProductAttribute)) as ProductAttribute; // 確定是否是需要的零件 if (pa.RoomPart == part) { // 利用反射創建產品零件類型 Object product = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return product as IProduct; } } return null; } } }
FactoryManager.cs
using System; namespace ReflectionFactory { /// <summary> /// 工廠管理類型 /// </summary> public class FactoryManager { public static IProduct GetProduct(RoomParts part) { // 一共只有一個工廠,不再需要根據產品類型而挑選工廠 Factory factory = new Factory(); IProduct product = factory.Produce(part); Console.WriteLine("生產了一個產品:" + product.GetName()); return product; } } }
Program.cs
using System; namespace ReflectionFactory { class Program { static void Main(string[] args) { // output : 生產了一個產品:柱子 IProduct pillar = FactoryManager.GetProduct(RoomParts.Pillar); // output : 生產了一個產品:屋頂 IProduct roof = FactoryManager.GetProduct(RoomParts.Roof); // output : 生產了一個產品:窗戶 IProduct window = FactoryManager.GetProduct(RoomParts.Window); Console.Read(); } } }