抽象工厂: interface IBag { int Price { get; } } interface IBrand { int Price { get; } string Material { get; } } class Nike : IBrand{ public int Price { get { return 1000; } } public string Material { get { return "Plastic"; } } } class Bag<Brand> : IBag where Brand : IBrand, new() { Brand brand; public Bag() { brand = new Brand(); } public int Price{ get { return brand.Price; } } } interface ICreate<Brand> where Brand : IBrand{ IBag CreateBag(); } class Create<Brand> : ICreate<Brand> where Brand : IBrand, new() { public IBag CreateBag(){ return new Bag<Brand>(); } } class Client<Brand> where Brand : IBrand, new(){ public void ClientMain(){ ICreate<Brand> create = new Create<Brand>(); IBag bag = create.CreateBag(); Console.WriteLine("I bought a bag which cost "+bag.Price); } } static class Program { static void Main(){ new Client<Nike>().ClientMain(); } } 生成器: interface IBag{ int Price { get; set; } } interface IBrand{ IBag CreateBag(); } class Nike : IBrand{ public IBag CreateBag(){ Bag b=new Bag(); Program.DoWork("Create a bag"); b.Price = 1000; return b; } } interface ICreate<Brand> where Brand:IBrand { IBag CreateBag(); } class Create<Brand> :ICreate<Brand> where Brand:IBrand,new (){ Brand brand; public Create(){ brand=new Brand(); } public IBag CreateBag() { return brand.CreateBag(); } } class Client<Brand> where Brand : IBrand, new() { public void ClientMain() { ICreate<Brand> create = new Create<Brand>(); IBag bag = create.CreateBag(); Console.WriteLine("I bought a bag witch cost "+bag.Price); } } class Bag : IBag{ public int Price { get; set; } } static class Program { static void Main(){ new Client<Nike>().ClientMain(); } public static void DoWork(string workitem){ Console.WriteLine(workitem); } }