{"id":1131,"date":"2021-02-11T15:22:59","date_gmt":"2021-02-11T07:22:59","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1131"},"modified":"2021-02-11T16:38:09","modified_gmt":"2021-02-11T08:38:09","slug":"%e5%89%b5%e5%bb%ba%e5%9e%8b%e6%a8%a1%e5%bc%8f-factory-method-pattern%e5%b7%a5%e5%bb%a0%e6%96%b9%e6%b3%95%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%e5%89%b5%e5%bb%ba%e5%9e%8b%e6%a8%a1%e5%bc%8f-factory-method-pattern%e5%b7%a5%e5%bb%a0%e6%96%b9%e6%b3%95%e6%a8%a1%e5%bc%8f\/","title":{"rendered":"\u5275\u5efa\u578b\u6a21\u5f0f-Factory Method Pattern(\u5de5\u5ee0\u65b9\u6cd5\u6a21\u5f0f)"},"content":{"rendered":"<h1>Factory Method Pattern(\u5de5\u5ee0\u65b9\u6cd5\u6a21\u5f0f)<\/h1>\n<ul>\n<li>\u6587\u7ae0\u76ee\u9304<br \/>\n<\/p>\n<ul>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/\u5275\u5efa\u578b\u6a21\u5f0f-factory-method-pattern\u5de5\u5ee0\u65b9\u6cd5\u6a21\u5f0f\/fly\/design-pattern\/#a\">\u4ecb\u7d39<\/a><br \/>\n<\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/\u5275\u5efa\u578b\u6a21\u5f0f-factory-method-pattern\u5de5\u5ee0\u65b9\u6cd5\u6a21\u5f0f\/fly\/design-pattern\/#b\">\u597d\u8655<\/a><br \/>\n<\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/\u5275\u5efa\u578b\u6a21\u5f0f-factory-method-pattern\u5de5\u5ee0\u65b9\u6cd5\u6a21\u5f0f\/fly\/design-pattern\/#c\">\u7a0b\u5f0f\u7bc4\u4f8b<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><\/h2>\n<p><a id=\"a\"><\/a><\/p>\n<h3>\u4ecb\u7d39<\/h3>\n<h5>\u5c07\u5927\u5de5\u5ee0\u88e1\u7684\u76f8\u540c\u985e\u578b\u62c6\u5206\u51fa\u4f86\u8b8a\u4e00\u584a\u4e00\u584a\u7684\u5c0f\u5de5\u5ee0<\/h5>\n<h5>EX:<\/h5>\n<h5>\u6c34\u679c\u5de5\u5ee0 > \u9999\u8549\u5de5\u5ee0\u3001\u860b\u679c\u5de5\u5ee0\u3001\u6a58\u5b50\u5de5\u5ee0<\/h5>\n<h5>\u7269\u6d41\u5de5\u5ee0 > \u6c34\u904b\u5de5\u5ee0\u3001\u9678\u904b\u5de5\u5ee0\u3001\u7a7a\u904b\u5de5\u5ee0<\/h5>\n<p><a id=\"b\"><\/a><\/p>\n<h3>\u597d\u8655<\/h3>\n<h5>\u5de5\u5ee0\u65b9\u6cd5\u628a\u5275\u5efa\u8207\u4f7f\u7528\u7684\u7a0b\u5f0f\u78bc\u5206\u96e2,\u8981\u64f4\u5c55\u6642\u5c31\u4e0d\u6703\u5f71\u97ff\u5176\u4ed6\u7684\u7a0b\u5f0f\u78bc<\/h5>\n<p><a id=\"c\"><\/a><\/p>\n<h3>\u7a0b\u5f0f\u7bc4\u4f8b<\/h3>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/MuHongWeiWei\/DesignPattern\/tree\/master\/src\/main\/factoryMethodPattern\" title=\"Factory Method Pattern\" target=\"_blank\" rel=\"noopener\">Factory Method Pattern<\/a><\/p>\n<h6>\u5275\u5efa\u5de5\u5ee0\u63a5\u53e3<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">interface FruitFactory {\n    fun getFruit(): Fruit\n}\n<\/code><\/pre>\n<h6>\u5275\u5efa\u7522\u54c1\u63a5\u53e3<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">interface Fruit {\n    fun eat()\n}\n<\/code><\/pre>\n<h6>\u5be6\u73fe\u9999\u8549\u7522\u54c1<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">class Banana : Fruit {\n    override fun eat() {\n        println(\"\u5403\u9999\u8549\")\n    }\n}\n<\/code><\/pre>\n<h6>\u5be6\u73fe\u860b\u679c\u7522\u54c1<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">class Apple : Fruit{\n    override fun eat() {\n        println(\"\u5403\u860b\u679c\")\n    }\n}\n<\/code><\/pre>\n<h6>\u5be6\u73fe\u6a58\u5b50\u7522\u54c1<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">class Orange : Fruit {\n    override fun eat() {\n        println(\"\u5403\u6a58\u5b50\")\n    }\n}\n<\/code><\/pre>\n<h6>\u5be6\u73fe\u9999\u8549\u5de5\u5ee0(\u53d6\u5f97\u9999\u8549)<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">class BananaFactory : FruitFactory {\n    override fun getFruit(): Fruit {\n        return Banana()\n    }\n}\n<\/code><\/pre>\n<h6>\u5be6\u73fe\u860b\u679c\u5de5\u5ee0(\u53d6\u5f97\u860b\u679c)<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">class AppleFactory : FruitFactory {\n    override fun getFruit(): Fruit {\n        return Apple()\n    }\n}\n<\/code><\/pre>\n<h6>\u5be6\u73fe\u6a58\u5b50\u5de5\u5ee0(\u53d6\u5f97\u6a58\u5b50)<\/h6>\n<pre><code class=\"language-Kotlin line-numbers\">class OrangeFactory : FruitFactory {\n    override fun getFruit(): Fruit {\n        return Orange()\n    }\n}\n<\/code><\/pre>\n<h3>\u57f7\u884c<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">fun main() {\n    FactoryTest().flyGetApple()\n    FactoryTest().flyGetBanana()\n    FactoryTest().flyGetOrange()\n}\n\nclass FactoryTest {\n\n    private lateinit var fruitFactory: FruitFactory\n\n    fun flyGetBanana() {\n        fruitFactory = BananaFactory()\n        fruitFactory.getFruit().eat()\n    }\n\n    fun flyGetApple() {\n        fruitFactory = AppleFactory()\n        fruitFactory.getFruit().eat()\n    }\n\n    fun flyGetOrange() {\n        fruitFactory = OrangeFactory()\n        fruitFactory.getFruit().eat()\n    }\n}\n<\/code><\/pre>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/02\/1.png\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/02\/1.png\" alt=\"\" \/><\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Factory Method Pattern(\u5de5\u5ee0\u65b9\u6cd5\u6a21\u5f0f) \u6587\u7ae0\u76ee\u9304 \u4ecb\u7d39 \u597d\u8655 \u7a0b\u5f0f\u7bc4\u4f8b \u4ecb\u7d39 \u5c07\u5927\u5de5\u5ee0\u88e1 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[50],"tags":[51,172,15],"class_list":["post-1131","post","type-post","status-publish","format-standard","hentry","category-design-pattern","tag-design-pattern","tag-factory-method-pattern","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1131"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1139,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1131\/revisions\/1139"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}