using System; namespace AbstractFactoryFunc{ public abstract class AbstractProductA{ public abstract void ProductAMethod(); } public class ProductA1 : AbstractProductA{ public override void ProductAMethod(){ Console.Write(this.ToString() + " : Method!"); Console.Read(); } } public class ProductA2 : AbstractProductA{ public override void ProductAMethod(){ Console.Write(this.ToString() + " : Method!"); Console.Read(); } } public abstract class AbstractProductB{ public abstract void ProductBMethod(); } public class ProductB1 : AbstractProductB{ public override void ProductBMethod(){ Console.Write(this.ToString() + " : Method!"); Console.Read(); } } public class ProductB2 : AbstractProductB{ public override void ProductBMethod(){ Console.Write(this.ToString() + " : Method!"); Console.Read(); } } public abstract class AbstractFactory{ public abstract AbstractProductA CreateProductA(); public abstract AbstractProductB CreateProductB(); } public class ConcreteFactory1 : AbstractFactory{ public override AbstractProductA CreateProductA() { return new ProductA1(); } public override AbstractProductB CreateProductB() { return new ProductB1(); } } public class ConcreteFactory2 : AbstractFactory{ public override AbstractProductA CreateProductA() { return new ProductA2(); } public override AbstractProductB CreateProductB() { return new ProductB2(); } } public class Client{ public AbstractProductA abstractProductA; public AbstractProductB abstractProductB; public Client(AbstractFactory abstractFactory){ abstractProductA = abstractFactory.CreateProductA(); abstractProductB = abstractFactory.CreateProductB(); } public void Run() { abstractProductA.ProductAMethod(); abstractProductB.ProductBMethod(); } } public class EntryPoint{ public static void Main(string[] args){ AbstractFactory factory1 = new ConcreteFactory1(); Client myClient = new Client(factory1); myClient.Run(); Console.Read(); Console.ReadKey(); } } }
转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/08/22/2651480.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/100850.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...