-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
当设计模式遇上 Kotlin #1780
当设计模式遇上 Kotlin #1780
Conversation
校对认领 @sqrthree |
@windmxf 好哒 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sqrthree 校对完成。译者翻译的很赞~~
|
||
It's also important to note that these patterns where discovered to overcome the shortcomings of how imperative programming languages were designed in the 90s (C++ in particular). Many modern languages provide features to overcome these issues without writing extra code or falling back to patterns. | ||
认识到这些模式的发明是为了弥补起源于上世纪九十年代的一些命令式编程语言(尤其是 C++)的不足是很重要的。很多现代编程语言提供了解决这些不足的特性,我们完全不需要再写多余的代码或者做刻意模仿设计模式这种事了。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[It's also important to note that these patterns]=>[需要特别说明一下,这些设计模式的发明]
|
||
Inspired by Mario Fusco's [Talk](https://www.youtube.com/watch?v=Rmer37g9AZM)/[Blog posts](https://www.voxxed.com/blog/2016/04/gang-four-patterns-functional-light-part-1/)/[Repository](https://github.com/mariofusco/from-gof-to-lambda) "From GoF to lambda", I decided to implement some of the most famous design patterns in computer science in Kotlin! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[I decided to implement some of the most famous design patterns in computer science in Kotlin!]=>[我决定用 Kotlin来实现一些计算机科学领域最著名的设计模式!]
可斟酌..
|
||
That's why —just like Mario in the `g ∘ f` repository— I will try to find a simpler, easier or more idiomatic way to solve the same problem each pattern is solving. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[a simpler, easier or more idiomatic way]=>[一种更简单方便、更惯用的方式]
|
||
If you don't like reading explanations, you can directly jump to my [github repository](https://github.com/lmller/gof-in-kotlin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[reading explanations]=>[看说明文字]
|
||
First up are the structural patterns. This is tough, because structural patterns are about - structure! How could I implement a structure with a *different* structure? I can't. An exception is the **Decorator**. While it's technically just about structure, its usage is more about behavior or responsibilities. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[I can't]=>[做不到。]
|
||
Although the **Singleton** is considered an anti-pattern these days, it has its usages (I won't discuss this topic here, just use it with caution). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[just use it with caution]=>[但需要谨慎地使用它。]
@@ -197,13 +197,13 @@ execute { | |||
} | |||
``` | |||
|
|||
As you can see, there is no need to have a class at all! One could argue, that this now resembles the **Strategy** pattern, which is probably correct. But then again, **Strategy** and **Template Method** are solving very similar problems (if not the same). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[As you can see, there is no need to have a class at all!]=>[可以看到,这里不需要写任何一个类!]
|
||
Writing an **Iterator** is a rare task. Most of the time, it's easier and more convenient to wrap a `List` and implement the `Iterable` interface. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Writing an Iterator is a rare task]=>[很少需要自己写一个迭代器。]
|
||
In Kotlin, `iterator()` is an operator function. This means that when a class defines a function `operator fun iterator()`, it can be iterated using a `for` loop (no interface needed). That's particularly cool because it works also with extension functions. That's right - by using an extension function, I can make *every* object iterable. Consider this example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[That's particularly cool]=>[这种方式非常酷。]
|
||
I hope this post gave you an idea on how Kotlin can result in different approaches to well known problems. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[-I hope this post gave you an idea on how Kotlin can result in different approaches to well known problems.]=>[希望这篇文章能给你些启发,让你了解用Kotlin可以带来新的思路来解决那些广为人知的问题。]
校对认领 @sqrthree |
|
||
The goal is not to simply *implement* the patterns, though. Since Kotlin supports object oriented programming and is interoperable with Java, I could just copy-auto-convert every java class in Mario's repository (doesn't matter whether it's the "traditional" or the "lambda" examples) and **it would still work!** | ||
当然,我的目标不是简单的 **实现** 这些模式。因为 Kotlin 支持面向对象编程并且和 Java 是可互操作的,我可以从 Mario 的仓库直接复制粘贴每一个 Java 文件(先不管是“传统”的还是“lambada 风格”的),**它们将仍然可以正常工作**! | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: [lambada] => [lambda]
|
||
That's why —just like Mario in the `g ∘ f` repository— I will try to find a simpler, easier or more idiomatic way to solve the same problem each pattern is solving. | ||
这就是为什么我像 Mario 那样,去寻找一种更简单方便、更惯用的方式来解决这些模式所要解决的问题。 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g of f 地址(https://github.com/mariofusco/from-gof-to-lambda) 可以考虑加上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对完成。翻译的不错
@@ -1,44 +1,44 @@ | |||
> * 原文地址:[Gang of Four Patterns in Kotlin](https://dev.to/lovis/gang-of-four-patterns-in-kotlin) | |||
> * 原文作者:[Lovis](https://dev.to/lovis) | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 译者: | |||
> * 译者:[Boiler Yao](https://github.com/boileryao) | |||
> * 校对者: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对者:wilsonandusa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
添加了校对者
校对完成 @sqrthree |
@boileryao @windmxf 的 review 还有许多没有折叠,是不需要调整吗?还是需要讨论下?麻烦 @windmxf 确认下哈。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有一个小问题需要调整下哈
|
||
--- | ||
|
||
As you might know, there are three types of (gof) patterns: **structural**, **creational** and **behavioral** patterns. | ||
众所周知,根据“四人帮”的定义设计模式可以分为三种: **结构型**、**创建型 **和 **行为型**。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gang of Four Patterns in Kotlin, 设计模式遇上 Kotlin
翻译完成