using System; using System.Reflection; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //这样我湖南工厂换广州工厂改抽象工厂类的实例对象就可以了,对产品鸭架,鸭脖没影响 //AbstractFactory nanChangFactory = new GuanzhouFactory(); AbstractFactory nanChangFactory = new HuNanFactory(); YaBo _yabo = nanChangFactory.CreateYaBo(); YaJia _yajia = nanChangFactory.CreateYaJia(); _yabo.Make(); _yajia.Make(); Console.ReadLine(); } } /// <summary> /// 抽象工厂类,提供创建两个不同地方的鸭架和鸭脖的接口 /// </summary> public abstract class AbstractFactory { // 抽象工厂提供创建一系列产品的接口,这里作为例子,只给出了绝味中鸭脖和鸭架的创建接口 public abstract YaBo CreateYaBo(); public abstract YaJia CreateYaJia(); } /// <summary> /// 广州绝味工厂负责制作广州的鸭脖和鸭架 /// </summary> public class GuanzhouFactory : AbstractFactory { public override YaBo CreateYaBo() { return new GuanzhouYaBo(); } public override YaJia CreateYaJia() { return new GuanzhouYaJia(); } } /// <summary> /// 湖南绝味工厂负责制作湖南的鸭脖和鸭架 /// </summary> public class HuNanFactory : AbstractFactory { public override YaBo CreateYaBo() { return new HuNanYaBo(); } public override YaJia CreateYaJia() { return new HuNanYaJia(); } } /// <summary> /// 鸭架抽象类,供每个地方的鸭架类继承 /// </summary> public abstract class YaBo { public abstract void Make(); } /// <summary> /// 鸭架抽象类,供每个地方的鸭架类继承 /// </summary> public abstract class YaJia { public abstract void Make(); } /// <summary> /// 广州的鸭脖类,一般辣 /// </summary> public class GuanzhouYaBo : YaBo { public override void Make() { Console.WriteLine("广州的鸭脖"); } } /// <summary> /// 湖南的鸭脖类,比较辣 /// </summary> public class HuNanYaBo : YaBo { public override void Make() { Console.WriteLine("湖南的鸭脖"); } } /// <summary> /// 广州的鸭架类,一般辣 /// </summary> public class GuanzhouYaJia : YaJia { public override void Make() { Console.WriteLine("广州的鸭架"); } } /// <summary> /// 湖南的鸭架类,比较辣 /// </summary> public class HuNanYaJia : YaJia { public override void Make() { Console.WriteLine("湖南的鸭架"); } } }
时间: 2024-10-11 05:30:09