芝士和奶酪一样吗

by Sihui Huang

黄思慧

使用简单工厂设计模式就像制作芝士蛋糕一样 (Using the Simple Factory design pattern is a lot like making cheesecake)

Factory Patterns are about encapsulating object creation.

工厂模式是关于封装对象创建的。

But before diving into details of the patterns, let’s talk about cheesecake. Because cheesecake is about … happiness! ???

但是在深入研究模式细节之前,让我们先谈谈芝士蛋糕。 因为芝士蛋糕是关于……幸福! ???

Let’s focus our gaze on six of my personal favorites: Original Cheesecake, Ore0 Cheesecake, Coffee Cheesecake, Tiramisu Cheesecake, S’mores Cheesecake, and Hazelnut Cheesecake.

让我们将注意力集中在我的六个个人最爱上:原始芝士蛋糕,Ore0芝士蛋糕,咖啡芝士蛋糕,提拉米苏芝士蛋糕,S'mores芝士蛋糕和榛子芝士蛋糕。

And here is how we make a cheesecake:

这是我们做芝士蛋糕的方法:

Create a cheesecake instance based on the selected type -> Make crust -> Add layers on top of the crust -> Bake it -> Refrigerate it -> Add toppings to the cake -> Return the cake! ???

根据选定的类型创建一个芝士蛋糕实例->制作外壳->在外壳上添加图层->烘烤->冷藏->为蛋糕添加浇头->返回蛋糕! ???

Wait … that Mango key lime cheesecake looks very tempting ???.

等一下……芒果关键石灰乳酪蛋糕看起来很诱人???。

Let me add it to my list:

让我将其添加到列表中:

One second …

一秒 …

I have been having too much caffeine lately. I don’t want the coffee cheesecake to be on my list anymore. Let me update the make_cheesecake method again.

我最近一直喝太多咖啡因。 我不希望咖啡芝士蛋糕列入我的名单。 让我再次更新make_cheesecake方法。

Oooh…. they have a low carb version of cheesecake. It’s always nice to have a low carb option. It needs to be on my list!

哦。 他们有低碳水化合物的芝士蛋糕。 低碳水化合物的选择总是很高兴。 它必须在我的清单上!

So since the first time we defined make_cheesecake, we have updated it three times. Each time, the change was for the exact same reason — to update my cheesecake list. And everything else, make_crust, add_layers, bake, refrigerate, and add_toppings, remained the same.

因此,自从我们第一次定义make_cheesecake ,我们已经对其进行了三次更新。 每次更改都是出于完全相同的原因-更新我的芝士蛋糕列表。 和其他一切, make_crust add_layers bake refrigerate add_toppings ,保持不变。

Sorry for changing my mind every three seconds. But as they say: change is the only constant in life (and software development).

很抱歉每三秒钟改变一次主意。 但是正如他们所说: 改变是生命(和软件开发)中唯一的不变因素。

To be honest, we will need to change the list at least one more time: pumpkin cheesecake will be available from September. It’s WORLD FAMOUS! Without a doubt, we need to add it to the list once September arrives. Oops, that means we need to remove it from the list when the holiday season passes.

老实说,我们至少需要再更改一次此列表:南瓜芝士蛋糕将从9月开始提供。 举世闻名! 毫无疑问,一旦9月到来,我们需要将其添加到列表中。 糟糕,这意味着我们需要在假期结束后将其从列表中删除。

It’s obvious that my cheesecake list changes often.

很明显,我的芝士蛋糕列表经常更改。

There is a design principle: encapsulate what varies.

有一个设计原则: 封装变化的东西

We should give it a try.

我们应该尝试一下。

该是芝士蛋糕工厂了! (It’s time for a Cheesecake Factory!)

The CheesecakeFactory is a simple class. All it does is create and return the correct cheesecake based on a given type.

CheesecakeFactory是一个简单的类。 它所做的只是根据给定的类型创建并返回正确的芝士蛋糕。

With the help of CheesecakeFactory, the make_cheesecake method becomes much simpler.

有了CheesecakeFactory的帮助, make_cheesecake方法变得更加简单。

The make_cheesecake method can now focus on the actual steps that go into making a cheesecake without having to worry about different cheesecake types.

现在, make_cheesecake方法可以专注于制作芝士蛋糕的实际步骤,而不必担心不同的芝士蛋糕类型。

Our CheesecakeFactory is an example of using the Simple Factory. Simple Factory is used for encapsulating object creation.

我们的CheesecakeFactory是使用简单工厂的示例。 简单工厂用于封装对象创建。

工厂模式家族 (The Factory Pattern Family)

Besides Simple Factory, there are two other members of the Factory Pattern family: Factory Method and Abstract Factory. We won’t go into the details of these two patterns.

除了简单工厂,工厂模式家族还有另外两个成员: 工厂方法抽象工厂。 我们将不介绍这两种模式的细节。

In a nutshell, Factory Method and Abstract Factory use inheritance. Factory Method is about creating one type of object, and Abstract Factory is about creating a family of different types of objects. All three of them are about encapsulating object creation by using the design principle: encapsulate what varies.

简而言之,工厂方法和抽象工厂使用继承。 工厂方法是关于创建一种类型的对象,而抽象工厂是关于创建一系列不同类型的对象的。 所有这三个都是关于使用设计原理封装对象创建的:封装变化的东西。

使用简单工厂的好处 (Benefits of using Simple Factory)

Pulling the logic of creating the correct cheesecake based on a given type is a small move that gives us lots of benefits. The biggest benefit is that we can modify the cheesecake list without touching the make_cheesecake method and its test. All we need to do is update the CheesecakeFactory class and leave make_cheesecake and its test alone.

采取基于给定类型创建正确的芝士蛋糕的逻辑,这是一小步,可为我们带来很多好处。 最大的好处是,我们无需修改make_cheesecake方法及其测试即可修改芝士蛋糕列表。 我们需要做的就是更新CheesecakeFactory类并保留make_cheesecake 及其测试。

We want to separate the parts that vary often from the stable parts. Because each time we modify a part of our code, we might introduce bugs. The parts that vary are the fragile parts of our system. We want to keep the stable parts away from the fragile parts. So if we did introduce bugs when updating a part of the system, it would be easier for us to locate the bug.

我们希望将经常变化的部分与稳定部分分开。 因为每次修改部分代码,我们可能都会引入错误。 变化的部分是我们系统的脆弱部分。 我们要使稳定部分远离易碎部分。 因此,如果在更新系统的一部分时确实引入了错误,那么对我们而言,查找错误将更加容易。

外卖: (Takeaways:)

  1. Factory Patterns are used for encapsulating object creation.

    工厂模式用于封装对象创建。

  2. Design Principle: encapsulate what varies.

    设计原理:封装各种内容。

I need to run to get a cheesecake now.

我现在要跑来拿起芝士蛋糕。

Don’t forget to subscribe so you won’t miss the next post!

不要忘记订阅,这样您就不会错过下一篇文章!

Next time, we will take a look at some waaaaaaaaffles!

下次,我们将看一些waaaaaaaaffles!

翻译自: https://www.freecodecamp.org/news/using-the-simple-factory-design-pattern-is-a-lot-like-making-cheesecake-92a119cde191/

芝士和奶酪一样吗

芝士和奶酪一样吗_使用简单工厂设计模式就像制作芝士蛋糕一样相关推荐

  1. 简单工厂设计模式与工厂设计模式

    工厂设计模式的目的:降低调用者和实现者之间的耦合度.当需求发生变化(增加或改变)时,不改变原有代码的结构. 1.简单工厂设计模式 优点:降低了调用者和实现者之间的耦合度.当引入一个新的实现类型时,只需 ...

  2. 【简单工厂设计模式】

    一:Simple Factory(简单工厂) 1. 何为简单工厂设计模式:                                             在创建一个对象的时候不向客户暴露内部 ...

  3. 设计模式——简单工厂设计模式

    简单工厂设计模式 建立一个工厂类,对实现了同一接口的类进行实例的创建.简单工厂模式的实质是有由一个工厂类根据传入的参数动态的决定应该创建哪一个实现类. /** 简单工厂设计模式* *///抽象接口水果 ...

  4. 工厂设计模式、简单工厂设计模式、抽象工厂设计模式

    一.简单工厂设计模式.工厂设计模式,抽象工厂设计模式总体分析 简单工厂模式 简单工厂模式不是23种里的一种,简而言之,就是有一个专门生产某个产品的类. 比如下图中的鼠标工厂,专业生产鼠标,给参数0,生 ...

  5. Qt下实现简单工厂设计模式

    简述 简单工厂设计模式是一种基础简单而常用的设计模式,对理解封装.继承和多态特性有非常大的帮助.下面就来在Qt下实现简单工厂模式. 代码之路 实现简单工厂至少需要四个类.其中最主要的两个类是工厂类和一 ...

  6. Java设计模式之简单工厂设计模式

    工厂设计模式 简单工厂设计模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂设计模式是工厂设计模式家族中最简单实用的模式 我们还是通过例子进行说明 实例场景:假设就是一个工厂生产phone和t ...

  7. 从零开始单排学设计模式「简单工厂设计模式」黑铁 III

    阅读本文大概需要 2 分钟. 本篇是设计模式系列的第二篇,虽然之前也写过相应的文章,但是因为种种原因后来断掉了,而且发现之前写的内容也很渣,不够系统.所以现在打算重写,加上距离现在也有一段时间了,也算 ...

  8. java设计模式--简单工厂设计模式SimpleFactoryPattern

    简单工厂模式     简单工程模式又称静态工厂方法,它的核心是类中包含一个静态方法,该方法用于根据参数来决定返回实现    同一接口不同类的实例. public class SimpleFactory ...

  9. 简单工厂设计模式的好处

    //盗墓者是个丑奴儿,原 我曾纳闷为什么要单独创建对象,为什么不直接new对象使用?好处就在这儿,想想方法吧,为什么要设计方法? 方法是一片代码的封装,当方法功能升级的时候,直接去改方法中的代码,所有 ...

最新文章

  1. 小蠢笔记:从继承特性来看构造函数
  2. php组合查询,PHP组合查询多条件查询实例代码第1/2页
  3. Kudu : 删除了不存在的数据报错 status=Not found: key not found (error 0)
  4. 深度学习这么调参训练_聊一聊深度学习中的调参技巧?
  5. 创建 Refs 的几种方式
  6. java中ajax的使用jar包_AJAX Java示例
  7. 《Ray Tracing in One Weekend》——Chapter 4: Adding a sphere
  8. [postgresql]postgresql的VACUUM 介绍
  9. rsyslog及loganalyzer
  10. 51单片机 多机串口通讯实验与双机串口通讯实验及proteus仿真
  11. Gitbook文档翻译
  12. APICloud的config.xml应用配置的说明
  13. 使用ftp访问资源管理器托拽文件下载出现“当前的安全设置不允许从该位置下载文件”提示
  14. 排序之low逼三人组及其python代码实现
  15. 微信公众号开发--公众号关注推送配置与菜单配置
  16. 当@PathVariable遇上了.
  17. 汉诺塔(河内塔)问题
  18. 如何在前端实现甘特图?SpreadJS两招搞定
  19. c++ 实现线索二叉树
  20. 百度EasyDate线上协同数据标注平台使用

热门文章

  1. C语言 mmap ( 建立内存映射 ) 、munmap ( 解除内存映射 )
  2. 以太网模块支持SCADA 软件通过三菱以太网协议连接
  3. Java面试题(十九) 细说线程池秘境“七大参数”护法的身世
  4. 大数据练习题mysql之求连续出勤和连续最大登陆天数
  5. dra7xx cpu frequency change
  6. pyepics PV -- 2
  7. 暂停后重开笔记本电脑音量突然变大
  8. 开源许可协议:GPL、LGPL、AGPL、MPL和BSD、MIT、Apache
  9. vue2和vue3的区别(由浅入深)
  10. 信号隔离器在PLC/DCS控制系统的应用