// 抽象工厂设计模式 如果有新的餐厅的加入 只需要在新建个Restaurant继承 的方法及他们的子类方法·
using
System;
using System.Reflection;
// 简单抽象工厂模式
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 餐厅类抽象工厂
/// </summary>
public abstract class Restaurant
...{
/**//// <summary>
/// 创建一个盘子类
/// </summary>
/// <returns>盘子</returns>
public abstract Plate CreatePlate();
/**//// <summary>
/// 创建一个鸡腿类
/// </summary>
/// <returns>鸡腿</returns>
public abstract Drumstick CreateDrumstick();
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 肯德基餐厅
/// </summary>
public class KFC : Restaurant
...{
public KFC()
...{ }
/**//// <summary>
/// 创建一个肯德基的盘子
/// </summary>
/// <returns>肯德基盘子</returns
public override Plate CreatePlate()
...{
return new KFCPlate();
}
/**//// <summary>
/// 创建一个肯德基鸡腿
/// </summary>
/// <returns>肯德基鸡腿</returns>
public override Drumstick CreateDrumstick()
...{
return new KFCDrumstick();
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 麦当劳餐厅
/// </summary>
public class MDL : Restaurant
...{
/**//// <summary>
/// 创建一个麦当劳盘子
/// </summary>
/// <returns>麦当劳盘子</returns>
public override Plate CreatePlate()
...{
return new MDLPlate();
}
/**//// <summary>
/// 创建一个麦当劳鸡腿
/// </summary>
/// <returns>麦当劳鸡腿</returns>
public override Drumstick CreateDrumstick()
...{
return new MDLDrumstick();
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 盘子产品父类
/// </summary>
public abstract class Plate
...{
/**//// <summary>
/// 盘子装载方法
/// </summary>
/// <returns></returns>
public abstract string Load();
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 肯德基的盘子
/// </summary>
public class KFCPlate : Plate
...{
public override string Load()
...{
return "肯德基的盘子装";
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 麦当劳的盘子
/// </summary>
public class MDLPlate : Plate
...{
public MDLPlate() ...{ }
public override string Load()
...{
return "麦当劳的盘子装";
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 鸡腿产品父类
/// </summary>
public abstract class Drumstick
...{
public abstract string Name ...{ get;}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 肯德基鸡腿
/// </summary>
public class KFCDrumstick : Drumstick
...{
public override string Name
...{
get ...{ return "肯德基的鸡腿"; }
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 麦当劳鸡腿
/// </summary>
public class MDLDrumstick : Drumstick
...{
public override string Name
...{
get ...{ return "麦当劳的鸡腿"; }
}
}
}
// namespace Jurassic.Training.AbstractFactory
// {
//
// // / <summary>
// // / 女友,模拟客户类
// // / </summary>
// public class Girlfriend
// {
// public static void Main()
// {
// // 走进肯德基
// Restaurant restaurant = new KFC();
// GoRestaurant(restaurant);
// Console.ReadLine();
// }
// public static void GoRestaurant(Restaurant restaurant)
// {
// Plate p = restaurant.CreatePlate();
// Drumstick d = restaurant.CreateDrumstick();
// Console.WriteLine(p.Load() + d.Name);
// }
// }
// }
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 全聚德餐厅
/// </summary>
public class QJD : Restaurant
...{
/**//// <summary>
/// 创建全聚德盘子
/// </summary>
/// <returns></returns>
public override Plate CreatePlate()
...{
return new QJDPlate();
}
/**//// <summary>
/// 创建全聚德鸡腿
/// </summary>
/// <returns></returns>
public override Drumstick CreateDrumstick()
...{
return new QJDDrumstick();
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 全聚德盘子
/// </summary>
public class QJDPlate : Plate
...{
public override string Load()
...{
return "全聚德的盘子装";
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 全聚德鸡腿
/// </summary>
public class QJDDrumstick : Drumstick
...{
public override string Name
...{
get ...{ return "全聚德鸡腿"; }
}
}
}
// namespace Jurassic.Training.AbstractFactory
// {
// public class Restaurant
// {
// private string _plateFile;
// private string _drumstickFile;
// private string _plate;
// private string _drumstick;
// public Restaurant(string type)
// {
// _plateFile = string.Format("{0}Plate.dll", type);
// _drumstickFile = string.Format("{0}Drumstick.dll", type);
// _plate = string.Format("Jurassic.Training.AbstractFactory.{0}Plate", type);
// _drumstick = string.Format("Jurassic.Training.AbstractFactory.{0}Drumstick", type);
// }
// public Plate CreatePlate()
// {
// Plate plate = Helper(_plateFile, _plate) as Plate;
// return plate;
// }
// public Drumstick CreateDrumstick()
// {
// Drumstick drumstick = Helper(_drumstickFile, _drumstick) as Drumstick;
// return drumstick;
// }
//
// // / <summary>
// // / 根据程序集文件名称和类型实例化类型
// // / </summary>
// // / <param name="fileName"></param>
// // / <param name="type"></param>
// // / <returns></returns>
// private object Helper(string fileName, string type)
// {
// Assembly asm = Assembly.LoadFrom(fileName);
// object o = asm.CreateInstance(type);
// return o;
// }
// }
// }
namespace Jurassic.Training.AbstractFactory
... {
public class Girlfriend
...{
public static void Main()
...{
//走进肯德基
Restaurant Krestaurant = new KFC();
GoRestaurant(Krestaurant);
//走进麦当劳
Restaurant Mrestaurant = new MDL();
GoRestaurant(Mrestaurant);
//走进全聚德
Restaurant restaurant = new QJD();
GoRestaurant(restaurant);
Console.ReadLine();
}
public static void GoRestaurant(Restaurant restaurant)
...{
Plate p = restaurant.CreatePlate();
Drumstick d = restaurant.CreateDrumstick();
Console.WriteLine(p.Load() + d.Name);
}
}
}
using System.Reflection;
// 简单抽象工厂模式
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 餐厅类抽象工厂
/// </summary>
public abstract class Restaurant
...{
/**//// <summary>
/// 创建一个盘子类
/// </summary>
/// <returns>盘子</returns>
public abstract Plate CreatePlate();
/**//// <summary>
/// 创建一个鸡腿类
/// </summary>
/// <returns>鸡腿</returns>
public abstract Drumstick CreateDrumstick();
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 肯德基餐厅
/// </summary>
public class KFC : Restaurant
...{
public KFC()
...{ }
/**//// <summary>
/// 创建一个肯德基的盘子
/// </summary>
/// <returns>肯德基盘子</returns
public override Plate CreatePlate()
...{
return new KFCPlate();
}
/**//// <summary>
/// 创建一个肯德基鸡腿
/// </summary>
/// <returns>肯德基鸡腿</returns>
public override Drumstick CreateDrumstick()
...{
return new KFCDrumstick();
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 麦当劳餐厅
/// </summary>
public class MDL : Restaurant
...{
/**//// <summary>
/// 创建一个麦当劳盘子
/// </summary>
/// <returns>麦当劳盘子</returns>
public override Plate CreatePlate()
...{
return new MDLPlate();
}
/**//// <summary>
/// 创建一个麦当劳鸡腿
/// </summary>
/// <returns>麦当劳鸡腿</returns>
public override Drumstick CreateDrumstick()
...{
return new MDLDrumstick();
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 盘子产品父类
/// </summary>
public abstract class Plate
...{
/**//// <summary>
/// 盘子装载方法
/// </summary>
/// <returns></returns>
public abstract string Load();
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 肯德基的盘子
/// </summary>
public class KFCPlate : Plate
...{
public override string Load()
...{
return "肯德基的盘子装";
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 麦当劳的盘子
/// </summary>
public class MDLPlate : Plate
...{
public MDLPlate() ...{ }
public override string Load()
...{
return "麦当劳的盘子装";
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 鸡腿产品父类
/// </summary>
public abstract class Drumstick
...{
public abstract string Name ...{ get;}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 肯德基鸡腿
/// </summary>
public class KFCDrumstick : Drumstick
...{
public override string Name
...{
get ...{ return "肯德基的鸡腿"; }
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 麦当劳鸡腿
/// </summary>
public class MDLDrumstick : Drumstick
...{
public override string Name
...{
get ...{ return "麦当劳的鸡腿"; }
}
}
}
// namespace Jurassic.Training.AbstractFactory
// {
//
// // / <summary>
// // / 女友,模拟客户类
// // / </summary>
// public class Girlfriend
// {
// public static void Main()
// {
// // 走进肯德基
// Restaurant restaurant = new KFC();
// GoRestaurant(restaurant);
// Console.ReadLine();
// }
// public static void GoRestaurant(Restaurant restaurant)
// {
// Plate p = restaurant.CreatePlate();
// Drumstick d = restaurant.CreateDrumstick();
// Console.WriteLine(p.Load() + d.Name);
// }
// }
// }
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 全聚德餐厅
/// </summary>
public class QJD : Restaurant
...{
/**//// <summary>
/// 创建全聚德盘子
/// </summary>
/// <returns></returns>
public override Plate CreatePlate()
...{
return new QJDPlate();
}
/**//// <summary>
/// 创建全聚德鸡腿
/// </summary>
/// <returns></returns>
public override Drumstick CreateDrumstick()
...{
return new QJDDrumstick();
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 全聚德盘子
/// </summary>
public class QJDPlate : Plate
...{
public override string Load()
...{
return "全聚德的盘子装";
}
}
}
namespace Jurassic.Training.AbstractFactory
... {
/**//// <summary>
/// 全聚德鸡腿
/// </summary>
public class QJDDrumstick : Drumstick
...{
public override string Name
...{
get ...{ return "全聚德鸡腿"; }
}
}
}
// namespace Jurassic.Training.AbstractFactory
// {
// public class Restaurant
// {
// private string _plateFile;
// private string _drumstickFile;
// private string _plate;
// private string _drumstick;
// public Restaurant(string type)
// {
// _plateFile = string.Format("{0}Plate.dll", type);
// _drumstickFile = string.Format("{0}Drumstick.dll", type);
// _plate = string.Format("Jurassic.Training.AbstractFactory.{0}Plate", type);
// _drumstick = string.Format("Jurassic.Training.AbstractFactory.{0}Drumstick", type);
// }
// public Plate CreatePlate()
// {
// Plate plate = Helper(_plateFile, _plate) as Plate;
// return plate;
// }
// public Drumstick CreateDrumstick()
// {
// Drumstick drumstick = Helper(_drumstickFile, _drumstick) as Drumstick;
// return drumstick;
// }
//
// // / <summary>
// // / 根据程序集文件名称和类型实例化类型
// // / </summary>
// // / <param name="fileName"></param>
// // / <param name="type"></param>
// // / <returns></returns>
// private object Helper(string fileName, string type)
// {
// Assembly asm = Assembly.LoadFrom(fileName);
// object o = asm.CreateInstance(type);
// return o;
// }
// }
// }
namespace Jurassic.Training.AbstractFactory
... {
public class Girlfriend
...{
public static void Main()
...{
//走进肯德基
Restaurant Krestaurant = new KFC();
GoRestaurant(Krestaurant);
//走进麦当劳
Restaurant Mrestaurant = new MDL();
GoRestaurant(Mrestaurant);
//走进全聚德
Restaurant restaurant = new QJD();
GoRestaurant(restaurant);
Console.ReadLine();
}
public static void GoRestaurant(Restaurant restaurant)
...{
Plate p = restaurant.CreatePlate();
Drumstick d = restaurant.CreateDrumstick();
Console.WriteLine(p.Load() + d.Name);
}
}
}