What is a DesignPattern? 什么是设计模式

What is a DesignPattern? 什么是设计模式

Christopher Alexander says, "Eachpattern describes a problem which occurs over and over again in our environment, and thendescribes the core of the solution to that problem, in such a way that you canuse this solution a million times over, without ever doing it the same waytwice" [AIS+77]. Even though Alexander was talking about patterns in buildings andtowns, what he says is true about object-oriented design patterns. Oursolutions are expressed in terms of objects and interfaces instead of walls and doors,but at the core of both kinds of patterns is a solution to a problem in a context.

Christopher Alexander说:“每一个模式描述了在我们的环境中反反复复出现的问题,之后成为描述问题解决方案的中心,以一种方式你可以使用这个解决方案百万次,甚至没有用第二种方法使用它。”尽管Alexander在城市和乡村中谈论设计模式,正确的面向对象设计模式是他说的吗?我们的解决方案的表述是对象和界面而言,而不是墙和门。但是设计模式是在某种环境中解决问题的核心。

In general, a pattern has four essentialelements:

总的说来,设计模式有4个必要元素

1. The pattern name is a handle we can useto describe a design problem, its solutions, and consequences in a word ortwo. Naming a pattern immediately increases our design vocabulary. It lets usdesign at a higher level of abstraction. Having a vocabulary forpatterns lets us talk about them with our colleagues in our documentation, andeven to ourselves. It makes it easier to think about designs and tocommunicate them and their trade-offs to others. Finding good names has been oneof the hardest parts of developingour catalog.

1 .模式的名字是一种感觉,是我们可以用来描述的设计问题。它的解决方案和重要性不是一个或两个字能说清的。命名设计模式会迅速丰富我们的设计词汇量。它会让我们的设计抽象在一个高水平。关于模式的词汇可以让我们和同事们一起讨论它。它让我们很容易的思考设计,去和他人交流和商讨。在开发我们的样品的过程中,寻找一个更好的名字是最困难的部分的其中之一

2. The problem describes when to apply thepattern. It explains the problem and its context. It might describe specificdesign problems such as how to represent algorithms as objects. Itmight describe class or object structures that are symptomatic of aninflexible design. Sometimes the problem will include a list of conditionsthat must be met before it makessense to apply the pattern.

当我们应用设计模式的时候存在着问题描述。它阐明了问题和它所处的环境。它可能描述了一些特殊的设计问题,例如在对象中如何去展现算法,它可能描述类或对象的结构,表明其灵活的设计,在使这个模式有意义之前,我们会遇到关于这个问题的一系列条件。

3. The solution describes the elements thatmake up the design, their relationships, responsibilities, andcollaborations. The solution doesn't describe a particular concrete design orimplementation, because a pattern is like a template that can be applied inmany different situations. Instead, the pattern provides an abstractdescription of a design problem and how a general arrangement of elements (classesand objects in our case) solves it

.解决方案描述了组成设计的元素,它们的关系、责任和合作。这种模式不能描述特别具体的设计或者成就,因为模式像可以被应用在任何情景中的模板。事实上,模式提供了设计问题的抽象描述和如何大致上排列元素解决它。

4. The consequences are the results andtrade-offs of applying the pattern.Though consequences are often unvoiced whenwe describe design decisions, they are critical for evaluating designalternatives and for understanding the costs and benefits of applying thepattern. The consequences for software often concern space and timetrade-offs. They may address language and implementation issues as well. Sincereuse is often a factor inobject-oriented design, the consequences ofa pattern include its impact on a system's flexibility, extensibility,or portability. Listing these consequences explicitly helps youunderstand and evaluate them.

产生和应用模板的重要性。在我们描述设计决定的时候,尽管这些重要性经常被忽视,

为了有取舍的评价设计和了解应用模式的花费和从中所获得的益处,这是决定性的。

软件经常关心时间和空间,这是很重要的。它们也可以是地址或安装启用。自从重用成为面向对象设计的因素,模式已经是影响系统灵活性、延伸性、可移植性的重要一项。列出这些重要性可以帮助你理解和评价。

Point of view affects one's interpretationof what is and isn't a pattern. One person's pattern can be another person'sprimitive building block. For this book we have concentrated on patterns at acertain level of abstraction. Design patterns are not about designs such as linked listsand hash tables that can be encoded in classes and reused as is. Nor are theycomplex, domain-specific designs for an entire application or subsystem. Thedesign patterns in this book are descriptions of communicating objects andclasses that are customized to solve a general design problem in a particular context.

观点影响了人们解释模式的样子,一个人的模式可以被其他人的模式作为原始模块。在这本书里,我们关注模式在一个确定的抽象水平上。设计模式不是设计例如链表或哈希表,它们可以被编码在类中重用它们。他们也不是复杂的或对整个应用程序或子系统的特定领域的设计。在这本书的设计模式中,描述了类和对象的交互,在一个特定的环境中解决大部分设计问题。

A design pattern names, abstracts, andidentifies the key aspects of a commondesign structure that make it useful forcreating a reusable object-oriented design.The design pattern identifies theparticipating classes and instances, their roles and collaborations, and the distribution ofresponsibilities. Each design pattern focuses on a particular object-orienteddesign problem or issue. It describes when it applies, whether it can beapplied in view of other design constraints, and the consequences andtrade-offs of its use. Since we must eventually implement  our designs, a design pattern also providessample C++ and (sometimes) Smalltalk code to illustrate an implementation.

在一个普通的设计结构中模式的名称,抽象和特征是关键的一方面,为了创建一个有用的面向对象设计,这非常有用。设计模式确定了参与的类和例子,它们的角色,协作和所承担的职责。每种设计模式聚焦了特别的面向对象设计问题或事件。它描述了当它被应用时,是否受到其他的设计缺陷的约束,应用的重要性,以及在使用是否需要协调。最终实现我们的设计,设计模式也提供了c++样品和Smalltalk 代码来说明这种实现。

Although design patterns describeobject-oriented designs, they are based on practical solutions that have beenimplemented in mainstream object-oriented programming languages like Smalltalk andC++ rather than procedural languages(Pascal, C, Ada) or more dynamicobject-oriented languages (CLOS, Dylan, Self). We chose Smalltalk and C++ for pragmaticreasons: Our day-to-day experience has been in these languages, and they areincreasingly popular.

尽管设计模式描述了面向对象设计,在主流的面向对象程序设计语言如c++,而不是过程语言或者动态的面向对象语言。我们选择smalltalk和c++原因:在这些语言中我们平日的经验在增长,它们也越来越受欢迎。

The choice of programming language isimportant because it influences one's point of view. Our patterns assumeSmalltalk/C++-level language features, and that choice determines what can and cannot beimplemented easily. If we assumed procedural languages, we might haveincluded design patterns called "Inheritance,""Encapsulation," and"Polymorphism." Similarly, some of our patterns are supported directly by the less common object-orientedlanguages. CLOS has multi-methods,for example, which lessen the need for apattern such as Visitor (page 366). In fact, there are enough differences betweenSmalltalk and C++ to mean that some patterns can be expressed more easily inone language than the other. (See Iterator(289) for an example.)

选择程序语言是非常重要的,因为它影响我们的观点。模式呈现了c++或smalltalk的特征,这些选择决定了我们可不可以很容易的实现设计。如果我们使用面向过程语言,我们可能包含了可能的设计模式如封装、继承和多态。类似的,一些模式是普遍支持很少的面向对象语言,CLOS有多方法,例如,减少访客需要的模式。事实上,在一种或者那里有足够的不同在c++和Smalltalk中,也意味着一种语言比其他语言更容易描述一些模式。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值