c# 设计模式 之:抽象工厂
标签:inf out type demo code set space sem type属性
1、作用:抽象工厂的目的就是生产【产品族】,之前讲的“工厂模式”的每一个factory只造一种产品,抽象工厂让一个factory造多个产品。
uml类图:
代码实现:
抽象工厂代码:
namespace CNBlogs.DesignPattern.Common
{
///
/// 抽象工厂类
///
public abstract class AbstractEquipment
{
///
/// 抽象方法: 创建一辆车
///
///
public abstract AbstractCar CreateCar();
///
/// 抽象方法: 创建背包
///
///
public abstract AbstractBackpack CreateBackpack();
}
}
抽象产品代码:
namespace CNBlogs.DesignPattern.Common
{
///
/// 抽象产品: 车抽象类
///
public abstract class AbstractCar
{
///
/// 车的类型属性
///
public abstract string Type
{
get;
}
///
/// 车的颜色属性
///
public abstract string Color
{
get;
}
}
///
/// 抽象产品: 背包抽象类
///
public abstract class AbstractBackpack
{
///
/// 包的类型属性
///
public abstract string Type
{
get;
}
///
/// 包的颜色属性
///
public abstract string Color
{
get;
}
}
}
具体工厂代码:
namespace CNBlogs.DesignPattern.Common
{
///
/// 运动装备
///
public class SportEquipment : AbstractEquipment
{
public override AbstractCar CreateCar()
{
return new SportCar();
}
public override AbstractBackpack CreateBackpack()
{
return new SportBackpack();
}
}
///
/// 越野装备 这里就不添加了,同运动装备一个原理,demo里只演示一个,实际项目中可以按需添加
///
//public class JeepEquipment : AbstractEquipment
//{
// public override AbstractCar CreateCar()
// {
// return new JeeptCar();
// }
// public override AbstractBackpack CreateBackpack()
// {
// return new JeepBackpack();
// }
//}
}
具体产品代码:
namespace CNBlogs.DesignPattern.Common
{
///
/// 跑车
///
public class SportCar : AbstractCar
{
private string type = "Sport";
private string color = "Red";
///
/// 重写基类的Type属性
///
public override string Type
{
get
{
return type;
}
}
///
/// 重写基类的Color属性
///
public override string Color
{
get
{
return color;
}
}
}
///
/// 运动背包
///
public class SportBackpack : AbstractBackpack
{
private string type = "Sport";
private string color = "Red";
///
/// 重写基类的Type属性
///
public override string Type
{
get
{
return type;
}
}
///
/// 重写基类的Color属性
///
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 (C) 2015-2016 All Rights Reserved
// 原博文地址: http://www.cnblogs.com/toutou/
// 作 者: 请叫我头头哥
//
//------------------------------------------------------------------------------
namespace CNBlogs.DesignPattern
{
using System;
using System.Configuration;
using System.Reflection;
using CNBlogs.DesignPattern.Common;
class Program
{
static void Main(string[] args)
{
// ***具体app.config配置如下*** //
//
//
//
// 创建一个工厂类的实例
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();
}
}
}
c# 设计模式 之:抽象工厂
标签:inf out type demo code set space sem type属性
原文地址:https://www.cnblogs.com/25miao/p/10389760.html
评论