设计模式之Frameworks 框架

来源:互联网 发布:淘宝店铺货源哪里找 编辑:程序博客网 时间:2024/05/05 06:42


A framework is a set of cooperating classesthat make up a reusable design for a specific class of software [Deu89, JF88].For example, a framework can be geared

toward building graphical editors fordifferent domains like artistic drawing, music composition, and mechanical CAD[VL90, Joh92]. Another framework can help

you build compilers for different programming languages and target machines [JML92]. Yet another might help you build financial modeling applications [BE93].

You customize a framework to a particular application by creating application-specific subclasses of abstract classes from the framework.

框架是软件的特殊类,它们构成了实现重用设计的一組类;举个简单的例子,

对于不同的域像绘画艺术、音乐创作、机械CAD,框架可以针对这些建立图像编辑。其他的框架可以为不同的程序语言和目标机器建立编译。另一个可以帮助你建立最后的应用模型。通过创建框架抽象类的特殊的子类,你可以为你的应用定制一个框架。

The framework dictates the architecture of your application. It will define the overall structure, its partitioning into classes and objects, the key responsibilities thereof, how the classes and objects collaborate, and the thread of control. A framework predefines these design parameters so that you, the application designer/implementer, canconcentrate on the specifics of your application. The framework captures thedesign decisions that are common to its application domain. Frameworks thusemphasize design reuse over code reuse, though a framework will usually include concrete subclasses you can put to work immediately.

框架指定了你的应用的体系结构。它会定义主体结构,它划分的类和对象,主要职责,类和对象之间是如何协作,和它是如何控制的。框架预定义了应用设计和实现这些设计参数,因此你可以专心致志于你的应用中特殊的地方。框架捕捉应用中普遍的设计决策。框架强调设计重用超过代码的重用,虽然框架包含了你可以立即投入使用的子类!

Reuse on this level leads to an inversion of control between the application and the software on which it's based. When you use a toolkit (or a conventional subroutine library for that matter), youwrite the main body of the application and call the code you want to reuse. When you use a framework, you reuse the main body and write the code it calls. You'll have to write operations with particular names and calling conventions, but that reduces the design decisions you have

to make.

在这个水平的重用会导致应用程序之间和它所基于的软件控制的反转。当你使用工具包(或者传统的子程序库),你写了应用的主体和你想去重用的代码。在你使用框架的时候,你可以重用程序的主体和它调用的代码。你会用特殊的名字和调用协议来写一个运算,但是这些减少了你所使用的设计决策。

Not only can you build applications fasteras a result, but the applications have similar structures. They are easier to maintain,and they seem more consistent to their users. On the other hand, you losesome creative freedom, since many design decisions have been made for you.

结果是你不仅可以很快的创建应用并且应用有相似的结构。它们很容易去维护,用户看上去很一致。另一方面,从你开始使用你创建的设计决策,可能会失去一些设计的灵活性。

If applications are hard to design, andtoolkits are harder, then frameworks are hardest of all. A framework designergambles that one architecture will work for

all applications in the domain. Anysubstantive change to the framework's design would reduce its benefits considerably,since the framework's main contribution

to an application is the architecture it defines. Therefore it's imperative to design the framework to be as flexible andextensible as possible.

如果应用很难设计,用工具包很费劲,这框架也很难。框架为在应用的作用域中起作用冒很大的风险。从框架开始作用在这个应用开始,任何实质性的改变都会对框架的益处带来相当大的改变。框架必要的设计一定要灵活和可扩展。

Furthermore, because applications are sodependent on the framework for their design, they are particularly sensitive tochanges in framework interfaces. As

a framework evolves, applications have toevolve with it. That makes loose coupling all the more important; otherwise even aminor change to the framework will have

major repercussions.

更长远的说,因为应用依赖为它所设计的框架,框架的接口是特别敏感的。在框架逐步演化的过程中,应用也随着它而演化。连接是非常重要的,否则框架一个很小的改变也会产生很大的影响。

The design issues just discussed are mostcritical to framework design. A framework that addresses them using design patternsis far more likely to achieve high levels

of design and code reuse than one thatdoesn't. Mature frameworks usually incorporate several design patterns. The patterns help make the framework's architecture suitable to many differentapplications without redesign.

在设计议题中,极重要的是框架的设计。地址使用设计模式的框架很容易达到很高的水平,它是设计和代码的重用所达不到的!成熟的设计模式通常包含了几个设计模式,在不重用设计的情况下,模式使框架的体系结构适合更多不同的应用。

An added benefit comes when the framework is documented with the design patterns it uses [BJ94]. People who know the patterns gain insight into the framework faster.

Even people who don't know the patterns canbenefit from the structure they lend to the framework's documentation. Enhancingdocumentation is important for all

types of software, but it's particularly important for frameworks. Frameworks often pose a steep learning curve that must be overcome before they're useful.While design patterns might not flatten the learning curve entirely, they can make it less steep by making key elements of the framework's design more explicit.

一个额外的好处是框架是设计模式所用的文件。人们都知道模式洞察框架更快!即使人们不知道模式可以从这个结构中获益,适合于框架文件。对所有的软件来说提高它的使用是非常重要的,对框架来说它显得特别的重要!框架的学习是一个陡峭的学习曲线,在使用它之前必须克服。然而设计模式也不是一个完全平坦的曲线,可以明确在定制框架时更关键的因素,就可以成功了!

Because patterns and frameworks have some similarities, people often wonder how or even if they differ. They are different in three major ways:

因为模式和框架有很多相似的地方,我们经常为它们的不同之处感到惊讶!它们不同在这三个主要的方面。

1. Design patterns are more abstract than frameworks. Frameworks can be embodied in code, but only examples of patterns can be embodied in code.

A strength of frameworks is that they can be written down in programming languages and not only studied but executed and reused directly. In contrast,

the design patterns in this book have to beimplemented each time they're used. Design patterns also explain theintent, trade-offs, and consequences

of a design.

1)设计模式比框架更抽象。框架可以用代码来体现。只是模式用代码实现的一个例子。一个强大的框架可以用程序语言写下来,不仅是学习和研究和有目的的重用!相比之下,在这本书中的设计模式在使用它们的时候都被实现了。设计模式也解释了意图.、权衡和设计的影响。

2. Design patterns are smaller architectural elements than frameworks. Atypical framework contains several design patterns, but the reverse is never

true.

设计模式是比框架还小的体系结构元素。框架包含了几种设计模式,但是反过来确实不正确的。

3. Design patterns are less specialized than frameworks. Frameworks always have a particular application domain. A graphical editor framework might be used in a factory simulation, but it won't be mistaken for a simulation framework. In contrast, the design patternsin this catalog can be used in nearly any kind of application. While more specialized design patterns than ours are certainly possible (say,design patterns for distributed systems or concurrent programming), even these wouldn't dictate an

application architecture like a framework would.

设计模式没有框架那么专业。框架总是有特别的应用域。图像编辑程序框架可能被工厂模拟。但是这种模拟不会因为是框架而犯错。相比之下,设计模式可以被应用在各种应用的设计样本中。而更专业的设计模式没有了(设计模式的分布系统或并行编程),就像一个框架这不决定应用结构。

Frameworks are becoming increasingly commonand important. They are the way that object-oriented systems achieve the mostreuse. Larger object-oriented

applications will end up consisting oflayers of frameworks that cooperate with each other. Most of the design and code inthe application will come from or be

influenced by the frameworks it uses.

框架变得越来越普遍,越重要了。它是面向对象系统获得更多重用的一种方法。巨大的面向对象应用以包含很多层的框架的相互合作完成,在应用中的设计和代码会来自于所用的框架,并且受到他所应用的框架的影响。