{"id":108,"date":"2008-03-08T13:27:31","date_gmt":"2008-03-08T13:27:31","guid":{"rendered":""},"modified":"2008-03-08T13:27:31","modified_gmt":"2008-03-08T13:27:31","slug":"108","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=108","title":{"rendered":"\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f(Abstract Factory Pattern)"},"content":{"rendered":"<p>\u4e00\u3001 \u62bd\u8c61\u5de5\u5382\uff08Abstract Factory\uff09\u6a21\u5f0f<br \/>\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u662f\u6240\u6709\u5f62\u6001\u7684\u5de5\u5382\u6a21\u5f0f\u4e2d\u6700\u4e3a\u62bd\u8c61\u548c\u6700\u5177\u4e00\u822c\u6027\u7684\u4e00\u79cd\u5f62\u6001\u3002<\/p>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u5f15\u8fdb\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\uff0c\u5f15\u8fdb\u4e00\u4e2a\u65b0\u6982\u5ff5\uff1a\u4ea7\u54c1\u65cf\uff08Product Family\uff09\u3002\u6240\u8c13\u4ea7\u54c1\u65cf\uff0c\u662f\u6307\u4f4d\u4e8e\u4e0d\u540c\u4ea7\u54c1\u7b49\u7ea7\u7ed3\u6784\uff0c\u529f\u80fd\u76f8\u5173\u8054\u7684\u4ea7\u54c1\u7ec4\u6210\u7684\u5bb6\u65cf\u3002\u5982\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/l200838132340.gif\" \/><br \/>\u56fe\u4e2d\u4e00\u5171\u6709\u56db\u4e2a\u4ea7\u54c1\u65cf\uff0c\u5206\u5e03\u4e8e\u4e09\u4e2a\u4e0d\u540c\u7684\u4ea7\u54c1\u7b49\u7ea7\u7ed3\u6784\u4e2d\u3002\u53ea\u8981\u6307\u660e\u4e00\u4e2a\u4ea7\u54c1\u6240\u5904\u7684\u4ea7\u54c1\u65cf\u4ee5\u53ca\u5b83\u6240\u5c5e\u7684\u7b49\u7ea7\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u552f\u4e00\u7684\u786e\u5b9a\u8fd9\u4e2a\u4ea7\u54c1\u3002<\/p>\n<p>\u5f15\u8fdb\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f<\/p>\n<p>\u6240\u8c13\u7684\u62bd\u8c61\u5de5\u5382\u662f\u6307\u4e00\u4e2a\u5de5\u5382\u7b49\u7ea7\u7ed3\u6784\u53ef\u4ee5\u521b\u5efa\u51fa\u5206\u5c5e\u4e8e\u4e0d\u540c\u4ea7\u54c1\u7b49\u7ea7\u7ed3\u6784\u7684\u4e00\u4e2a\u4ea7\u54c1\u65cf\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u3002\u5982\u679c\u7528\u56fe\u6765\u63cf\u8ff0\u7684\u8bdd\uff0c\u5982\u4e0b\u56fe\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/8200838132349.gif\" \/><br \/>\u4e8c\u3001 Abstract Factory\u6a21\u5f0f\u7684\u7ed3\u6784\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/6200838132423.gif\" \/><br \/>\u56fe\u4e2d\u63cf\u8ff0\u7684\u4e1c\u897f\u7528\u4ea7\u54c1\u65cf\u63cf\u8ff0\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/s200838132435.gif\" \/><br \/>\u62bd\u8c61\u5de5\u5382\uff08Abstract Factory\uff09\u89d2\u8272\uff1a\u62c5\u4efb\u8fd9\u4e2a\u89d2\u8272\u7684\u662f\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\u7684\u6838\u5fc3\uff0c\u5b83\u662f\u4e0e\u5e94\u7528\u7cfb\u7edf\u5546\u4e1a\u903b\u8f91\u65e0\u5173\u7684\u3002<\/p>\n<p>\u5177\u4f53\u5de5\u5382\uff08Concrete Factory\uff09\u89d2\u8272\uff1a\u8fd9\u4e2a\u89d2\u8272\u76f4\u63a5\u5728\u5ba2\u6237\u7aef\u7684\u8c03\u7528\u4e0b\u521b\u5efa\u4ea7\u54c1\u7684\u5b9e\u4f8b\u3002\u8fd9\u4e2a\u89d2\u8272\u542b\u6709\u9009\u62e9\u5408\u9002\u7684\u4ea7\u54c1\u5bf9\u8c61\u7684\u903b\u8f91\uff0c\u800c\u8fd9\u4e2a\u903b\u8f91\u662f\u4e0e\u5e94\u7528\u7cfb\u7edf\u7684\u5546\u4e1a\u903b\u8f91\u7d27\u5bc6\u76f8\u5173\u7684\u3002<\/p>\n<p>\u62bd\u8c61\u4ea7\u54c1\uff08Abstract Product\uff09\u89d2\u8272\uff1a\u62c5\u4efb\u8fd9\u4e2a\u89d2\u8272\u7684\u7c7b\u662f\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\u6240\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u7236\u7c7b\uff0c\u6216\u5b83\u4eec\u5171\u540c\u62e5\u6709\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u5177\u4f53\u4ea7\u54c1\uff08Concrete Product\uff09\u89d2\u8272\uff1a\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u6240\u521b\u5efa\u7684\u4efb\u4f55\u4ea7\u54c1\u5bf9\u8c61\u90fd\u662f\u67d0\u4e00\u4e2a\u5177\u4f53\u4ea7\u54c1\u7c7b\u7684\u5b9e\u4f8b\u3002\u8fd9\u662f\u5ba2\u6237\u7aef\u6700\u7ec8\u9700\u8981\u7684\u4e1c\u897f\uff0c\u5176\u5185\u90e8\u4e00\u5b9a\u5145\u6ee1\u4e86\u5e94\u7528\u7cfb\u7edf\u7684\u5546\u4e1a\u903b\u8f91\u3002<\/p>\n<p>\u4e09\u3001 \u7a0b\u5e8f\u4e3e\u4f8b\uff1a<br \/>\u8be5\u7a0b\u5e8f\u6f14\u793a\u4e86\u62bd\u8c61\u5de5\u5382\u7684\u7ed3\u6784\uff0c\u672c\u8eab\u4e0d\u5177\u6709\u4efb\u4f55\u5b9e\u9645\u4ef7\u503c\u3002<br \/><code><br \/>\/\/ Abstract Factory pattern -- Structural example&nbsp;&nbsp;<br \/>using System;<\/p>\n<p>\/\/ \"AbstractFactory\"<br \/>abstract class AbstractFactory<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public AbstractProductA CreateProductA();<br \/>&nbsp;&nbsp;abstract public AbstractProductB CreateProductB();<br \/>}<\/p>\n<p>\/\/ \"ConcreteFactory1\"<br \/>class ConcreteFactory1 : AbstractFactory<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public AbstractProductA CreateProductA()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return new ProductA1();<br \/>&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;override public AbstractProductB CreateProductB()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return new ProductB1();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ConcreteFactory2\"<br \/>class ConcreteFactory2 : AbstractFactory<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public AbstractProductA CreateProductA()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return new ProductA2();<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;override public AbstractProductB CreateProductB()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return new ProductB2();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"AbstractProductA\"<br \/>abstract class AbstractProductA<br \/>{<br \/>}<\/p>\n<p>\/\/ \"AbstractProductB\"<br \/>abstract class AbstractProductB<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public void Interact( AbstractProductA a );<br \/>}<\/p>\n<p>\/\/ \"ProductA1\"<br \/>class ProductA1 : AbstractProductA<br \/>{<br \/>}<\/p>\n<p>\/\/ \"ProductB1\"<br \/>class ProductB1 : AbstractProductB<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void Interact( AbstractProductA a )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( this + \" interacts with \" + a );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ProductA2\"<br \/>class ProductA2 : AbstractProductA<br \/>{<br \/>}<\/p>\n<p>\/\/ \"ProductB2\"<br \/>class ProductB2 : AbstractProductB<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void Interact( AbstractProductA a )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( this + \" interacts with \" + a );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"Client\" - the interaction environment of the products<br \/>class Environment<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;private AbstractProductA AbstractProductA;<br \/>&nbsp;&nbsp;private AbstractProductB AbstractProductB;<\/p>\n<p>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public Environment( AbstractFactory factory )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;AbstractProductB = factory.CreateProductB();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;AbstractProductA = factory.CreateProductA();<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public void Run()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;AbstractProductB.Interact( AbstractProductA );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/**\/\/\/\/ &lt;summary&gt;<br \/>\/\/\/ ClientApp test environment<br \/>\/\/\/ &lt;\/summary&gt;<br \/>class ClientApp<br \/>{<br \/>&nbsp;&nbsp;public static void Main(string[] args)<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;AbstractFactory factory1 = new ConcreteFactory1();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Environment e1 = new Environment( factory1 );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;e1.Run();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;AbstractFactory factory2 = new ConcreteFactory2();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Environment e2 = new Environment( factory2 );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;e2.Run();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code> <\/p>\n<p>\u56db\u3001 \u5728\u4ec0\u4e48\u60c5\u5f62\u4e0b\u4f7f\u7528\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\uff1a<br \/>\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u5e94\u5f53\u8003\u8651\u4f7f\u7528\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\uff1a<\/p>\n<p>\u4e00\u4e2a\u7cfb\u7edf\u4e0d\u5e94\u5f53\u4f9d\u8d56\u4e8e\u4ea7\u54c1\u7c7b\u5b9e\u4f8b\u5982\u4f55\u88ab\u521b\u5efa\u3001\u7ec4\u5408\u548c\u8868\u8fbe\u7684\u7ec6\u8282\uff0c\u8fd9\u5bf9\u4e8e\u6240\u6709\u5f62\u6001\u7684\u5de5\u5382\u6a21\u5f0f\u90fd\u662f\u91cd\u8981\u7684\u3002 <br \/>\u8fd9\u4e2a\u7cfb\u7edf\u6709\u591a\u4e8e\u4e00\u4e2a\u7684\u4ea7\u54c1\u65cf\uff0c\u800c\u7cfb\u7edf\u53ea\u6d88\u8d39\u5176\u4e2d\u67d0\u4e00\u4ea7\u54c1\u65cf\u3002 <br \/>\u540c\u5c5e\u4e8e\u540c\u4e00\u4e2a\u4ea7\u54c1\u65cf\u7684\u4ea7\u54c1\u662f\u5728\u4e00\u8d77\u4f7f\u7528\u7684\uff0c\u8fd9\u4e00\u7ea6\u675f\u5fc5\u987b\u5728\u7cfb\u7edf\u7684\u8bbe\u8ba1\u4e2d\u4f53\u73b0\u51fa\u6765\u3002 <br \/>\u7cfb\u7edf\u63d0\u4f9b\u4e00\u4e2a\u4ea7\u54c1\u7c7b\u7684\u5e93\uff0c\u6240\u6709\u7684\u4ea7\u54c1\u4ee5\u540c\u6837\u7684\u63a5\u53e3\u51fa\u73b0\uff0c\u4ece\u800c\u4f7f\u5ba2\u6237\u7aef\u4e0d\u4f9d\u8d56\u4e8e\u5b9e\u73b0\u3002<\/p>\n<p>\u4e94\u3001 \u62bd\u8c61\u5de5\u5382\u7684\u8d77\u6e90<br \/>\u636e\u8bf4\u6700\u65e9\u7684\u5e94\u7528\u662f\u7528\u6765\u521b\u5efa\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u89c6\u7a97\u73af\u5883\u4e0b\u90fd\u80fd\u591f\u8fd0\u884c\u7684\u7cfb\u7edf\u3002\u6bd4\u5982\u5728Windows\u4e0eUnix\u7cfb\u7edf\u4e0b\u90fd\u6709\u89c6\u7a97\u73af\u5883\u7684\u6784\u4ef6\uff0c\u5728\u6bcf\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u90fd\u6709\u4e00\u4e2a\u89c6\u7a97\u6784\u4ef6\u7ec4\u6210\u7684\u6784\u4ef6\u5bb6\u65cf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u62bd\u8c61\u89d2\u8272\u7ed9\u51fa\u529f\u80fd\u63cf\u8ff0\uff0c\u800c\u7531\u5177\u4f53\u5b50\u7c7b\u7ed9\u51fa\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u5982\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/v200838132514.gif\" \/><br \/>\u53ef\u4ee5\u53d1\u73b0\u4e0a\u9762\u4ea7\u54c1\u7c7b\u56fe\u6709\u4e24\u4e2a\u4ea7\u54c1\u7b49\u7ea7\u7ed3\u6784\uff0c\u5206\u522b\u662fButton\u4e0eText\uff1b\u540c\u65f6\u6709\u4e24\u4e2a\u4ea7\u54c1\u65cf\uff1aUnix\u4ea7\u54c1\u65cf\u4e0eWindows\u4ea7\u54c1\u65cf\u3002<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/c200838132548.gif\" \/><br \/>\u7cfb\u7edf\u5bf9\u4ea7\u54c1\u5bf9\u8c61\u7684\u521b\u5efa\u8981\u6c42\u7531\u4e00\u4e2a\u5de5\u5382\u7684\u7b49\u7ea7\u7ed3\u6784\u6ee1\u8db3\u3002\u5176\u4e2d\u6709\u4e24\u4e2a\u5177\u4f53\u5de5\u5382\u89d2\u8272\uff0c\u5373UnixFactory\u548cWinFactory\u3002UnixFactory\u5bf9\u8c61\u8d1f\u8d23\u521b\u5efaUnix\u4ea7\u54c1\u65cf\u4e2d\u7684\u4ea7\u54c1\uff0c\u800cWinFactory\u8d1f\u8d23\u521b\u5efaWindows\u4ea7\u54c1\u65cf\u4e2d\u7684\u4ea7\u54c1\u3002<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/z200838132556.gif\" \/><br \/>\u663e\u7136\u4e00\u4e2a\u7cfb\u7edf\u53ea\u80fd\u591f\u5728\u67d0\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u7684\u89c6\u7a97\u73af\u5883\u4e0b\u8fd0\u884c\uff0c\u800c\u4e0d\u80fd\u540c\u65f6\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\u6240\u4ee5\uff0c\u7cfb\u7edf\u5b9e\u9645\u4e0a\u53ea\u80fd\u6d88\u8d39\u5c5e\u4e8e\u540c\u4e00\u4e2a\u4ea7\u54c1\u65cf\u7684\u4ea7\u54c1\u3002<\/p>\n<p>\u5728\u73b0\u4ee3\u7684\u5e94\u7528\u4e2d\uff0c\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u7684\u4f7f\u7528\u8303\u56f4\u5df2\u7ecf\u5927\u5927\u6269\u5927\u4e86\uff0c\u4e0d\u518d\u8981\u6c42\u7cfb\u7edf\u53ea\u80fd\u6d88\u8d39\u67d0\u4e00\u4e2a\u4ea7\u54c1\u65cf\u4e86\u3002<\/p>\n<p>\u516d\u3001 Abstract Factory\u6a21\u5f0f\u5728\u5b9e\u9645\u7cfb\u7edf\u4e2d\u7684\u5b9e\u73b0<br \/>Herbivore\uff1a\u8349\u98df\u52a8\u7269<br \/>Carnivore\uff1a\u98df\u8089\u52a8\u7269<br \/>Bison\uff1a[&#8216;baisn]\uff0c\u7f8e\u6d32\u6216\u6b27\u6d32\u7684\u91ce\u725b<\/p>\n<p>\u4e0b\u9762\u5b9e\u9645\u4ee3\u7801\u6f14\u793a\u4e86\u4e00\u4e2a\u7535\u8111\u6e38\u620f\u4e2d\u521b\u5efa\u4e0d\u540c\u52a8\u7269\u7684\u62bd\u8c61\u5de5\u5382\u3002\u5c3d\u7ba1\u5728\u4e0d\u540c\u5927\u9646\u4e0b\u52a8\u7269\u7269\u79cd\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u4f46\u52a8\u7269\u95f4\u7684\u5173\u7cfb\u4ecd\u7136\u4fdd\u7559\u4e86\u4e0b\u6765\u3002<br \/><code>\/\/ Abstract Factory pattern -- Real World example&nbsp;&nbsp;<br \/>using System;<\/p>\n<p>\/\/ \"AbstractFactory\"<br \/>abstract class ContinentFactory<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public Herbivore CreateHerbivore();<br \/>&nbsp;&nbsp;abstract public Carnivore CreateCarnivore();<br \/>}<\/p>\n<p>\/\/ \"ConcreteFactory1\"<br \/>class AfricaFactory : ContinentFactory<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public Herbivore CreateHerbivore()<br \/>&nbsp;&nbsp;{ return new Wildebeest(); }<\/p>\n<p>&nbsp;&nbsp;override public Carnivore CreateCarnivore()<br \/>&nbsp;&nbsp;{ return new Lion(); }<br \/>}<\/p>\n<p>\/\/ \"ConcreteFactory2\"<br \/>class AmericaFactory : ContinentFactory<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public Herbivore CreateHerbivore()<br \/>&nbsp;&nbsp;{ return new Bison(); }<\/p>\n<p>&nbsp;&nbsp;override public Carnivore CreateCarnivore()<br \/>&nbsp;&nbsp;{ return new Wolf(); }<br \/>}<\/p>\n<p>\/\/ \"AbstractProductA\"<br \/>abstract class Herbivore<br \/>{<br \/>}<\/p>\n<p>\/\/ \"AbstractProductB\"<br \/>abstract class Carnivore<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public void Eat( Herbivore h );<br \/>}<\/p>\n<p>\/\/ \"ProductA1\"<br \/>class Wildebeest : Herbivore<br \/>{<br \/>}<\/p>\n<p>\/\/ \"ProductB1\"<br \/>class Lion : Carnivore<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void Eat( Herbivore h )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ eat wildebeest<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( this + \" eats \" + h );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ProductA2\"<br \/>class Bison : Herbivore<br \/>{<br \/>}<\/p>\n<p>\/\/ \"ProductB2\"<br \/>class Wolf : Carnivore<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void Eat( Herbivore h )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Eat bison<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( this + \" eats \" + h );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"Client\"<br \/>class AnimalWorld<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;private Herbivore herbivore;<br \/>&nbsp;&nbsp;private Carnivore carnivore;<\/p>\n<p>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public AnimalWorld( ContinentFactory factory )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;carnivore = factory.CreateCarnivore();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;herbivore = factory.CreateHerbivore();<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public void RunFoodChain()<br \/>&nbsp;&nbsp;{ carnivore.Eat(herbivore); }<br \/>}<\/p>\n<p>\/**\/\/\/\/ &lt;summary&gt;<br \/>\/\/\/&nbsp;&nbsp;GameApp test class<br \/>\/\/\/ &lt;\/summary&gt;<br \/>class GameApp<br \/>{<br \/>&nbsp;&nbsp;public static void Main( string[] args )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Create and run the Africa animal world<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ContinentFactory africa = new AfricaFactory();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;AnimalWorld world = new AnimalWorld( africa );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;world.RunFoodChain();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Create and run the America animal world<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ContinentFactory america = new AmericaFactory();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;world = new AnimalWorld( america );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;world.RunFoodChain();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code> <\/p>\n<p>\u62bd\u8c61\u5de5\u5382\u7684\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/e200838132624.gif\" \/><br \/>\u5982\u4f55\u8bbe\u8ba1\u62bd\u8c61\u7c7b\u5de5\u5382\u7559\u4f5c\u601d\u8003\u3002<\/p>\n<p>\u4e03\u3001 &#8220;\u5f00\u653e\uff0d\u5c01\u95ed&#8221;\u539f\u5219<br \/>&#8220;\u5f00\u653e\uff0d\u5c01\u95ed&#8221;\u539f\u5219\u8981\u6c42\u7cfb\u7edf\u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5c01\u95ed\u3002\u901a\u8fc7\u6269\u5c55\u8fbe\u5230\u589e\u5f3a\u5176\u529f\u80fd\u7684\u76ee\u7684\u3002\u5bf9\u4e8e\u6d89\u53ca\u5230\u591a\u4e2a\u4ea7\u54c1\u65cf\u4e0e\u591a\u4e2a\u4ea7\u54c1\u7b49\u7ea7\u7ed3\u6784\u7684\u7cfb\u7edf\uff0c\u5176\u529f\u80fd\u589e\u5f3a\u5305\u62ec\u4e24\u65b9\u9762\uff1a<\/p>\n<p>\u589e\u52a0\u4ea7\u54c1\u65cf\uff1aAbstract Factory\u5f88\u597d\u7684\u652f\u6301\u4e86&#8221;\u5f00\u653e\uff0d\u5c01\u95ed&#8221;\u539f\u5219\u3002<\/p>\n<p>\u589e\u52a0\u65b0\u4ea7\u54c1\u7684\u7b49\u7ea7\u7ed3\u6784\uff1a\u9700\u8981\u4fee\u6539\u6240\u6709\u7684\u5de5\u5382\u89d2\u8272\uff0c\u6ca1\u6709\u5f88\u597d\u652f\u6301&#8221;\u5f00\u653e\uff0d\u5c01\u95ed&#8221;\u539f\u5219\u3002<\/p>\n<p>\u7efc\u5408\u8d77\u6765\uff0c\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u4ee5\u4e00\u79cd\u503e\u659c\u7684\u65b9\u5f0f\u652f\u6301\u589e\u52a0\u65b0\u7684\u4ea7\u54c1\uff0c\u5b83\u4e3a\u65b0\u4ea7\u54c1\u65cf\u7684\u589e\u52a0\u63d0\u4f9b\u65b9\u4fbf\uff0c\u800c\u4e0d\u80fd\u4e3a\u65b0\u7684\u4ea7\u54c1\u7b49\u7ea7\u7ed3\u6784\u7684\u589e\u52a0\u63d0\u4f9b\u8fd9\u6837\u7684\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 \u62bd\u8c61\u5de5\u5382\uff08Abstract Factory\uff09\u6a21\u5f0f\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u662f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-Note"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}