From 2abf3ae24ae8a845afa660940ab61d96499f9c87 Mon Sep 17 00:00:00 2001 From: Awin Huang Date: Tue, 21 Jun 2022 16:25:49 +0800 Subject: [PATCH] vault backup: 2022-06-21 16:25:48 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: 02. PARA/03. Resources(資源)/Design Pattern.md --- .../03. Resources(資源)/Design Pattern.md | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/02. PARA/03. Resources(資源)/Design Pattern.md b/02. PARA/03. Resources(資源)/Design Pattern.md index f9b5a62..c822e15 100644 --- a/02. PARA/03. Resources(資源)/Design Pattern.md +++ b/02. PARA/03. Resources(資源)/Design Pattern.md @@ -151,27 +151,46 @@ notify()則是當發生變化時,用來通知所有觀察者的實作。 假設這樣寫: - !!!col - 1 - + ### Ingredient class + ```cpp + class IngredientBubble { ... }; + class IngredientRedbean { ... }; + class IngredientGreenbean { ... }; + class IngredientFairyGrass { ... }; + ``` -````col - + - 2 + ### Beverage class + ```cpp + class BeverageMilkTea { ... }; + class BeverageGreenTea { ... }; + class BeverageBlackTea { + IngredientBubble* bubble; + IngredientRedbean* redbean; + IngredientGreenbean* greenbean; + IngredientFairyGrass* fairyGrass; + } + ``` + +每個飲料的class裡面都將每個配料定義為一個member,如果客人有加配料的話,我們就將配料實例化,假設奶茶加了珍珠: ```cpp -Ingredients -class BeverageMilkTea { ... }; -class BeverageGreenTea { ... }; class BeverageBlackTea { - -} + void addBubble() { + if (!bubble) bubble = new IngredientBubble(); + } +}; ``` - +要算價格的時候: ```cpp -class BeverageMilkTea { ... }; -class BeverageGreenTea { ... }; class BeverageBlackTea { - -} -``` - -```` - + void cost() { + if (bubble) cost += 10; + if (redbean) cost += 10; + if (greenbean) cost += 10; + if (fairyGrass) cost += 10; + } +private: + int32_t cost = 30; // 奶茶本身10元 +}; +``` \ No newline at end of file