抽象工厂模式
定义
public abstract class Yabo { public abstract void print(); } public class WuhanYabo : Yabo { public WuhanYabo() { Console.WriteLine("我是武汉鸭脖"); } public override void print() { Console.WriteLine("我是武汉鸭脖"); } } public class ShenzhenYabo : Yabo { public ShenzhenYabo() { Console.WriteLine("我是深圳鸭脖"); } public override void print() { Console.WriteLine("我是深圳鸭脖"); } } public abstract class Yajia { public abstract void print(); } public class WuhanYajia : Yajia { public WuhanYajia() { Console.WriteLine("我是武汉鸭架"); } public override void print() { Console.WriteLine("我是武汉鸭架"); } } public class ShenzhenYajia : Yajia { public ShenzhenYajia() { Console.WriteLine("我是深圳鸭架"); } public override void print() { Console.WriteLine("我是深圳鸭架"); } } public abstract class AbsFactory { public abstract Yabo CreateYabo(); public abstract Yajia CreateYajia(); } public class WuhanFactory : AbsFactory { public override Yajia CreateYajia() { return new WuhanYajia(); } public override Yabo CreateYabo() { return new WuhanYabo(); } } public class ShenzhenFactory : AbsFactory { public override Yajia CreateYajia() { return new ShenzhenYajia(); } public override Yabo CreateYabo() { return new ShenzhenYabo(); } }
调用
AbsFactory wuhanFactory=new WuhanFactory(); Yajia whyajia= wuhanFactory.CreateYajia(); Yabo whyabo=wuhanFactory.CreateYabo(); AbsFactory shenzhenFactory = new ShenzhenFactory(); Yajia szyajia = shenzhenFactory.CreateYajia(); Yabo szyabo = shenzhenFactory.CreateYabo();
喜欢 C#、SQL、Web