{"id":196531,"date":"2020-07-26T10:32:06","date_gmt":"2020-07-26T02:32:06","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=196531"},"modified":"2020-07-20T09:33:14","modified_gmt":"2020-07-20T01:33:14","slug":"pattern-builder-pattern","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/pattern-builder-pattern.html","title":{"rendered":"java\u8bbe\u8ba1\u6a21\u5f0f-\u5efa\u9020\u8005\u6a21\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5efa\u9020\u8005\u6a21\u5f0f\uff08Builder Pattern\uff09\u4f7f\u7528\u591a\u4e2a\u7b80\u5355\u7684\u5bf9\u8c61\u4e00\u6b65\u4e00\u6b65\u6784\u5efa\u6210\u4e00\u4e2a\u590d\u6742\u7684\u5bf9\u8c61\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u8bbe\u8ba1\u6a21\u5f0f\u5c5e\u4e8e\u521b\u5efa\u578b\u6a21\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u6700\u4f73\u65b9\u5f0f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/a><\/p>\n

\u4ecb\u7ecd<\/strong><\/div>\n

\u610f\u56fe\uff1a\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u6784\u5efa\u4e0e\u5176\u8868\u793a\u76f8\u5206\u79bb\uff0c\u4f7f\u5f97\u540c\u6837\u7684\u6784\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7684\u8868\u793a\u3002<\/p>\n

\u4e3b\u8981\u89e3\u51b3\uff1a\u4e3b\u8981\u89e3\u51b3\u5728\u8f6f\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u6709\u65f6\u5019\u9762\u4e34\u7740\"\u4e00\u4e2a\u590d\u6742\u5bf9\u8c61\"\u7684\u521b\u5efa\u5de5\u4f5c\uff0c\u5176\u901a\u5e38\u7531\u5404\u4e2a\u90e8\u5206\u7684\u5b50\u5bf9\u8c61\u7528\u4e00\u5b9a\u7684\u7b97\u6cd5\u6784\u6210\uff1b\u7531\u4e8e\u9700\u6c42\u7684\u53d8\u5316\uff0c\u8fd9\u4e2a\u590d\u6742\u5bf9\u8c61\u7684\u5404\u4e2a\u90e8\u5206\u7ecf\u5e38\u9762\u4e34\u7740\u5267\u70c8\u7684\u53d8\u5316\uff0c\u4f46\u662f\u5c06\u5b83\u4eec\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u7b97\u6cd5\u5374\u76f8\u5bf9\u7a33\u5b9a\u3002<\/p>\n

\u4f55\u65f6\u4f7f\u7528\uff1a\u4e00\u4e9b\u57fa\u672c\u90e8\u4ef6\u4e0d\u4f1a\u53d8\uff0c\u800c\u5176\u7ec4\u5408\u7ecf\u5e38\u53d8\u5316\u7684\u65f6\u5019\u3002<\/p>\n

\u5982\u4f55\u89e3\u51b3\uff1a\u5c06\u53d8\u4e0e\u4e0d\u53d8\u5206\u79bb\u5f00\u3002<\/p>\n

\u5173\u952e\u4ee3\u7801\uff1a\u5efa\u9020\u8005\uff1a\u521b\u5efa\u548c\u63d0\u4f9b\u5b9e\u4f8b\uff0c\u5bfc\u6f14\uff1a\u7ba1\u7406\u5efa\u9020\u51fa\u6765\u7684\u5b9e\u4f8b\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n

\u5e94\u7528\u5b9e\u4f8b\uff1a 1\u3001\u53bb\u80af\u5fb7\u57fa\uff0c\u6c49\u5821\u3001\u53ef\u4e50\u3001\u85af\u6761\u3001\u70b8\u9e21\u7fc5\u7b49\u662f\u4e0d\u53d8\u7684\uff0c\u800c\u5176\u7ec4\u5408\u662f\u7ecf\u5e38\u53d8\u5316\u7684\uff0c\u751f\u6210\u51fa\u6240\u8c13\u7684\"\u5957\u9910\"\u3002 2\u3001JAVA \u4e2d\u7684 StringBuilder\u3002<\/p>\n

\u4f18\u70b9\uff1a 1\u3001\u5efa\u9020\u8005\u72ec\u7acb\uff0c\u6613\u6269\u5c55\u3002 2\u3001\u4fbf\u4e8e\u63a7\u5236\u7ec6\u8282\u98ce\u9669\u3002<\/p>\n

\u7f3a\u70b9\uff1a 1\u3001\u4ea7\u54c1\u5fc5\u987b\u6709\u5171\u540c\u70b9\uff0c\u8303\u56f4\u6709\u9650\u5236\u3002 2\u3001\u5982\u5185\u90e8\u53d8\u5316\u590d\u6742\uff0c\u4f1a\u6709\u5f88\u591a\u7684\u5efa\u9020\u7c7b\u3002<\/p>\n

\u4f7f\u7528\u573a\u666f\uff1a 1\u3001\u9700\u8981\u751f\u6210\u7684\u5bf9\u8c61\u5177\u6709\u590d\u6742\u7684\u5185\u90e8\u7ed3\u6784\u3002 2\u3001\u9700\u8981\u751f\u6210\u7684\u5bf9\u8c61\u5185\u90e8\u5c5e\u6027\u672c\u8eab\u76f8\u4e92\u4f9d\u8d56\u3002<\/p>\n

\u6ce8\u610f\u4e8b\u9879\uff1a\u4e0e\u5de5\u5382\u6a21\u5f0f\u7684\u533a\u522b\u662f\uff1a\u5efa\u9020\u8005\u6a21\u5f0f\u66f4\u52a0\u5173\u6ce8\u4e0e\u96f6\u4ef6\u88c5\u914d\u7684\u987a\u5e8f\u3002<\/p>\n

\u5b9e\u73b0<\/strong><\/div>\n

\u6211\u4eec\u5047\u8bbe\u4e00\u4e2a\u5feb\u9910\u5e97\u7684\u5546\u4e1a\u6848\u4f8b\uff0c\u5176\u4e2d\uff0c\u4e00\u4e2a\u5178\u578b\u7684\u5957\u9910\u53ef\u4ee5\u662f\u4e00\u4e2a\u6c49\u5821\uff08Burger\uff09\u548c\u4e00\u676f\u51b7\u996e\uff08Cold drink\uff09\u3002\u6c49\u5821\uff08Burger\uff09\u53ef\u4ee5\u662f\u7d20\u98df\u6c49\u5821\uff08Veg Burger\uff09\u6216\u9e21\u8089\u6c49\u5821\uff08Chicken Burger\uff09\uff0c\u5b83\u4eec\u662f\u5305\u5728\u7eb8\u76d2\u4e2d\u3002\u51b7\u996e\uff08Cold drink\uff09\u53ef\u4ee5\u662f\u53ef\u53e3\u53ef\u4e50\uff08coke\uff09\u6216\u767e\u4e8b\u53ef\u4e50\uff08pepsi\uff09\uff0c\u5b83\u4eec\u662f\u88c5\u5728\u74f6\u5b50\u4e2d\u3002<\/p>\n

\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u8868\u793a\u98df\u7269\u6761\u76ee\uff08\u6bd4\u5982\u6c49\u5821\u548c\u51b7\u996e\uff09\u7684 Item \u63a5\u53e3\u548c\u5b9e\u73b0 Item \u63a5\u53e3\u7684\u5b9e\u4f53\u7c7b\uff0c\u4ee5\u53ca\u4e00\u4e2a\u8868\u793a\u98df\u7269\u5305\u88c5\u7684 Packing \u63a5\u53e3\u548c\u5b9e\u73b0 Packing \u63a5\u53e3\u7684\u5b9e\u4f53\u7c7b\uff0c\u6c49\u5821\u662f\u5305\u5728\u7eb8\u76d2\u4e2d\uff0c\u51b7\u996e\u662f\u88c5\u5728\u74f6\u5b50\u4e2d\u3002<\/p>\n

\u7136\u540e\u6211\u4eec\u521b\u5efa\u4e00\u4e2a Meal \u7c7b\uff0c\u5e26\u6709 Item \u7684 ArrayList \u548c\u4e00\u4e2a\u901a\u8fc7\u7ed3\u5408 Item \u6765\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684 Meal \u5bf9\u8c61\u7684 MealBuilder\u3002BuilderPatternDemo\uff0c\u6211\u4eec\u7684\u6f14\u793a\u7c7b\u4f7f\u7528 MealBuilder \u6765\u521b\u5efa\u4e00\u4e2a Meal\u3002
\n\"\"<\/p>\n

\u6b65\u9aa4 1<\/strong><\/span><\/div>\n

\u521b\u5efa\u4e00\u4e2a\u8868\u793a\u98df\u7269\u6761\u76ee\u548c\u98df\u7269\u5305\u88c5\u7684\u63a5\u53e3\u3002<\/p>\n

\r\nItem.java\r\npublic interface Item {\r\n   public String name();\r\n   public Packing packing();\r\n   public float price();    \r\n}\r\nPacking.java\r\npublic interface Packing {\r\n   public String pack();\r\n}\r\n<\/pre>\n
\u6b65\u9aa4 2<\/strong><\/span><\/div>\n

\u521b\u5efa\u5b9e\u73b0 Packing \u63a5\u53e3\u7684\u5b9e\u4f53\u7c7b\u3002<\/p>\n

\r\nWrapper.java\r\npublic class Wrapper implements Packing {\r\n \r\n   @Override\r\n   public String pack() {\r\n      return \"Wrapper\";\r\n   }\r\n}\r\nBottle.java\r\npublic class Bottle implements Packing {\r\n \r\n   @Override\r\n   public String pack() {\r\n      return \"Bottle\";\r\n   }\r\n}<\/pre>\n
\u6b65\u9aa4 3<\/strong><\/span><\/div>\n

\u521b\u5efa\u5b9e\u73b0 Item \u63a5\u53e3\u7684\u62bd\u8c61\u7c7b\uff0c\u8be5\u7c7b\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u7684\u529f\u80fd\u3002<\/p>\n

\r\nBurger.java\r\npublic abstract class Burger implements Item {\r\n \r\n   @Override\r\n   public Packing packing() {\r\n      return new Wrapper();\r\n   }\r\n \r\n   @Override\r\n   public abstract float price();\r\n}\r\nColdDrink.java\r\npublic abstract class ColdDrink implements Item {\r\n \r\n    @Override\r\n    public Packing packing() {\r\n       return new Bottle();\r\n    }\r\n \r\n    @Override\r\n    public abstract float price();\r\n}<\/pre>\n
\u6b65\u9aa4 4<\/strong><\/span><\/div>\n

\u521b\u5efa\u6269\u5c55\u4e86 Burger \u548c ColdDrink \u7684\u5b9e\u4f53\u7c7b\u3002<\/p>\n

\r\nVegBurger.java\r\npublic class VegBurger extends Burger {\r\n \r\n   @Override\r\n   public float price() {\r\n      return 25.0f;\r\n   }\r\n \r\n   @Override\r\n   public String name() {\r\n      return \"Veg Burger\";\r\n   }\r\n}\r\nChickenBurger.java\r\npublic class ChickenBurger extends Burger {\r\n \r\n   @Override\r\n   public float price() {\r\n      return 50.5f;\r\n   }\r\n \r\n   @Override\r\n   public String name() {\r\n      return \"Chicken Burger\";\r\n   }\r\n}\r\nCoke.java\r\npublic class Coke extends ColdDrink {\r\n \r\n   @Override\r\n   public float price() {\r\n      return 30.0f;\r\n   }\r\n \r\n   @Override\r\n   public String name() {\r\n      return \"Coke\";\r\n   }\r\n}\r\nPepsi.java\r\npublic class Pepsi extends ColdDrink {\r\n \r\n   @Override\r\n   public float price() {\r\n      return 35.0f;\r\n   }\r\n \r\n   @Override\r\n   public String name() {\r\n      return \"Pepsi\";\r\n   }\r\n}<\/pre>\n
\u6b65\u9aa4 5<\/strong><\/span><\/div>\n

\u521b\u5efa\u4e00\u4e2a Meal \u7c7b\uff0c\u5e26\u6709\u4e0a\u9762\u5b9a\u4e49\u7684 Item \u5bf9\u8c61\u3002<\/p>\n

\r\nMeal.java\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n \r\npublic class Meal {\r\n   private List items = new ArrayList<\/item>();    \r\n \r\n   public void addItem(Item item){\r\n      items.add(item);\r\n   }\r\n \r\n   public float getCost(){\r\n      float cost = 0.0f;\r\n      for (Item item : items) {\r\n         cost += item.price();\r\n      }        \r\n      return cost;\r\n   }\r\n \r\n   public void showItems(){\r\n      for (Item item : items) {\r\n         System.out.print(\"Item : \"+item.name());\r\n         System.out.print(\", Packing : \"+item.packing().pack());\r\n         System.out.println(\", Price : \"+item.price());\r\n      }        \r\n   }    \r\n}<\/item><\/pre>\n
\u6b65\u9aa4 6<\/strong><\/span><\/div>\n

\u521b\u5efa\u4e00\u4e2a MealBuilder \u7c7b\uff0c\u5b9e\u9645\u7684 builder \u7c7b\u8d1f\u8d23\u521b\u5efa Meal \u5bf9\u8c61\u3002<\/p>\n

\r\nMealBuilder.java\r\npublic class MealBuilder {\r\n \r\n   public Meal prepareVegMeal (){\r\n      Meal meal = new Meal();\r\n      meal.addItem(new VegBurger());\r\n      meal.addItem(new Coke());\r\n      return meal;\r\n   }   \r\n \r\n   public Meal prepareNonVegMeal (){\r\n      Meal meal = new Meal();\r\n      meal.addItem(new ChickenBurger());\r\n      meal.addItem(new Pepsi());\r\n      return meal;\r\n   }\r\n}<\/pre>\n
\u6b65\u9aa4 7<\/strong><\/span><\/div>\n

BuiderPatternDemo \u4f7f\u7528 MealBuider \u6765\u6f14\u793a\u5efa\u9020\u8005\u6a21\u5f0f\uff08Builder Pattern\uff09\u3002<\/p>\n

\r\nBuilderPatternDemo.java\r\npublic class BuilderPatternDemo {\r\n   public static void main(String[] args) {\r\n      MealBuilder mealBuilder = new MealBuilder();\r\n \r\n      Meal vegMeal = mealBuilder.prepareVegMeal();\r\n      System.out.println(\"Veg Meal\");\r\n      vegMeal.showItems();\r\n      System.out.println(\"Total Cost: \" +vegMeal.getCost());\r\n \r\n      Meal nonVegMeal = mealBuilder.prepareNonVegMeal();\r\n      System.out.println(\"\\n\\nNon-Veg Meal\");\r\n      nonVegMeal.showItems();\r\n      System.out.println(\"Total Cost: \" +nonVegMeal.getCost());\r\n   }\r\n}<\/pre>\n
\u6b65\u9aa4 8<\/strong><\/span><\/div>\n

\u6267\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

\r\nVeg Meal\r\nItem : Veg Burger, Packing : Wrapper, Price : 25.0\r\nItem : Coke, Packing : Bottle, Price : 30.0\r\nTotal Cost: 55.0\r\n\r\n\r\nNon-Veg Meal\r\nItem : Chicken Burger, Packing : Wrapper, Price : 50.5\r\nItem : Pepsi, Packing : Bottle, Price : 35.0\r\nTotal Cost: 85.5<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u610f\u56fe\uff1a\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u6784\u5efa\u4e0e\u5176\u8868\u793a\u76f8\u5206\u79bb\uff0c\u4f7f\u5f97\u540c\u6837\u7684\u6784\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7684\u8868\u793a\u3002 \u4e3b\u8981\u89e3\u51b3\uff1a\u4e3b\u8981\u89e3\u51b3\u5728\u8f6f\u4ef6\u7cfb\u7edf\u4e2d\uff0c […]<\/p>\n","protected":false},"author":1481,"featured_media":196532,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-196531","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/196531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=196531"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/196531\/revisions"}],"predecessor-version":[{"id":196537,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/196531\/revisions\/196537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/196532"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=196531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=196531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=196531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}