通過在Chipotle用餐了解模板方法設計模式

by Sihui Huang

黃思慧

通過在Chipotle用餐了解模板方法設計模式 (Understanding the Template Method design pattern by eating at Chipotle)

Object-Oriented Design Patterns in Life— gain an intuitive understanding of OO design patterns by linking them with real-life examples.

生活中的面向對象設計模式-通過將它們與實際示例聯系起來,可以直觀地了解OO設計模式。

Template Method is a commonly used design pattern in programming and real life.

模板方法是編程和現實生活中常用的設計模式。

Before we dive into details of the pattern, let’s learn an important life lesson:

在深入研究模式的細節之前,讓我們學習一個重要的人生課程:

Chipotle 101:如何在Chipotle訂購。 (Chipotle 101: How to Order at Chipotle.)

There are four steps involved:

涉及四個步驟:

  1. Choose a “vessel”: Burrito vs. Bowl vs. Tacos vs. Salad

    選擇一個“容器”:墨西哥卷餅,碗,玉米餅和沙拉
  2. Add meat: Chicken vs Steak vs. Barbacoa vs. Carnitas vs. Vegetarian

    添加肉類:雞肉vs牛排vs. Barbacoa vs. Carnitas vs.素食主義者
  3. Add toppings: Tomato vs. Corn vs. Green Chili vs. Red Chili

    添加澆頭:番茄,玉米,綠色辣椒,紅色辣椒
  4. Add extras & drinks: Chips vs. Guacamole vs. Salsa vs. Beer vs. Soda

    添加額外功能和飲料:薯片,鱷梨調味醬,鱷梨醬,莎莎,啤酒和汽水

For example, my go-to order is Bowl + Steak + (Tomato + Corn) + Guacamole and my friend Amber’s go-to order is Burrito + Chicken + (Green Chili + Red Chili) + (Chips + Soda).

例如,我的下單是碗+牛排+(番茄+玉米)+鱷梨調味醬,而我的朋友琥珀的下單是墨西哥卷餅+雞肉+(綠辣椒+紅辣椒)+(薯片+蘇打)。

If we code our go-to orders in Ruby, they will look like:

如果我們使用Ruby編寫轉到訂單,則它們將如下所示:

When we order, we put everything we want into the vessel and return the stuffed vessel.

訂購時,我們會將所需的所有物品放入容器中,然后將填充的容器退還。

Unfortunately, Amber and I decided to go on a diet for a while. And we decided that when we ordered from Chipotle, we could only get tomato as a topping and no extras. So our choices are limited to:

不幸的是,我和琥珀決定節食一段時間。 然后我們決定,當我們從Chipotle訂購時,我們只能拿到番茄作為澆頭,而沒有多余的東西。 因此,我們的選擇僅限于:

  1. Vessel: Burrito vs. Bowl vs. Tacos vs. Salad

    船只:墨西哥卷餅vs.碗vs.炸玉米餅vs.沙拉
  2. Meat: Chicken vs. Steak vs. Barbacoa vs. Carnitas vs. Vegetarian

    肉類:雞肉,牛排,Barbacoa,Carnitas和素食主義者
  3. Toppings: Tomato

    澆頭:番茄
  4. No extras & drinks

    沒有額外的東西和飲料

During the diet, our go-to orders have to be modified to:

在節食期間,我們的常用訂單必須修改為:

  • Sihui: Bowl + Steak + Tomato + No extras & drinks

    四會:碗+牛排+番茄+沒有額外的飲料
  • Amber: Burrito + Chicken + Tomato + No extras & drinks

    琥珀色:墨西哥卷餅+雞肉+番茄+沒有額外的飲料

Putting our orders down in Ruby, we have the following:

在Ruby中下訂單,我們有以下幾點:

Since both our orders have the exact same toppings, extras, and order methods, it makes sense to pull them out as a parent class, DietOrder, and have DietOrderSihui and DietOrderAmber inherit from it.

由于我們的兩個訂單都具有完全相同的澆頭附加功能訂購方法,因此將它們作為父類DietOrder退出并讓DietOrderSihuiDietOrderAmber繼承是有意義的

Now our friend Ben wants to join our Chipotle Diet Club, and he likes Tacos with Carnitas. Then his order will be:

現在,我們的朋友Ben想加入我們的Chipotle Diet Club,他喜歡帶有肉堿的炸玉米餅。 那么他的命令將是:

Ta-da, you just learned the Template Method design pattern! ? ? ?

Ta-da,您剛剛了解了Template Method設計模式! ? ? ?

Don’t believe me?

不相信我嗎

Take a look at the definition of the Template Method:

看一下Template方法的定義:

The Template Method pattern is a behavioral design pattern that
模板方法模式是一種行為設計模式,
- defines the program skeleton of an algorithm in an operation,
-定義運算中算法的程序框架,
- deferrs some steps to subclasses.
-將某些步驟推遲到子類。
It lets one redefine certain steps of an algorithm without changing the algorithm’s structure.
它允許重新定義算法的某些步驟,而無需更改算法的結構。

Doesn’t this sound exactly like what we just did with our DietOrder and SihuiDietOrder/AmberDietOrder/BenDietOrder?

這聽起來不完全像我們對DietOrderSihuiDietOrder / AmberDietOrder / BenDietOrder所做的那樣嗎?

DietOrder defines the order skeleton: one can only get tomato as a topping and no extras & drinks, and one orders by picking a vessel and putting everything inside the chosen vessel.

DietOrder定義了訂單框架:一個人只能獲得番茄作為澆頭,而沒有額外的飲料,而一個訂單則是通過撿起一個容器并將所有東西放入選定的容器中來完成的。

SihuiDietOrder/AmberDietOrder/BenDietOrder redefine the vessel and meat depending on our personal preferences.

SihuiDietOrder / AmberDietOrder / BenDietOrder根據我們的個人喜好重新定義容器和肉類。

Let’s say a month passed by, and Amber and I followed our diet strictly. We decided to reward ourselves with cheat days!

假設過去了一個月,我和Amber嚴格遵守飲食習慣。 我們決定用作弊的日子來獎勵自己!

On a cheat day, we have soda as our drinks. ??? And each of us can decide which day of the month will be our cheat day.

在作弊的一天,我們喝蘇打水。 ??? 而且我們每個人都可以決定每月的哪一天是我們的作弊日。

Since Ben is new to the club, he decides to stick to the diet strictly for a bit longer.

由于本是俱樂部的新成員,他決定嚴格堅持飲食更長的時間。

Let’s see how it looks in Ruby:

讓我們看看它在Ruby中的外觀:

In DietOrder, we ask if today is a cheat day. If so, we can have Soda as an extra. Otherwise, there are no extras. And by default, today is not a cheat day.

DietOrder中 ,我們詢問今天是否是作弊日。 如果是這樣,我們可以額外提供汽水。 否則,將沒有額外費用。 而且默認情況下,今天不是騙人的日子。

Amber and I get to define our own cheat days:

我和琥珀要定義自己的作弊天數:

Since Ben is sticking with the diet strictly, he doesn’t get a cheat day.

由于本嚴格遵守飲食習慣,所以他一天都不會作弊。

His class doesn’t need to change.

他的課不需要改變。

The is_cheat_day? method is a hook.

is_cheat_day? 方法是一個鉤子。

A hook provides a way for a subclass to implement an optional part of an algorithm.

鉤子為子類提供了一種實現算法的可選部分的方法。

If the subclass doesn’t care about the part, it can skip it and use the default implementation in the parent class.

如果子類不關心該部分,則可以跳過該部分,并使用父類中的默認實現。

In our case, is_cheat_day? is optional. SihuiDietOrder and AmberDietOrder implement it because we want to have a cheat day each month. But Ben does not want to have a cheat day. So BenDietOrder skips implementing is_cheat_day? and uses the default one from DietOrder, which always returns false.

在我們的情況下, is_cheat_day? 是可選的。 SihuiDietOrder和AmberDietOrder實現了它,因為我們希望每個月都有一個作弊日。 但本不想不想作弊。 所以BenDietOrder跳過實現is_cheat_day嗎? 并使用DietOrder中的默認值(始終返回false)。

There are two important object-oriented design principles used in the Template Method:

模板方法中使用了兩個重要的面向對象設計原則:

1. Encapsulate what varies.

1.封裝變化的內容。

In our case, the varying parts are vessel, meat, and is_cheat_day?. We encapsulate them in subclasses. For the parts that don’t vary, toppings and extras, we leave them in the parent class.

在我們的例子中,不同的部分是vesselmeatis_cheat_day ?。 我們將它們封裝在子類中。 對于不變的部分, 澆頭附加料 ,我們將其留在父類中。

2. The Hollywood Principle: Don’t call us, we’ll call you.

2.好萊塢原則:請勿致電給我們,我們會致電給您。

Yes, The Hollywood Principle is a real thing.

是的,好萊塢原則是真實的東西 。

In Hollywood, movie producers will tell actors: “Don’t call us, we’ll call you if we find a role that fits you.”

在好萊塢,電影制片人會告訴演員:“不要打電話給我們,如果找到適合您的角色,我們就會打電話給您。”

In programming, low-level components can participate in the computation, like AmberDietOrder defining its own is_cheat_day?, but the high-level components control when and how, like DietOrder calls is_cheat_day? within extras.

在編程中,低級組件可以參與計算,例如AmberDietOrder定義自己的is_cheat_day?。 ,但高層組件控制何時和如何,例如DietOrder調用is_cheat_day?額外費用內。

外賣: (Takeaways:)

One definition =>

<一個定義= >

The Template Method pattern is a behavioral design pattern that
模板方法模式是一種行為設計模式,
- defines the program skeleton of an algorithm in an operation,
-定義運算中算法的程序框架,
- deferrs some steps to subclasses.
-將某些步驟推遲到子類。
It lets one redefine certain steps of an algorithm without changing the algorithm’s structure.
它允許重新定義算法的某些步驟,而無需更改算法的結構。

Two Design Principles =&gt;

<兩個設計原理 >

1. Encapsulate what varies.
1.封裝變化的內容。
2. The Hollywood Principle: Don’t call us, we’ll call you.
2.好萊塢原則:請勿致電我們,我們會致電給您。

Or…

要么…

you can just take away a Chipotle order ? ? ?

您可以拿走一份Chipotle訂單嗎? ? ?

Next time, we take our design & food adventure to ???

下次,我們將我們的設計和美食之旅帶到???

翻譯自: https://www.freecodecamp.org/news/understanding-the-template-method-design-pattern-by-eating-at-chipotle-37f6e029f065/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395068.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395068.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395068.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Coriant助力Aureon部署100Gbps光纖網絡

根據相關消息顯示&#xff0c;光傳輸設備廠商Coriant日前表示已經向網絡傳輸和業務通信服務供應商Aureon Technology提供了7100納米分組光傳輸平臺&#xff0c;幫助其進行100Gbps光纖網絡的拓展。 該服務供應商&#xff08;Aureon&#xff09;將利用該分組光傳輸系統&#xff0…

python class tynu()_Visual Studio Express | Teraz Visual Studio Community

Program Visual Studio 2019 jest teraz dost?pnyDostosowany instalatorTwrz aplikacje w technologiach WPF, WinForms, platformy uniwersaln? systemu Windows, Win32, Android, iOS i innych — wszystko to za pomoc? jednego ?rodowiska IDE zapewniaj?cego wszyst…

css樣式中如何設置中文字體?

代碼如下: .selector{font-family: SimHei,"微軟雅黑",sans-serif;} 注意&#xff1a;加上中文名“微軟雅黑”是為了兼容opera瀏覽器&#xff0c;中文字體名必須加上引號&#xff08;單引號雙引號都可以&#xff09;。 MicrosoftJhengHei為微軟正黑體&#xff0c;STH…

前端做CRM管理系統是做什么_代辦行業的CRM客戶關系管理系統應該是什么樣子的?...

隨著互聯網的深耕細化&#xff0c;很多企業也在不斷優化自己的辦公方式&#xff0c;以優化企業的辦公流程&#xff0c;提高企業的辦事效率。因此實現辦公自動化&#xff0c;或者說實現數字化辦公就需要逐漸提上日程。今天給大家講講可以幫助代辦行業實現辦公自動化的產品&#…

(譯) JSON-RPC 2.0 規范(中文版)

http://wiki.geekdream.com/Specification/json-rpc_2.0.html 起源時間: 2010-03-26(基于2009-05-24版本) 更新: 2013-01-04 作者: JSON-RPC工作組< json-rpcgooglegroups.com > 原文鏈接: http://www.jsonrpc.org/specification翻譯: leozvc < xxfs91gmail.com >…

ios pusher使用_如何使用JavaScript和Pusher實時更新用戶狀態

ios pusher使用by Rahat Khanna通過拉哈特漢娜 如何使用JavaScript和Pusher實時更新用戶狀態 (How to update a User’s Status in realtime using JavaScript and Pusher) “Hey, what’s up?” is not a phrase we need to ask someone these days. These days knowing wha…

python + pyqt5 UI和信號槽分離方法

初級菜鳥&#xff0c;知識點記錄。 每次重新生成UI.py文件的時候&#xff0c;里面的按鈕方法都會被清除&#xff0c;想一個方法可以把按鈕響應方法放到外面&#xff0c;利于維護。 新建一個按鈕文件并繼承UI代碼&#xff0c;把信號槽及按鈕響應方法寫在按鈕文件里面&#xff0c…

學習之路~sqh

推薦博客 Edison Chou&#xff1b;Vamei&#xff1b;算法?面試專題 - 簡書&#xff1b;xingoo - 博客園&#xff1b;設計模式 極速理解設計模式系列【目錄索引】- Caleung&#xff1b;Net設計模式 - 靈動生活&#xff1b;宅男程序員給老婆的計算機課程系列&#xff1b;C設計模…

python format函數保留兩位小數_python format函數

在Python 3.0中&#xff0c;%操作符通過一個更強的格式化方法format()進行了增強。對str.format()的支持已經被反向移植到了Python 2.6在2.6中&#xff0c;8-bit字符串和Unicode字符串都有一個format()方法&#xff0c;這個方法會把字符串當作一個模版&#xff0c;通過傳入的參…

藍牙 sig base uuid_藍牙模塊采用陶瓷天線和PCB天線的區別

一、陶瓷天線陶瓷天線是一種適合于藍牙設備使用的小型化天線,又分為塊狀陶瓷天線和多層陶瓷天線。陶瓷天線占用空間很小、性能比較好&#xff1b; 帶寬窄&#xff0c;比較難做到多頻段&#xff1b;有效提高主板的整合度&#xff0c;并可降低天線對ID的限制&#xff1b;需要在主…

kubernetes系列12—二個特色的存儲卷configmap和secret

本文收錄在容器技術學習系列文章總目錄 1、configmap 1.1 認識configmap ConfigMap用于保存配置數據的鍵值對&#xff0c;可以用來保存單個屬性&#xff0c;也可以用來保存配置文件。ConfigMap跟secret很類似&#xff0c;但它可以更方便地處理不包含敏感信息的字符串。 1.2 創建…

華為完成拉美銅網寬帶G.fast技術部署測試

1/11/2016,英國大東通信巴拿馬分公司日前與華為公司發布消息稱&#xff0c;覆蓋拉丁美洲地區的最快銅纜寬帶服務系統成功完成初次測試。 作為巴拿馬地區領先的移動寬帶服務提供商&#xff0c;大東通信巴拿馬分公司也是當地最大的電信服務提供商&#xff0c;此次與華為合作在現有…

kotlin調用類中的方法_一種輕松的方法來測試Kotlin中令人沮喪的靜態方法調用

kotlin調用類中的方法by Oleksii Fedorov通過Oleksii Fedorov 一種輕松的方法來測試Kotlin中令人沮喪的靜態方法調用 (A stress-free way to test frustrating static method calls in Kotlin) Let me make a wild guess… You have encountered some code in Kotlin that is …

python圖像加密模塊_使用Pycryp的圖像加密和解密

這和加密或解密文本是一樣的。示例首先導入一些模塊&#xff1a;from Crypto.Cipher import AESfrom Crypto import Random然后&#xff0c;讓我們生成一個鍵和一個初始化向量。key Random.new().read(AES.block_size)iv Random.new().read(AES.block_size)加密下面的代碼加載…

遇到attemp to invoke virtual method

這個很大原因是沒有預先初始化sdk&#xff0c;檢查application的配置是否配置了application&#xff1a;name 轉載于:https://www.cnblogs.com/caimuqing/p/5894099.html

app啟動頁自動跳轉源碼_關于移動端App啟動頁的策劃方案

App啟動頁是指app在啟東時需要加載必要的運行環境和配置&#xff0c;在這個過程中提示用戶等待的一個過渡頁面。在產品經理眼里啟動頁是app給予用戶重要的第一印象&#xff1b;也是App最重要的黃金頁面之一&#xff0c;所有用戶100%都會看到的頁面。啟動頁適合用來做以下幾個事…

電信運營商占IDC市場65%:中國電信占行業半數以上

隨著云計算、大數據的快速發展&#xff0c;作為重要基礎設施的IDC數據中心也在高速擴張。 近日&#xff0c;DCA常務理事長何寶宏介紹&#xff0c;我國規劃在建數據中心共計246個&#xff0c;總設計機架數約為103萬個&#xff0c;總設計服務器規模約1326萬臺。在用超大型、大型數…

Python 日期和時間戳的轉換

Python 日期和時間戳的轉換 1. Python中處理時間的模塊 Python中處理時間的模塊有time、datetime和calendar。 在Python中表示時間的方式&#xff1a; 時間戳&#xff1a;10位整數位和若干小數位&#xff0c;例如 1551153156.6358607元組&#xff08;struct_time&#xff09;: …

快應用比賽_我的應用如何在國際學生比賽中獲得第三名

快應用比賽by Rafael Melo通過拉斐爾梅洛 我的應用如何在國際學生比賽中獲得第三名 (How my App won third place in an International Student Competition) I developed an App that won third place at the IEEE Mobile Applications Development Contest 2017 (IEEEmadC 2…

JAVA中String類的intern()方法的作用

一般我們變成很少使用到 intern這個方法&#xff0c;今天我就來解釋一下這個方法是干什么的&#xff0c;做什么用的 首先請大家看一個例子&#xff1a; public static void main(String[] args) throws Exception { String a "b" ; String b "b" ; …