{"id":1083,"date":"2021-10-10T23:27:15","date_gmt":"2021-10-10T15:27:15","guid":{"rendered":"http:\/\/www.mustenaka.cn\/?p=1083"},"modified":"2021-10-11T00:18:29","modified_gmt":"2021-10-10T16:18:29","slug":"csharpfactorypatternnote","status":"publish","type":"post","link":"https:\/\/www.mustenaka.cn\/index.php\/2021\/10\/10\/csharpfactorypatternnote\/","title":{"rendered":"C# \u5de5\u5382\u6a21\u5f0f\uff08\u8bbe\u8ba1\u6a21\u5f0f\u7b14\u8bb0\uff09"},"content":{"rendered":"<h1>C#\u5de5\u5382\u6a21\u5f0f<\/h1>\n<h3>C# \u5de5\u5382\u6a21\u5f0f\u7b80\u4ecb<\/h3>\n<p>C# \u5de5\u5382\u6a21\u5f0f(Factory Pattern)\u662f\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u5e38\u7528\u7684\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f(Design Patterns)\uff0c\u5c5e\u4e8e\u521b\u5efa\u578b\u6a21\u5f0f(<span>Creational pattern<\/span>)\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2d\u5728\u9762\u5bf9\u62e5\u6709\u5171\u540c\u7279\u8d28\u7684\u5bf9\u8c61\u7684\u4e00\u4e2d\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u6700\u4f73\u65b9\u5f0f\u3002<\/p>\n<p>\u6ce8\uff1a\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u5206\u4e3a\uff1a\u521b\u5efa\u578b\u6a21\u5f0f\uff08Creational Patterns\uff09\uff0c\u7ed3\u6784\u578b\u6a21\u5f0f\uff08Structural Patterns\uff09\uff0c\u884c\u4e3a\u6a21\u5f0f\uff08Behavioral Patterns\uff09\uff0cJ2EE\u6a21\u5f0f\uff08J2EE Patterns\uff09\uff0c\u5176\u8bbe\u8ba1\u7406\u5ff5\u4e3a\uff1a<\/p>\n<ol>\n<li><strong>Creational Patterns:<\/strong><span>\u00a0<\/span>These design patterns provide ways to create objects while hiding the<span>\u00a0<\/span><em>creation logic<\/em>, instead of instantiating objects directly using the<span>\u00a0<\/span><code>new<\/code><span>\u00a0<\/span>operator. This gives the program more flexibility in deciding which objects need to be created for a given<span>\u00a0<\/span><em>use case<\/em>.<\/li>\n<li><strong>Structural Patterns:<\/strong><span>\u00a0<\/span>These design patterns deal with class and object composition. The concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionality.<\/li>\n<li><strong>Behavioral Patterns:<\/strong><span>\u00a0<\/span>These design patterns are specifically concerned with communication between objects.<\/li>\n<li><strong>J2EE Patterns:<\/strong><span>\u00a0<\/span>These design patterns are specifically concerned with the presentation tier. These patterns were formerly identified by Sun Java now Oracle.<\/li>\n<\/ol>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u521b\u5efa\u578b\u6a21\u5f0f\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u7684\u65f6\u5019\u901a\u8fc7\u9690\u85cf\u521b\u5efa\u65b9\u6cd5\uff0c\u4e0d\u66b4\u9732\u521b\u5efa\u7684\u903b\u8f91\uff0c\u4e0d\u662f\u7b80\u5355\u7684\u901a\u8fc7new\u6307\u5411\u65b0\u7c7b\u578b\u3002\u800c\u5bf9\u4e8e\u5de5\u5382\u6a21\u5f0f\u800c\u8a00\uff0c\u5728\u521b\u5efa\u7684\u65f6\u5019\u5bf9\u4e8e\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u901a\u8fc7\u4e00\u4e2a\u5171\u540c\u7684\u63a5\u53e3\u6216\u8005\u62bd\u8c61\u7c7b\u7684\u65b9\u5f0f\u6765\u6307\u5411\u65b0\u7684\u5bf9\u8c61\u521b\u5efa\u3002<\/p>\n<h3>\u521b\u5efa\u65b9\u6cd5<\/h3>\n<p>\u601d\u60f3\u90fd\u662f\u7533\u660e\u4e00\u4e2a\u516c\u7528\u7684\u8c03\u7528\u65b9\u6cd5\uff0c\u603b\u7ed3\u8d77\u6765\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u7533\u660e\u4e00\u4e2ainterface\u63a5\u53e3\uff0c\u901a\u8fc7\u63a5\u53e3\u7279\u6b8a\u7533\u660e\u65b9\u6cd5<\/li>\n<li>\u7533\u660e\u4e00\u4e2aabstract\u62bd\u8c61\u7c7b\uff0c\u901a\u8fc7\u7ee7\u627f\u5b9e\u73b0<\/li>\n<\/ol>\n<h5>\u63a5\u53e3\u7533\u660e\u7684\u521b\u5efa\u65b9\u5f0f\uff1a<\/h5>\n<p>\u4ee5\u4e0b\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u52a8\u7269\u57fa\u7c7b\uff0c\u5206\u522b\u521b\u5efa\u732b\uff0c\u72d7\u4e24\u4e2a\u5177\u4f53\u7684\u7c7b\u578b\uff0c\u732b\u548c\u72d7\u90fd\u5b58\u5728\u54ac\u8fd9\u4e00\u4e2a\u52a8\u4f5c\u4e3a\u4f8b\uff0c\u5206\u522b\u5229\u7528\u4ee3\u7801\u5b9e\u73b0<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" cs=\"\">using System;\r\n\r\nnamespace TestCode.DesignPatterns.CreationalPattern.FactoryPattern\r\n{\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u63a5\u53e3\uff0c\u4f5c\u4e3a\u52a8\u7269\u7c7b\uff0c\u5176\u7edf\u7edf\u5305\u542b\u54ac\uff08Bite\uff09\u8fd9\u4e00\u4e2a\u52a8\u4f5c\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public interface Animal\r\n    {\r\n        void Bite();\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u732b\u7c7b\uff0c\u53ca\u5176\u7ec6\u8282\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public class Cat : Animal\r\n    {\r\n        public void Bite()\r\n        {\r\n            Console.WriteLine(\"The Cat bite.\");\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u72d7\u7c7b\uff0c\u53ca\u5176\u7ec6\u8282\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public class Dog : Animal\r\n    {\r\n        public void Bite()\r\n        {\r\n            Console.WriteLine(\"The Dog bite.\");\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u4e00\u4e2a\u679a\u4e3e\uff0c\u65b9\u4fbf\u5339\u914d\u7c7b\u578b\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public enum AnimalType\r\n    {\r\n        Cat,\r\n        Dog\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u5de5\u5382\u7c7b\uff0c\u76f4\u63a5\u6839\u636e\u52a8\u7269\u7c7b\u578b\u5339\u914d\u8fd4\u56de\u7c7b\u578b\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public class AnimalFactory\r\n    {\r\n        public static Animal GetAnimal(AnimalType animalType)\r\n        {\r\n            switch (animalType)\r\n            {\r\n                case AnimalType.Cat:\r\n                    return new Cat();\r\n                case AnimalType.Dog:\r\n                    return new Dog();\r\n                default:\r\n                    return null;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>\u5728\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528\u7684\u65b9\u6cd5\u4e3a\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" cs=\"\">using System;\r\nusing TestCode.DesignPatterns.CreationalPattern.FactoryPattern;\r\n\r\nnamespace TestCode\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u732b\u7c7b,\u5e76\u4e14\u8ba9\u732b\u54ac\u4e00\u53e3\r\n            Animal animal01 = AnimalFactory.GetAnimal(AnimalType.Cat);\r\n            animal01.Bite();\r\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u72d7\u7c7b,\u5e76\u4e14\u8ba9\u72d7\u54ac\u4e00\u53e3\r\n            Animal animal02 = AnimalFactory.GetAnimal(AnimalType.Dog);\r\n            animal02.Bite();\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<h5>\u62bd\u8c61\u7c7b\u7684\u7533\u660e\u65b9\u5f0f<\/h5>\n<p>\u5728\u7533\u660e\u7c7b\u578b\u7684\u65f6\u5019\u5b58\u5728\u4e00\u4e9b\u53d8\u5316\uff0c\u628ainterface\u53d8\u6210abstract\uff0c\u5e76\u4e14\u5bf9\u8be5\u7c7b\u578b\u8fdb\u884coverride\u590d\u5199\uff0c\u4f8b\u5b50\u4ee3\u7801\u5982\u4e0b\uff0c\u5de5\u5382\u7c7b\u7684\u521b\u5efa\u65b9\u5f0f\u4e0d\u53d8\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" cs=\"\">    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u62bd\u8c61\u57fa\u7c7b\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public abstract class AnimalAbstract\r\n    {\r\n        public abstract void Bite();\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u732b\u7c7b\uff0c\u53ca\u5176\u7ec6\u8282\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public class CatAb : AnimalAbstract\r\n    {\r\n        public override void Bite()\r\n        {\r\n            Console.WriteLine(\"The Cat bite.\");\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7533\u660e\u72d7\u7c7b\uff0c\u53ca\u5176\u7ec6\u8282\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public class DogAb : AnimalAbstract\r\n    {\r\n        public override void Bite()\r\n        {\r\n            Console.WriteLine(\"The Dog bite.\");\r\n        }\r\n    }<\/code><\/pre>\n<p>\u8fd9\u4e24\u8005\u5747\u53ef\uff0c\u6839\u636e\u81ea\u8eab\u5e38\u7528\u8bed\u8a00\u7279\u6027\u53d6\u820d\u5373\u53ef\u3002<\/p>\n<h3>\u62bd\u8c61\u7c7b\u7684\u4f18\u7f3a\u70b9\u548c\u5e94\u7528\u4e3e\u4f8b<\/h3>\n<p><strong>\u610f\u56fe\uff1a<\/strong>\u5b9a\u4e49\u4e00\u4e2a\u521b\u5efa\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u8ba9\u5176\u5b50\u7c7b\u81ea\u5df1\u51b3\u5b9a\u5b9e\u4f8b\u5316\u54ea\u4e00\u4e2a\u5de5\u5382\u7c7b\uff0c\u5de5\u5382\u6a21\u5f0f\u4f7f\u5176\u521b\u5efa\u8fc7\u7a0b\u5ef6\u8fdf\u5230\u5b50\u7c7b\u8fdb\u884c\u3002<\/p>\n<p><strong>\u4e3b\u8981\u89e3\u51b3\uff1a<\/strong>\u4e3b\u8981\u89e3\u51b3\u63a5\u53e3\u9009\u62e9\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u4f55\u65f6\u4f7f\u7528\uff1a<\/strong>\u6211\u4eec\u660e\u786e\u5730\u8ba1\u5212\u4e0d\u540c\u6761\u4ef6\u4e0b\u521b\u5efa\u4e0d\u540c\u5b9e\u4f8b\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3\uff1a<\/strong>\u8ba9\u5176\u5b50\u7c7b\u5b9e\u73b0\u5de5\u5382\u63a5\u53e3\uff0c\u8fd4\u56de\u7684\u4e5f\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u4ea7\u54c1\u3002<\/p>\n<p><strong>\u5173\u952e\u4ee3\u7801\uff1a<\/strong>\u521b\u5efa\u8fc7\u7a0b\u5728\u5176\u5b50\u7c7b\u6267\u884c\u3002<\/p>\n<p><strong>\u5e94\u7528\u5b9e\u4f8b\uff1a<\/strong>\u00a01\u3001\u60a8\u9700\u8981\u4e00\u8f86\u6c7d\u8f66\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u5de5\u5382\u91cc\u9762\u63d0\u8d27\uff0c\u800c\u4e0d\u7528\u53bb\u7ba1\u8fd9\u8f86\u6c7d\u8f66\u662f\u600e\u4e48\u505a\u51fa\u6765\u7684\uff0c\u4ee5\u53ca\u8fd9\u4e2a\u6c7d\u8f66\u91cc\u9762\u7684\u5177\u4f53\u5b9e\u73b0\u3002 2\u3001Hibernate \u6362\u6570\u636e\u5e93\u53ea\u9700\u6362\u65b9\u8a00\u548c\u9a71\u52a8\u5c31\u53ef\u4ee5\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u00a01\u3001\u4e00\u4e2a\u8c03\u7528\u8005\u60f3\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u53ea\u8981\u77e5\u9053\u5176\u540d\u79f0\u5c31\u53ef\u4ee5\u4e86\u3002 2\u3001\u6269\u5c55\u6027\u9ad8\uff0c\u5982\u679c\u60f3\u589e\u52a0\u4e00\u4e2a\u4ea7\u54c1\uff0c\u53ea\u8981\u6269\u5c55\u4e00\u4e2a\u5de5\u5382\u7c7b\u5c31\u53ef\u4ee5\u3002 3\u3001\u5c4f\u853d\u4ea7\u54c1\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u8c03\u7528\u8005\u53ea\u5173\u5fc3\u4ea7\u54c1\u7684\u63a5\u53e3\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u6bcf\u6b21\u589e\u52a0\u4e00\u4e2a\u4ea7\u54c1\u65f6\uff0c\u90fd\u9700\u8981\u589e\u52a0\u4e00\u4e2a\u5177\u4f53\u7c7b\u548c\u5bf9\u8c61\u5b9e\u73b0\u5de5\u5382\uff0c\u4f7f\u5f97\u7cfb\u7edf\u4e2d\u7c7b\u7684\u4e2a\u6570\u6210\u500d\u589e\u52a0\uff0c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u589e\u52a0\u4e86\u7cfb\u7edf\u7684\u590d\u6742\u5ea6\uff0c\u540c\u65f6\u4e5f\u589e\u52a0\u4e86\u7cfb\u7edf\u5177\u4f53\u7c7b\u7684\u4f9d\u8d56\u3002\u8fd9\u5e76\u4e0d\u662f\u4ec0\u4e48\u597d\u4e8b\u3002<\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u00a01\u3001\u65e5\u5fd7\u8bb0\u5f55\u5668\uff1a\u8bb0\u5f55\u53ef\u80fd\u8bb0\u5f55\u5230\u672c\u5730\u786c\u76d8\u3001\u7cfb\u7edf\u4e8b\u4ef6\u3001\u8fdc\u7a0b\u670d\u52a1\u5668\u7b49\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u8bb0\u5f55\u65e5\u5fd7\u5230\u4ec0\u4e48\u5730\u65b9\u3002 2\u3001\u6570\u636e\u5e93\u8bbf\u95ee\uff0c\u5f53\u7528\u6237\u4e0d\u77e5\u9053\u6700\u540e\u7cfb\u7edf\u91c7\u7528\u54ea\u4e00\u7c7b\u6570\u636e\u5e93\uff0c\u4ee5\u53ca\u6570\u636e\u5e93\u53ef\u80fd\u6709\u53d8\u5316\u65f6\u3002 3\u3001\u8bbe\u8ba1\u4e00\u4e2a\u8fde\u63a5\u670d\u52a1\u5668\u7684\u6846\u67b6\uff0c\u9700\u8981\u4e09\u4e2a\u534f\u8bae\uff0c&#8221;POP3&#8243;\u3001&#8221;IMAP&#8221;\u3001&#8221;HTTP&#8221;\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e09\u4e2a\u4f5c\u4e3a\u4ea7\u54c1\u7c7b\uff0c\u5171\u540c\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong>\u4f5c\u4e3a\u4e00\u79cd\u521b\u5efa\u7c7b\u6a21\u5f0f\uff0c\u5728\u4efb\u4f55\u9700\u8981\u751f\u6210\u590d\u6742\u5bf9\u8c61\u7684\u5730\u65b9\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\u3002\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\u5c31\u662f\u590d\u6742\u5bf9\u8c61\u9002\u5408\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\uff0c\u800c\u7b80\u5355\u5bf9\u8c61\uff0c\u7279\u522b\u662f\u53ea\u9700\u8981\u901a\u8fc7 new \u5c31\u53ef\u4ee5\u5b8c\u6210\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u65e0\u9700\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\u3002\u5982\u679c\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\uff0c\u5c31\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u5de5\u5382\u7c7b\uff0c\u4f1a\u589e\u52a0\u7cfb\u7edf\u7684\u590d\u6742\u5ea6\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\u6587\u732e\uff1a<a href=\"https:\/\/www.cnblogs.com\/heroysj\/p\/10875046.html\">https:\/\/www.cnblogs.com\/heroysj\/p\/10875046.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#\u5de5\u5382\u6a21\u5f0f C# \u5de5\u5382\u6a21\u5f0f\u7b80\u4ecb C# \u5de5\u5382\u6a21\u5f0f(Factory Pattern)\u662f\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u5e38\u7528\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,95,48,94,96],"class_list":["post-1083","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-c","tag-creational-pattern","tag-csharp","tag-design-patterns","tag-factory-pattern"],"views":1115,"_links":{"self":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1083"}],"version-history":[{"count":6,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1083\/revisions"}],"predecessor-version":[{"id":1089,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1083\/revisions\/1089"}],"wp:attachment":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}