标签:
意图
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类
实现
工厂类
/// <summary> /// 美食工厂 /// </summary> public abstract class CateFactory { public abstract HuoGuo GetHuoGuo(); public abstract XiaoChi GetXiaoChi(); }
public class ChongQingCateFactory : CateFactory { public override HuoGuo GetHuoGuo() { return new ChongQingHuoGuo(); } public override XiaoChi GetXiaoChi() { return new ChongQingXiaoChi(); } }
public class NanChangCateFactory : CateFactory { public override HuoGuo GetHuoGuo() { return new NanChangHuoGuo(); } public override XiaoChi GetXiaoChi() { return new NanChangXiaoChi(); } }
美食-火锅
/// <summary> /// 火锅 /// </summary> public abstract class HuoGuo { public abstract void show(); }
public class ChongQingHuoGuo : HuoGuo { public override void show() { Console.WriteLine("重庆火锅"); } }
public class NanChangHuoGuo : HuoGuo { public override void show() { Console.WriteLine("南昌火锅"); } }
美食-小吃
/// <summary> /// 小吃 /// </summary> public abstract class XiaoChi { public abstract void Show(); }
public class ChongQingXiaoChi : XiaoChi { public override void Show() { Console.WriteLine("重庆小吃"); } }
public class NanChangXiaoChi : XiaoChi { public override void Show() { Console.WriteLine("南昌小吃"); } }
客户端
class Program { static void Main(string[] args) { CateFactory factory = new ChongQingCateFactory(); factory.GetHuoGuo().show(); factory.GetXiaoChi().Show(); Console.ReadLine(); } }
标签:
原文地址:http://www.cnblogs.com/Jabben/p/5536507.html