Design Patterns — 中英对照
Chapter 1: Introduction
Design patterns are typical solutions to commonly occurring problems in software design. They are like pre-made blueprints that you can customize to solve a recurring design problem in your code.
You can't just find a pattern and copy it into your program, the way you can with off-the-shelf functions or libraries. The pattern is not a specific piece of code, but a general concept for solving a particular problem. You can follow the pattern details and implement a solution that suits the realities of your own program.
Patterns are often confused with algorithms, because both concepts describe typical solutions to some known problems. While an algorithm always defines a clear set of actions that can achieve some goal, a pattern is a more high-level description of a solution.
What Does the Pattern Look Like?
Most patterns are described very formally so people can reproduce them in many contexts. Here are the sections that are usually present in a pattern description:
- Intent of the pattern briefly describes both the problem and the solution.
- Motivation further explains the problem and the solution the pattern makes possible.
- Structure of classes shows each part of the pattern and how they are related.
- Code example in one of the popular programming languages makes it easier to grasp the idea behind the pattern.
Why Should I Learn Patterns?
The truth is that you might manage to work as a programmer for many years without knowing about a single pattern. A lot of people do just that. Even so, you might be implementing some patterns without realizing it. So why spend time learning them?
Design patterns are a toolkit of tried and tested solutions to common problems in software design. Even if you never encounter these problems, knowing patterns is still useful because it teaches you how to solve all sorts of problems using principles of object-oriented design.
第一章:引言
设计模式是软件设计中常见问题的典型解决方案。它们就像预先制作好的蓝图,你可以对其进行定制,以解决代码中反复出现的设计问题。
你不能像使用现成的函数或库那样,找到一个模式就直接复制到程序中。模式不是一段特定的代码,而是解决特定问题的一般性概念。你可以按照模式的详细说明,实现一个适合你自己程序实际情况的解决方案。
模式经常与算法混淆,因为这两个概念都描述了某些已知问题的典型解决方案。虽然算法总是定义一组可以实现某个目标的明确操作步骤,但模式是对解决方案的更高级别描述。
模式是什么样的?
大多数模式都以非常正式的方式描述,以便人们能在多种场景中复现它们。以下是模式描述中通常包含的部分:
- 意图简要描述了问题和解决方案。
- 动机进一步解释了问题以及模式所提供的解决方案。
- 类结构展示了模式的每个部分以及它们之间的关系。
- 代码示例以一种流行的编程语言呈现,让人更容易理解模式背后的思想。
为什么要学习设计模式?
事实上,你可能在不了解任何模式的情况下作为程序员工作多年。很多人就是这样做的。即便如此,你可能已经在不知不觉中实现了一些模式。那么为什么要花时间学习它们呢?
设计模式是一套经过验证的、用于解决软件设计中常见问题的工具包。即使你永远不会遇到这些问题,了解模式仍然很有用,因为它教会你如何使用面向对象设计的原则来解决各种问题。