系统学习做汉堡

by Sihui Huang

黄思慧

您如何学习策略设计模式? 做一个汉堡! (How can you learn the Strategy Design Pattern? Make a hamburger!)

Do you know how to order a burger?

你知道如何点汉堡吗?

If so, I have good news for you. Then you know how to use one of the most commonly used design patterns, strategy pattern!

如果是这样,我对你有个好消息。 然后,您知道如何使用最常用的设计模式之一,即策略模式 !

“How so?” you might ask. Well, let’s take a look at the features of strategy pattern.

“为何如此?” 你可能会问。 好吧,让我们看一下策略模式的功能。

  • It defines a family of algorithms.它定义了一系列算法。
  • It encapsulates each algorithm.它封装了每种算法。
  • It makes the algorithms interchangeable within that family.它使算法在该系列中可以互换。

Strategy pattern lets the algorithm vary independently from clients that use it.

策略模式使算法可以独立于使用该算法的客户端而变化。

How does this have anything to do with burgers?

这与汉堡有什么关系?

Let’s think about burgers for a second.

让我们考虑一下汉堡。

There are many varieties of burgers: veggie burger, cheeseburger, grilled chicken burger, and double cheeseburger to name a few. All of them share the same format: the top bun + patty + bottom bun.

汉堡的种类很多:素食汉堡,芝士汉堡,烤鸡肉汉堡和双层芝士汉堡。 它们都共享相同的格式:顶部面包+小馅饼+底部面包。

It’s the patty that makes each burger different. A cheeseburger has cheese and a beef patty in the middle, whereas a grilled chicken burger has a grilled chicken breast patty.

是使每个汉堡与众不同的肉饼。 芝士汉堡的中间是奶酪和牛肉饼,而烤鸡肉汉堡的是烤鸡胸肉饼。

Let’s recap the strategy pattern definition in terms of burgers. What is the family of algorithms in terms of burgers? It’s the family of different patties:

让我们来回顾一下汉堡的战略模式定义。 关于汉堡的算法家族是什么? 这是不同馅饼的家族:

  • patty for a chicken burger = [grilled chicken breast]鸡肉汉堡小馅饼= [烤鸡胸肉]
  • patty for a cheeseburger = [cheese + beef patty]芝士汉堡小馅饼= [奶酪+牛肉小馅饼]
  • patty for a double cheeseburger = [cheese + beef patty + cheese + beef patty]双芝士汉堡的肉饼= [奶酪+牛肉饼+奶酪+牛肉饼]

They are encapsulated and interchangeable with each other. Swap out the chicken burger patty with the cheeseburger patty, and you get a cheeseburger.

它们被封装并且可以互换。 将鸡肉汉堡肉饼和芝士汉堡肉饼交换掉,您会得到一个芝士汉堡。

Strategy lets the algorithm vary independently from clients that use it.

策略使算法独立于使用该算法的客户端而变化。

You can order any burger you like. But for a chef, making a burger follows the same general procedure: prepare the bun, cook the patty, and then put the patty in-between the top and bottom buns.

您可以订购任何喜欢的汉堡。 但是对于厨师来说,制作汉堡的过程与一般相同:准备面包,煮小馅饼,然后将小馅饼放在上下面包之间。

A burger is a real-life example of using strategy pattern.

汉堡是使用策略模式的真实示例。

Let’s take a look at the code.

让我们看一下代码。

There are three participants in the strategy pattern.

该策略模式有三个参与者。

Strategy declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a concrete strategy.

策略声明了所有支持的算法通用的接口。 上下文使用此接口来调用由具体策略定义的算法。

Concrete strategy implements the algorithm using the StrategyInterface.

具体策略使用StrategyInterface实现算法。

Context is configured with a ConcreteStrategy Object; maintains a reference to a strategy object; may define an interface that lets strategy access its data.

上下文是用ConcreteStrategy对象配置的; 维护对策略对象的引用; 可以定义一个接口,让策略访问其数据。

GrilledChickenStuffing and BeefPattyStuffing are our concrete strategies. Each defines how its stuffing should be cooked. The Burger class is our context. It’s configured with a concrete strategy and it uses the concrete strategy later when a burger needs to be cooked.

GrilledChickenStuffing和BeefPattyStuffing是我们的具体策略。 每个都定义了馅料应该如何烹饪。 Burger班是我们的背景。 它配置有具体的策略,并且在以后需要烹饪汉堡时会使用具体的策略。

In our burger example, we don’t have a specific class that declares what interface the concrete strategies should implement. That’s because we don’t need to, thanks to Ruby’s duck typing. If it walks like a duck and talks like a duck, it’s a duck. If it can be cooked as a burger patty, it is a burger patty.

在我们的汉堡示例中,我们没有特定的类来声明具体策略应实现的接口。 这是因为由于Ruby的鸭子输入,我们不需要。 如果它走路像鸭子,说话像鸭子,那就是鸭子。 如果可以将其制成汉堡肉饼,则为汉堡肉饼。

Strategy declares the interface for which a concrete strategy should implement and for which context can use. As you can see in the above code, both GrilledChickenStuffing and BeefPattyStuffing implements the cook method, and that’s the method the user of a concrete strategy, aka the context, expects a concrete strategy to provide.

策略声明接口,具体策略应针对该接口实施,并且上下文可以使用。 如您在上面的代码中看到的,GrilledChickenStuffing和BeefPattyStuffing都实现了cook方法,这就是具体策略(也称为上下文)的用户所期望的一种具体策略。

战略模式的关键思想 (The key idea of the strategy pattern)

The key to the strategy pattern is to pull the varying algorithms out into a separate object. These objects become a family of algorithms the context can choose from. Each of these objects, aka the strategies, does the same job and supports the same interface.

策略模式的关键是将各种算法引入单独的对象中。 这些对象成为上下文可以选择的一系列算法。 这些对象(也称为策略)均执行相同的工作并支持相同的接口。

In our burger example, we have different patty strategies for a burger. And each of the concrete burger patty strategy supports the same interface by implementing the cook method.

在我们的汉堡示例中,我们对汉堡有不同的馅饼策略。 每个具体的汉堡肉饼策略都通过实现cook方法来支持相同的界面。

It’s all about composition. The user has a strategy and delegates. The user of the strategy delegates the job. In our example, a burger has a patty, and it delegates the job of cooking.

一切都与构图有关。 用户有一个策略和代表。 策略的用户委派工作。 在我们的示例中,一个汉堡有一块馅饼,它委派了烹饪工作。

战略模式的优势 (Advantages of the strategy pattern)

  • It achieves better separation of concerns by pulling out a set of strategies from a class and relives the Burger class of any responsibility for our knowledge of the stuffing.

    它通过从班级中撤出一套策略来更好地分离问题,并使Burger班级对我们对馅料的了解负有任何责任。

  • It makes it easy to switch strategies at runtime because the pattern is based on composition and delegation, rather than on inheritance.它使运行时切换策略变得容易,因为该模式基于组成和委派而不是继承。

考虑使用策略模式时需要注意的事项 (Things to watch out for while considering using the strategy pattern)

  • Data passing between context and strategy. If the implementation of a concrete strategy requires data from the context, you can either pass the data as parameters to a concrete strategy or pass the context itself into a concrete strategy. This is so that the concrete strategy can have access to the data through the context. Whichever way you choose, watch out for the tangling of the context and the concrete strategy.在上下文和策略之间传递数据。 如果具体策略的实施需要上下文中的数据,则可以将数据作为参数传递给具体策略,也可以将上下文本身传递到具体策略中。 这样,具体策略就可以通过上下文访问数据。 无论选择哪种方式,请注意上下文和具体策略的纠缠。
  • Double check if you actually need the strategy pattern, the template method, or the decorator pattern.

    仔细检查您是否确实需要策略模式, 模板方法或装饰器模式 。

策略模式中使用的设计原则 (Design principles used in the strategy pattern)

  • Encapsulate what varies封装变化
  • Favor composition over inheritance偏重于继承而不是继承
  • Program to interfaces, not implementation程序到接口,而不是实现

Now you have learned about the strategy pattern.

现在您已经了解了策略模式。

Here’s an important question: what’s your favorite burger strategy? :)

这是一个重要的问题:您最喜欢的汉堡策略是什么? :)

I publish to sihui.io weekly.

我每周出版sihui.io 。

Subscribe so you won’t miss the next article from the series.

订阅,这样您就不会错过该系列的下一篇文章。

Next time we will take a look at the template method and …

下次,我们将看一下模板方法和…

翻译自: https://www.freecodecamp.org/news/how-can-you-learn-the-strategy-design-pattern-make-a-hamburger-a6ad4332b838/

系统学习做汉堡

系统学习做汉堡_您如何学习策略设计模式? 做一个汉堡!相关推荐

  1. python可以神奇的做什么_可以用 Python 编程语言做哪些神奇好玩的事情?

    源自于一个用Python写各种小游戏的github合集,star数1k.除了坦克大战外,还包含滑雪者.皮卡丘GOGO.贪吃蛇.推箱子.拼图等游戏. 帮助你快速生成属于自己的铅笔画,单色或是彩铅都可以. ...

  2. 怎么做抽奖活动_没有公众号怎么做刮刮乐链接

    1,虽然我们分享的是公众号里怎么添加刮刮乐小程序,但是如果我们没有公众号也是可以制作刮刮乐小程序呢,因为刮刮乐小程序会有独立的链接和二维码,只要对于很多小伙伴不想使用微信公众号的来说,就大大的方便了, ...

  3. 分享微信抽奖怎么做步骤_没有公众号可以做刮刮乐抽奖链接吗

    1,虽然我们分享的是公众号里怎么添加刮刮乐小程序,但是如果我们没有公众号也是可以制作刮刮乐小程序呢,因为刮刮乐小程序会有独立的链接和二维码,只要对于很多小伙伴不想使用微信公众号的来说,就大大的方便了, ...

  4. 社区团购系统存在的意义_社区团购怎么做

    社区团购系统比传统批发成本更低 前两年社区团购来了一波千团大战,资本巨头疯狂烧钱,最后国家叫停了这波垄断,因为巨头们已经把手伸到了民生领域了,菜篮子是影响国家稳定的,巨头们无非是想先砸钱再垄断,最后再 ...

  5. 中两个数做减法_人生下半场,学会做减法

    作者:洞见·余生 人生如逆旅,你我皆行人. 梭罗在瓦尔登湖中写道: "一个人,只要满足了基本生活所需,不再汲汲于声名,不再汲汲于富贵,便可以更从容,更充实地享受人生." 曾经觉得, ...

  6. dsge模型难做吗_百度Seo优化好做吗,现在做是不是越来越难做?

    最近爆出了猛料,百度14年来首个季度亏损,对于seoer的内心又蒙上了一层阴影,到底现在的seo优化还好做吗?是不是越来越难做了?这个问题蝙蝠侠IT是这么看的对于现在的seo没有难不难做的说法,只是操 ...

  7. cv岗工作做什么_只会python能做什么工作

    现在互联网巨头,都已经转投到人工智能领域,而人工智能的首选编程语言就是python,未来前景显而易见.那么问题来了,想学Python,Python工程师工资一般多少?值得去学吗? 说句实在话,还得看你 ...

  8. python运维主要做什么_运维工程师主要做什么?

    很多想从事云计算运维工程师岗位的人,都想知道自己应该会什么样的技术.运维工程是都需要会什么技术?才会被企业所认可,才能找到一份专业的工作.今天传智播客就来说说运维工程师主要做什么. 运维工程师(Ope ...

  9. python做兼职_“隐形贫困者”自学Python做副业,教你如何月入10000+

    现在的年轻人总是觉得攒不下钱,虽然表面风光,每天出入高级CBD,可是几乎每个人都是月光族,这一类人被定义为"隐形贫困者",原因是什么呢?根据小编的分析,现在人们对生活质量要求普遍提 ...

最新文章

  1. SAP QM QA08批量维护QMAT数据
  2. Singleton Pattern
  3. SCROLLINFO结构详解
  4. iOS限定UITextField的输入格式
  5. 计算机名字更改时不显示文字,教大家电脑中文件夹不显示名字怎么办
  6. python语言怎么学-如何从零开始学习Python,python语言编程入门
  7. 2020 idea 查看内存消耗_idea内存如何设置
  8. win7下wifi密码的保存路径
  9. DOM和BOM的区别
  10. 腾讯回应两块八卖《鬼谷八荒》:已下架并退款;iMac Pro 售完即停产;iCloud 识别姓氏闹乌龙 | 极客头条...
  11. 关于JVM的几个垃圾收集算法思想
  12. 2021-07-13 CNN池化理解学习
  13. 国内的健身房管理系统榜单-捷径系统
  14. EDM广告邮件代码内容编写参考建议
  15. 关于抢红包的_关于抢红包的作文600
  16. MATLAB快速读取STL文件
  17. 华为交换机和锐捷交换机端口隔离
  18. python import turtle as t_turtle库的学习笔记(python)
  19. arduino/Mixly使用LGT8F328P
  20. Python | 阿尔法基本图形绘制

热门文章

  1. 腾讯游戏助手运行闪退日志查看
  2. 数据库 explain详解
  3. 实现微信向多好友定时发送信息
  4. Java swing的功能测试类库 FEST-Swing
  5. 什么是大数据?什么是物联网?
  6. iocp端口断开_在完成端口IOCP模型判断客户端是否已关闭连接(掉线) | 学步园
  7. my ReadFood_Nanking Delicious / food / delicious
  8. win10 docker 启动 mysq MySQL Server. Supplied value : /var/lib/mysql-files
  9. 通过IDM提高QQ群文件下载速度
  10. 戒不掉的温柔 (残月