设计模式(Technology Computer English)

来源:互联网 发布:linux snmp协议 编辑:程序博客网 时间:2024/04/29 17:45

Initially, you can think of a pattern as an especially
clever and insightful way of solving a particular class of
problems. That is, it looks like a lot of people have worked
out all the angles of a problem and have come up with the
most general, flexible solution for it. The problem could be
one you have seen and solved before, but your solution probabl y
didn’t have the kind of completeness you’ll see embodied in
a pattern.
Although they’re called “design patterns,” they really aren’t
tied to the realm of design. A pattern seems to stand apart
from the traditional way of thinking about analysis, design,
and implementation. Instead, a pattern embodies a complete
idea within a program, and thus it can sometimes appear at
t h e a n a l y s i s p h a s e o r h i g h - l e v e l d e s i g n p h a s e . T h i s i s
interesting because a pattern has a direct implementation in
code and so you might not expect it to show up before low-level
design or implementation (and in fact you might not realize
that you need a particular pattern until you get to those
phases).
The basic concept of a pattern can also be seen as the
basic concept of program design: adding layers of abstraction.
Whenever you abstract something you’re isolating particular
det ails, and one of the most compelling motivations behind
this is to separate things that change from things that stay
the same. Another way to put this is that once you find some
part of your program that’s l ikely to change for one reason
or another, you’ll want to keep those changes from propagating
other modifications throughout your code. Not only does
this make the code much cheaper to maintain, but it also
turns out that it is usually simpler to understand (which results
最初, 你可以把一个模式当作解决一类特定问题的
特别聪明而见解深刻的方法。也就是说, 看起来就像是
有很多人已经处理了一个问题所有的角落, 并得到了这
个最普遍、最灵活的解决方案。你以前也许曾经见到过
并解决过这个问题, 但是你的解决方案可能不会有你看
到模式表现的解决方案那么完善。
尽管它们被称为“ 设计模式” , 但实际上它们并不
仅仅局限于设计的领域内。看上去, 模式与分析、设计
和实现的思考的传统方法相去甚远。取而代之的, 模式
在一个程序中实现了一套完整的思想, 因此它有时也可
以出现在分析阶段或高层设计阶段。这一点很有趣, 因
为模式都有一个直接的代码实现, 所以你可能认为它不
会在低层设计或实现之前的阶段出现( 而且实际上, 在
你真正到达这些阶段之前, 你可能没有意识到自己需要
某个特定的模式) 。
模式的基本概念也可以被看作程序设计的基本概
念: 添加抽象层。当你抽象出某些东西的时候, 你就将
特定的细节隔离开来。而在这背后, 最重要的动机就是
要将变化的事物与保持不变的事物相分离。换一种说
法: 当你发现程序的某一部分因为这样或那样的原因有
可能发生变化时, 你不希望让变化传播出去而修改你的
代码之外的部分。这样不但可以使代码维护的代价更
低, 也使代码更容易理解( 结果就是更低的开销) 。

 in lowe red costs).
Often, the most difficult part of developing an elegant
and cheap-to-maintain design is in discovering what I call
“the vector of change.” (Here, “vector” refers to the maximum
gradient and not a container class.) This means finding the
most important thing that changes in your system, or put
another way, discovering where your greatest cost is. Once
you discover the vector of change, you have the focal point
around which to structure your design.
So the goal of design patterns is to isolate changes in
your code. If you look at it this way, you’ve been seeing
some design patterns already in this book. For example,
inheritance could be thought of as a design pattern (albeit
one implemented by the compiler). It allows you to express
differences in behavior (that’s the thing that changes) in
objects that all have the same interface (that’s what stays
the same). Composition could also be considered a pattern,
since it allows you to change --dynamically or statically - -
the objects that implement your class, and thus the way that
class works. Normally, however, features that are directly
supported by a programming language are not classified as
design patterns.
You’ve also already seen another pattern that appears
in Design Patterns: the iterator. This is the fundamental
tool used in the design of the STL; it hides the particular
implementation of the container as you’re stepping through
and selecting the elements one by one. The iterator allows
you to write generic code that performs an operation on all
of the elements in a range without regard to the container
that holds the range. Thus your generic code can be used
with any container that can produce iterators.
开发一个优雅而容易维护的设计方案最困难的部
分, 就是发现被我称为“ 变化矢量” 的那种东西。( 在
这里, “ 矢量” 是指“ 最大的斜率” , 而不是一个容器
类。) 也就是说, 发现系统中最重要的变化, 或者换个
说法, 发现你最大的开销是什么。一旦你发现了变化矢
量, 你就拥有了组织设计的焦点。
因此, 设计模式的目标就是隔离你的代码中的变
化。如果你用这种方式来观察, 你会发现本书中已经出
现过一些设计模式了。比如说, 继承可以被考虑为一种
设计模式( 尽管它是由编译器来实现的) 。它让你可以
让拥有相同接口的对象( 这是保持不变的) 表现出不同
的行为( 这是变化的东西) 。组合也可以被考虑为一种
模式, 因为它让你可以— — 动态的或静态的— — 改变实
现你的类的对象, 并由此而改变类的工作方式。但是,
一般来说, 直接由编程语言支持的特性是不被分类为设
计模式的。
你还看到了另一个在《设计模式》中出现过的模
式:I t e r a t o r 模式。这是S T L 的设计中用到的一个基本
的工具; 在你遍历并逐个选择元素的时候, 它隐藏了容
器的特定实现。I t e r a t o r 模式让你可以编写一般性的代
码, 并在某个范围内所有的元素上执行一个操作, 而不
用关心包容这个范围的容器。因此, 你的一般性代码可
以应用在任何可以生成I t e r a t o r 的容器上。

原创粉丝点击