// "Handler" using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace ConsoleApplication3 { abstract class Factory { abstract public ProductA ProductA(); abstract public ProductB ProductB(); } class WindowFactory : Factory { public override ProductA ProductA() { return new WindowProductA(); } public override ProductB ProductB() { return new WindowProductB(); } } class LinuxFactory : Factory { public override ProductA ProductA() { return new LinuxProductA(); } public override ProductB ProductB() { return new LinuxProductB(); } } abstract class ProductA { } abstract class ProductB { } class WindowProductA : ProductA { public WindowProductA() { Console.WriteLine("WindowProductA"); } } class WindowProductB : ProductB { public WindowProductB() { Console.WriteLine("WindowProductB"); } } class LinuxProductA : ProductA { public LinuxProductA() { Console.WriteLine("LinuxProductA"); } } class LinuxProductB : ProductB { public LinuxProductB() { Console.WriteLine("LinuxProductB"); } } class Program { public static void Main(string[] args) { string factoryName = System.Configuration.ConfigurationSettings.AppSettings["Factory"]; Factory aFactory = (Factory)Assembly.GetExecutingAssembly().CreateInstance("ConsoleApplication3."+factoryName); ProductA SystemA = aFactory.ProductA(); ProductB SystemB = aFactory.ProductB(); Console.ReadKey(); } } }
App.config中的配置:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Factory" value="LinuxFactory"/> </appSettings> </configuration>

浙公网安备 33010602011771号