void Main() { AbstractFactory.ChooseFactory("A").GetProduct1().Dump(); AbstractFactory.ChooseFactory("A").GetProduct2().Dump(); AbstractFactory.ChooseFactory("B").GetProduct1().Dump(); AbstractFactory.ChooseFactory("B").GetProduct2().Dump(); } interface IProduct1{} interface IProduct2{} class Product1A:IProduct1{} class Product1B:IProduct1{} class Product2A:IProduct2{} class Product2B:IProduct2{} abstract class AbstractFactory{ public static AbstractFactory ChooseFactory(string name){ switch (name) { case "A":return new FactoryA(); case "B":return new FactoryB(); default:return null; } } public abstract IProduct1 GetProduct1(); public abstract IProduct2 GetProduct2(); } class FactoryA:AbstractFactory{ public override IProduct1 GetProduct1(){return new Product1A();} public override IProduct2 GetProduct2(){return new Product2A();} } class FactoryB:AbstractFactory { public override IProduct1 GetProduct1(){return new Product1B();} public override IProduct2 GetProduct2(){return new Product2B();} }
时间: 2024-10-13 06:19:21