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

芝士和奶酪一样吗

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/

芝士和奶酪一样吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值