标签:inf out type demo code set space sem type属性
1、作用:抽象工厂的目的就是生产【产品族】,之前讲的“工厂模式”的每一个factory只造一种产品,抽象工厂让一个factory造多个产品。
uml类图:
代码实现:
抽象工厂代码:
namespace CNBlogs.DesignPattern.Common { /// <summary> /// 抽象工厂类 /// </summary> public abstract class AbstractEquipment { /// <summary> /// 抽象方法: 创建一辆车 /// </summary> /// <returns></returns> public abstract AbstractCar CreateCar(); /// <summary> /// 抽象方法: 创建背包 /// </summary> /// <returns></returns> public abstract AbstractBackpack CreateBackpack(); } }
抽象产品代码:
namespace CNBlogs.DesignPattern.Common { /// <summary> /// 抽象产品: 车抽象类 /// </summary> public abstract class AbstractCar { /// <summary> /// 车的类型属性 /// </summary> public abstract string Type { get; } /// <summary> /// 车的颜色属性 /// </summary> public abstract string Color { get; } } /// <summary> /// 抽象产品: 背包抽象类 /// </summary> public abstract class AbstractBackpack { /// <summary> /// 包的类型属性 /// </summary> public abstract string Type { get; } /// <summary> /// 包的颜色属性 /// </summary> public abstract string Color { get; } } }
具体工厂代码:
namespace CNBlogs.DesignPattern.Common { /// <summary> /// 运动装备 /// </summary> public class SportEquipment : AbstractEquipment { public override AbstractCar CreateCar() { return new SportCar(); } public override AbstractBackpack CreateBackpack() { return new SportBackpack(); } } /// <summary> /// 越野装备 这里就不添加了,同运动装备一个原理,demo里只演示一个,实际项目中可以按需添加 /// </summary> //public class JeepEquipment : AbstractEquipment //{ // public override AbstractCar CreateCar() // { // return new JeeptCar(); // } // public override AbstractBackpack CreateBackpack() // { // return new JeepBackpack(); // } //} }
具体产品代码:
namespace CNBlogs.DesignPattern.Common { /// <summary> /// 跑车 /// </summary> public class SportCar : AbstractCar { private string type = "Sport"; private string color = "Red"; /// <summary> /// 重写基类的Type属性 /// </summary> public override string Type { get { return type; } } /// <summary> /// 重写基类的Color属性 /// </summary> public override string Color { get { return color; } } } /// <summary> /// 运动背包 /// </summary> public class SportBackpack : AbstractBackpack { private string type = "Sport"; private string color = "Red"; /// <summary> /// 重写基类的Type属性 /// </summary> public override string Type { get { return type; } } /// <summary> /// 重写基类的Color属性 /// </summary> public override string Color { get { return color; } } } } //具体产品可以有很多很多, 至于越野类的具体产品这里就不列出来了。
创建装备代码:
namespace CNBlogs.DesignPattern.Common { public class CreateEquipment { private AbstractCar fanCar; private AbstractBackpack fanBackpack; public CreateEquipment(AbstractEquipment equipment) { fanCar = equipment.CreateCar(); fanBackpack = equipment.CreateBackpack(); } public void ReadyEquipment() { Console.WriteLine(string.Format("老范背着{0}色{1}包开着{2}色{3}车。", fanBackpack.Color, fanBackpack.Type, fanCar.Color, fanCar.Type )); } } }
客户端代码:
//------------------------------------------------------------------------------ // <copyright file="Program.cs" company="CNBlogs Corporation"> // Copyright (C) 2015-2016 All Rights Reserved // 原博文地址: http://www.cnblogs.com/toutou/ // 作 者: 请叫我头头哥 // </copyright> //------------------------------------------------------------------------------ namespace CNBlogs.DesignPattern { using System; using System.Configuration; using System.Reflection; using CNBlogs.DesignPattern.Common; class Program { static void Main(string[] args) { // ***具体app.config配置如下*** // //<add key="assemblyName" value="CNBlogs.DesignPattern.Common"/> //<add key="nameSpaceName" value="CNBlogs.DesignPattern.Common"/> //<add key="typename" value="SportEquipment"/> // 创建一个工厂类的实例 string assemblyName = ConfigurationManager.AppSettings["assemblyName"]; string fullTypeName = string.Concat(ConfigurationManager.AppSettings["nameSpaceName"], ".", ConfigurationManager.AppSettings["typename"]); AbstractEquipment factory = (AbstractEquipment)Assembly.Load(assemblyName).CreateInstance(fullTypeName); CreateEquipment equipment = new CreateEquipment(factory); equipment.ReadyEquipment(); Console.Read(); } } }
标签:inf out type demo code set space sem type属性
原文地址:https://www.cnblogs.com/25miao/p/10389760.html