public static IProduct GetProduct(RoomParts part) { IFactory factory = null; // 传统工厂模式的弊端:工厂管理类和工厂类族的紧耦合 switch (part) { case RoomParts.Roof: factory = new RoofFactory(); break; case RoomParts.Window: factory = new WindowFactory(); break; case RoomParts.Pillar: factory = new PillarFactory(); break; default: return(null); } // 利用工厂生产产品 var product = factory.Produce(); Console.WriteLine($"生产了一个产品:{product.GetName()}"); return(product); }
public static IProduct GetProduct(RoomParts part) { IFactory factory = null; //这里就是传统工厂模式的弊端,工厂管理者和工厂类族类耦合 //根据不同的产品类型,找到对应的工厂 switch (part) { case RoomParts.Roof: factory = new RoofFactory(); break; case RoomParts.Window: factory = new WindowFactory(); break; case RoomParts.Pillar: factory = new PillarFactory(); break; default: break; } //利用工厂生产出产品 IProduct product = factory.Produce(); Console.WriteLine("生产了一个产品:{0}", product.GetName()); return(product); }
public static IProduct GetProduct(RoomParts part) { Factory factory = new Factory(); IProduct product = factory.Produce(part); Console.WriteLine("生产了一个产品:{0}", product.GetName()); return(product); }
public static IProduct GetProduct(RoomParts part) { // 一共只有一个工厂 var factory = new Factory(); var product = factory.Produce(part); Console.WriteLine($"生产了一个产品:{product.GetName()}"); return(product); }
public IProduct Produce(RoomParts parts) { ProductListAttribute attr = (ProductListAttribute)Attribute.GetCustomAttribute(typeof(IProduct), typeof(ProductListAttribute)); foreach (var type in attr.ProductList) { ProductAttribute pa = (ProductAttribute)Attribute.GetCustomAttribute(type, typeof(ProductAttribute)); if (pa.RoomPart == parts) { object product = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return(product as IProduct); } } return(null); }
public IProduct Produce(RoomParts part) { // 通过反射从IProduct接口中获得属性从而获得所有产品列表 var attr = (ProductListAttribute)Attribute.GetCustomAttribute(typeof(IProduct), typeof(ProductListAttribute)); // 遍历所有的实现产品零件类型 foreach (var type in attr.ProductList) { // 利用反射查找其属性 var pa = (ProductAttribute)Attribute.GetCustomAttribute(type, typeof(ProductAttribute)); // 确定是否是需要到的零件 if (pa.RoomPart == part) { // 利用反射动态创建产品零件类型实例 var product = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return(product as IProduct); } } return(null); }
public static IProduct GetProduct(RoomParts part) { IFactory factory = null; switch (part) { case RoomParts.Roof: factory = new RoofFactory(); break; case RoomParts.Window: factory = new WindowFactory(); break; case RoomParts.Pillar: factory = new PillarFactory(); break; default: return null; } IProduct product = factory.Produce(); Console.WriteLine("生产了一个产品:{0}", product.GetName()); return product; }
public IProduct Produce(RoomParts part) { //通过反射,从IProduct接口中获取属性 //从而获得所有的产品零件列表 ProductListAttribute attr = (ProductListAttribute)Attribute.GetCustomAttribute(typeof(IProduct), typeof(ProductListAttribute)); //遍历所有的实现产品零件类型 foreach (var type in attr.ProductList) { //利用反射查找其属性 ProductAttribute pa = (ProductAttribute)Attribute.GetCustomAttribute(type, typeof(ProductAttribute)); //确定是否是需要的产品 if (pa.RoomPart == part) { //再一次利用反射,创建产品零件类型 Object product = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return(product as IProduct); } } return(null); }
public ProductAttribute(RoomParts parts) { myRoomPart = parts; }
public ProductAttribute(RoomParts part) { _myRoomPart = part; }