说明:使用抽象类的继承和方法返回值,使之可以在产品上,工厂上无限扩展
大致代码
public abstract class AbsProduct { public abstract void Exc(); } class ProductA1 : AbsProduct { public override void Exc() { Console.WriteLine("A1"); } } class ProductB1 : AbsProduct { public override void Exc() { Console.WriteLine("B1"); } } public abstract class AbsCreator { public abstract AbsProduct GetProduct(string str); } public class CreatorA : AbsCreator { public override AbsProduct GetProduct(string str) { if(str=="a") { return new ProductA1(); } return null; } } public class CreatorB : AbsCreator { public override AbsProduct GetProduct(string str) { if(str=="b") { return new ProductB1(); } return null; } } AbsCreator c1 = new CreatorA(); AbsCreator c2 = new CreatorB(); c1.GetProduct("a").Exc(); c2.GetProduct("b").Exc();