headstart系统设计设计模式直觉

The other day, a friend asked me on how I go about choosing a design pattern in a given scenario, because it was not always very clear.

有一天,一个朋友问我如何在给定场景中选择设计模式,因为这并不总是很清楚。

Some design patterns are similar in terms of functionality (i.e. factory vs. builder)

某些设计模式在功能上是相似的(例如,工厂与制造商)

I presented my intuitions via chat, and at the end of the discussion we both realized that with these intuitions for some of the design patterns in mind, choosing next time among them will be easier.

我通过聊天介绍了我的直觉,在讨论的最后,我们都意识到,考虑到一些设计模式的直觉,在它们中进行下一次选择将更加容易。

Hence, I thought of documenting and sharing it with everyone.

因此,我想到了记录文档并与所有人共享。

FactorySay that you are shopping from a factory outlet of a clothing company. You will only get t-shirts based on the size grades i.e. S, M, L, XL etc.

工厂假设您正在服装公司的一家工厂购物。 您只会根据尺寸等级(例如S,M,L,XL等)获得T恤。

Hence, single input from our side i.e. S, M, L, XL etc. will define the whole t-shirt.

因此,我们这边的单一输入(即S,M,L,XL等)将定义整个T恤。

Similarly, when you want to specify a single input argument like a keyword or an ENum, and get a variation of the main abstract class/interface TShirt created for you like SmallSizeTShirt, then you should go with Factory pattern.

类似地,当您想要指定单个输入参数(如关键字或 ENum )并获得 为您创建 的主要 抽象类/接口 TShirt 的变体( SmallSizeTShirt)时 ,则应使用 Factory 模式。

Similar example is covered here, with abstract class/interface Person and its custom variations Villager and CityPerson created for use that are created on the basic of a single input, the enum PersonType.

这里涵盖了类似的示例,其中包含抽象类/接口Person及其创建的自定义变体VillagerCityPerson以供使用,这些变体是在单个输入枚举PersonType的基础上创建的。

Image for post
Wikipedia 维基百科

Builder

建造者

Now say that instead of going to a factory outlet, you want to have something that fits you better, so you went to a tailor for custom tailored shirt.

现在说,您想去一家更适合自己的东西,而不是去工厂直销,所以您去了一家裁缝店,专门定制衬衫。

Now, the tailor will take note of various other inputs like your chest size, your arm length, your shoulder length etc.

现在,裁缝会记下其他各种输入,例如您的胸部大小,手臂长度,肩膀长度等。

Hence, you can see that the tailor doesn’t have grades based on only one input like size, instead he has a number of input parameters and is makes unique shirts for each customer based on the input values for each of them.

因此,您可以看到裁缝师没有仅基于一个输入(例如尺寸)来评分,而是拥有许多输入参数,并且根据每个客户的输入值为每个客户制作了唯一的衬衫。

Here, tailor is synonymous with a builder.

在这里,裁缝是建造者的代名词。

Hence, when there are multiple inputs from your side to construct a variation of the main abstract class/interface Shirt, use ShirtBuilder to return a CustomShirt for you based on various inputs.

因此,当有从你身边多个输入来构建主要 抽象类/接口衬衫 的变化 ,使用 ShirtBuilder 您基于各种输入 返回 CustomShirt。

Similar example is covered here, with ICarBuilder using various inputs like NumDoors, Colour, BrandName and ModelName and some code logic to construct a Ferrari 488 Spider Car.

类似的例子是使用各种输入像NumDoors, 颜色名优产品MODELNAME和一些代码逻辑构建法拉利488蜘蛛汽车这里覆盖,ICarBuilder。

Image for post
Wikipedia 维基百科

Singleton

辛格尔顿

Suppose, you have a DB connection to initialize, and you want this DB connection to be established with your DB, and then every time you want to query for some data in a function, you get the same Connection object in establishedConnection state returned, to quickly query data.

假设您有一个数据库连接要初始化,并且希望与数据库建立此数据库连接,然后每次您要查询函数中的某些数据时,都将返回一个已建立状态相同Connection对象 ,以快速查询数据。

In such scenarios where, not only do you want the same type of object to be returned, but you also want it to have a given state always, and for it to be a static method call to get that object, then you use the Singleton pattern.

在这种情况下,不仅要返回相同类型的对象,而且还希望它始终具有给定状态,并且要使其成为 获取该对象 静态方法调用 ,然后使用 Singleton 模式。

Additional feature: A thread safe singleton can be created so that singleton property is maintained even in multi-threaded environment.

附加功能: 可以创建线程安全的单例,以便即使在多线程环境中也可以维护单例属性。

To make a singleton class thread-safe, getInstance() method is made synchronized so that one one thread can access it at a time.

为了使单例类线程安全,请使getInstance()方法同步,以便一个线程一次可以访问它。

Image for post
Wikipedia 维基百科

Mixins

混合蛋白

Suppose you are being told to make a system to accept pizza orders for a restaurant.

假设您被告知要建立一个系统来接受餐馆的比萨饼订单。

Here, class Pizza itself has many variations like ThinCrust, ThickCrust, ExtraCheese etc.

在这里, 匹萨类本身具有许多变体,如ThinCrustThickCrustExtraCheese等。

And, on-top of pizza you have a extra class like class Topping which has variations like Mushroom, BlackOlives, Pineapple etc.

而且,在披萨的顶部,您还有一门额外的课程,例如“ Topping”课程 里面有蘑菇黑橄榄菠萝变种

Here, any Topping can go with any Pizza and vice versa.

在这里,任何摘心可以与任何比萨 ,反之亦然去。

Hence, for this kind of multiple inheritance requirement where:1. You want to provide a lot of optional features for a class.2. You want to use one particular feature in a lot of different classes.

因此,对于这种多重继承要求,其中:1。 您想为一个类提供很多可选功能。2。 您想在许多不同的类中使用一种特定功能。

For cases like these, you can use the Mixin pattern.

对于此类情况,可以使用 Mixin 模式。

Note: Mixins only exist in languages that support multiple-inheritance. You can’t do a mixin in Java or C#.

注意 :Mixins仅在支持多重继承的语言中存在。 您不能使用Java或C#进行混合。

Image for post
Mixin Design Pattern (Pizza-Topping Example) in Python
Python中的Mixin设计模式(Pizza-Topping示例)
Image for post
Output for above example
以上示例的输出

Adapter

适配器

Suppose that you have to build an app interface for users to orders food from different restaurants. Here, menu shown to the user on your app is same for every restaurant, but the order information that each restaurant gets in slightly different.

假设您必须构建一个应用程序界面供用户从不同的餐厅订购食物。 在这里,您的应用程序向用户显示的菜单对于每个餐厅都是相同的,但是每个餐厅的订单信息会略有不同。

In cases like these, where one common input has to be mapped to different kinds to input or actions taken we should use the Adapter Pattern.

在这种情况下,必须将一个公共输入映射到不同种类以进行输入或采取的操作,我们应该使用 适配器 模式。

Intuition is that adapters like charging adapter in real world do exactly the same thing. They send normal AC current coming at a fixed voltage-current range from the main socket into different charger pins for different devices like camera, phone, usb charger pin etc., all with their own voltage-current range.

直觉 是,像现实世界中的充电适配器这样的适配器会做完全相同的事情。 它们将来自固定插座的正常AC电流以固定的电压-电流范围发送到用于不同设备(如相机,电话,USB充电器引脚等)的不同充电器引脚,所有这些引脚都有自己的电压-电流范围。

Similar example is covered here, where different charging styles for LightningPhone and MicroUsbPhone are defined via interfaces.

这里涵盖了类似的示例,其中通过接口定义了LightningPhoneMicroUsbPhone的不同充电方式。

Class IPhone implements only LightningPhone interface (it can only be charged via Lightning) and class Android implements only MicroUsbPhone interface (it can only be charged via Micro USB).

IPhone类仅实现LightningPhone接口(只能通过Lightning进行充电) Android类仅实现MicroUsbPhone接口(只能通过Micro USB充电)。

Now, the class LightningToMicroUsbAdapter helps to implement MicroUsbPhone interface and helps charge class Android with that, which otherwise was not possible.

现在,类LightningToMicroUsbAdapter有助于实现MicroUsbPhone接口,并帮助向Android类收费,否则这是不可能的。

Image for post
Wikipedia 维基百科

Reference Links:

参考链接:

  1. Factory pattern

    工厂模式

  2. Builder pattern

    建造者模式

  3. Singleton pattern

    单例模式

  4. Mixin Pattern: Forum Discussion, Blog

    Mixin模式论坛讨论博客

  5. Adapter Pattern

    适配器图案

  6. Other design patterns and their categories:

    其他设计模式及其类别:

    -

    --

    GangOfFour (Classical book on Design Patterns)

    GangOfFour (有关设计模式的经典书籍)

    -

    --

    Blog

    博客

Thanks for reading till the last bit!

感谢您阅读到最后!

I am Ravi Vats, a Software Engineer at Grab, and Computer Science and Engineering Graduate from Ramaiah Institute of Technology, Bangalore.

我是Grab的软件工程师Ravi Vats,是班加罗尔Ramaiah理工学院的计算机科学与工程专业的毕业生。

My areas of interest are domains like Deep Learning, ML, Algorithms & Data Structures, Scalable & Concurrent Systems, Data Analysis & Visualization.

我感兴趣的领域是深度学习,机器学习,算法和数据结构,可伸缩和并发系统,数据分析和可视化等领域。

You can connect with me on my LinkedIn profile & follow my GitHub page.

您可以在我的LinkedIn个人资料上与我联系并关注我的GitHub页面。

Alternatively, I am also available on Twitter, Facebook, Instagram, and Quora.

另外,我也可以在TwitterFacebookInstagramQuora上使用

All my blogs will also be available at my blog page.

我所有的博客也都可以在我的博客页面上找到。

I hope you find this series interesting and resourceful. I am always open to any edits or suggestions to enhance the information provided in this series.

希望您发现本系列有趣而又有意义。 我总是乐于接受任何编辑或建议来增强本系列提供的信息。

Cheers to learning! :)

为学习加油! :)

翻译自: https://medium.com/swlh/headstart-system-design-design-pattern-intuitions-de5e9a5bb65c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值