迁移学习 简而言之_简而言之,桥接模式

迁移学习 简而言之

The bridge pattern is another ideological abstraction that keeps the boundaries of your code clean and separated. While it has similarities with the adapter pattern, it is not quite the exact same. The bridge pattern can be seen as an extension of the adapter pattern — or commonly known as the double adapter pattern.

桥接模式是另一种意识形态上的抽象,它使您的代码边界保持清晰和分离。 尽管它与适配器模式有相似之处,但并不完全相同。 桥接模式可以看作是适配器模式的扩展,或者通常称为双适配器模式

Here’s what the adapter pattern looks like in normal human English:

这是普通人类英语中的适配器模式:

Image for post
image by Aphinya Dechalert
图片由Aphinya Dechalert提供

The bridge pattern takes it one step further and creates an interface for both sides of the code. This means that you end up with two interfaces — one for the original code and another for the new code.

桥图案采取它一个步骤进一步并创建针对代码的两侧的界面。 这意味着您最终会有两个接口-一个用于原始代码,另一个用于新代码。

Image for post
image by Aphinya Dechalert
图片由Aphinya Dechalert提供

Even in diagram form, having two interfaces seems a bit unnecessary. However, it can come in handy if you have multiple ‘branches’. The second interface helps keep your code isolated and decreases coupling and interdependence between code.

即使采用图表形式,具有两个接口似乎也没有必要。 但是,如果您有多个“分支”,它可能会派上用场。 第二个接口有助于使您的代码隔离,并减少代码之间的耦合和相互依赖性。

With the bridge pattern, the structure of your code looks something like this:

使用桥接模式,您的代码结构如下所示:

Image for post
image by Aphinya Dechalert
图片由Aphinya Dechalert提供

This is much more efficient and less prone to breakages than just have a single interface layer. For example, if the requirements in code set 1 changes, only the associated interface needs to change. Any changes made down that particular branch won’t impact on code set 2.

与仅具有单个接口层相比,此方法效率更高且不易损坏。 例如,如果代码集1中的要求发生更改,则仅需要更改关联的接口。 对该特定分支进行的任何更改都不会影响代码集2。

While in other programming languages, there are specific requirements and techniques to implement this pattern, JavaScript’s simple nature pushes builder structures into an ideological and developer diligence space. This is achieved through how you name your functions and classes.

尽管在其他编程语言中,实现此模式有特定的要求和技术,但JavaScript的简单本质将构建器结构推向意识形态和开发人员勤奋的空间。 这是通过命名函数和类的方式来实现的。

名字叫什么? (What’s in a name?)

What’s in a name? that which we call a rose By any other name would smell as sweet.

名字叫 什么? 我们称其为玫瑰的玫瑰,闻起来像甜。

spoken by Juliet, Act 2 Scene 2 — Romeo and Juliet, by William Shakespeare

朱丽叶(Act 2场景2)的讲话-罗密欧与朱丽叶(William Shakespeare)

In programming, a name that’s not descriptive or obvious is not as sweet. Rather, it can lead to faster code decay and potential code smell. What’s code smell? It’s a characteristic of the code that can lead to deeper problems when things change.

在编程中,没有描述性或不明显的名称并不那么甜美。 相反,它可以导致更快的代码衰减和潜在的代码气味。 什么是代码气味? 这是代码的特征,当事情发生变化时,可能导致更深层次的问题。

Code is a language. Everyone knows that but a lot of us don’t really understand what that truly means.

代码是一种语言。 每个人都知道这一点,但我们很多人并不真正理解这到底意味着什么。

A language is a way we communicate with each other. It’s a way to transmit meaningful information through a medium. Language by itself is not as useful. It needs an audience — and when it comes to code, developers are the primary target.

语言是我们彼此交流的一种方式。 这是一种通过媒体传输有意义的信息的方法。 语言本身并不是那么有用。 它需要听众-在编写代码时,开发人员是主要目标。

Under traditional thinking, a programming language is the interface between the machine and the programmer. The programmer types the code and the machine compiles it to specifications.

在传统思想下,编程语言是机器和程序员之间的接口。 程序员输入代码,然后机器将其编译为规范。

But a programming language’s functionality is much more than this. It is also an interface between developers — or else we would all be coding in binary.

但是编程语言的功能远不止于此。 它也是开发人员之间的接口-否则我们所有人都将使用二进制编码。

Clean code is designing a program that supports visual thinking. The level of ‘noise’ in code is measured through the ratio of useful information and irrelevant data. A piece of code may be technically correct but visually, it can appear noisy when your function or class names doesn’t immediately communicate its purpose.

简洁的代码正在设计一个支持视觉思维的程序。 代码中“噪声”的级别是通过有用信息和无关数据的比率来衡量的。 一段代码在技术上可能是正确的,但在视觉上,当您的函数或类名称未立即传达其用途时,它可能显得很嘈杂。

How is this related to the bridge pattern?

这与桥接模式有何关系?

JavaScript is a language that doesn’t have many enforced rules or structural requirements. Implementation of patterns and practices falls on the developer. How you name things isn’t just limited to the builder pattern. It can also be extended to other parts of your code.

JavaScript是一种没有很多强制性规则或结构要求的语言。 模式和实践的实施取决于开发人员。 您如何命名事物不仅限于构建器模式。 它也可以扩展到代码的其他部分。

This means that if you’re using the adapter pattern, call it what it is — a CartAdapter. Yes, it's an interface but using the pattern name gives the developer looking at the code more information about the purpose of the function or class.

这意味着,如果您使用的是适配器模式,请称其为“ CartAdapter 。 是的,它是一个接口,但是使用模式名称可使开发人员在代码中查看有关功能或类用途的更多信息。

For the bridge pattern, you can use a two-tier approach. Here’s an example of what it could potentially be:

对于桥接模式,可以使用两层方法。 这是一个可能的示例:

Image for post
image by Aphinya Dechalert
图片由Aphinya Dechalert提供

You don’t have to follow this exact naming pattern. It’s only one of many potential ideas. What’s more important is that you have a naming pattern that fits with what you want to communicate.

您不必遵循这种确切的命名模式。 这只是许多潜在想法中的一种。 更重要的是,您要拥有一种适合您要交流的命名模式。

That’s basically it for a bridge pattern in JavaScript. For adapter pattern code sample, check it out here:

基本上,这就是JavaScript中的桥接模式。 有关适配器模式代码示例,请在此处查看:

翻译自: https://medium.com/madhash/bridge-pattern-in-a-nutshell-162a62e7b889

迁移学习 简而言之

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值