首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计方式之Relating Run-Time and Compile-TimeStructures

2013-10-08 
设计模式之Relating Run-Time and Compile-TimeStructuresRelating Run-Time and Compile-TimeStructures

设计模式之Relating Run-Time and Compile-TimeStructures

Relating Run-Time and Compile-TimeStructures 有关运行时和编译时的结构

An object-oriented program's run-time structure often bears little resemblance to its code structure. The code structure is frozen at compile-time; it consists of classes in fixed inheritance relationships.A program's run-time structure consists of rapidly changing networks of communicating objects. In fact, the two structures are largely independent. Trying to understand one from the other is like trying to understand the dynamism of living ecosystems from the static taxonomy of plants and animals, and viceversa.

面向对象程序结构在运行时结构往往一点不像其代码结构。代码结构在编译时动态的。包含了类固定不变的继承关系。程序在运行是的结构包含快速变化的网络通信对象。实际上,这两个的结构有很大的独立性。试着去其中一个来理解另一个就像理解从静态的植物和动物的分类来理解一个活的生态系统,反之亦然。

Consider the distinction between object aggregation and acquaintance and how differently they manifest themselves at compile- and run-times. Aggregation implies that one object owns or is responsible for another object. Generally we speak of an object having or being part of another object. Aggregation implies that an aggregate object and its owner have identical lifetimes.

考虑对象的聚集和友元的不同,和它们在编译时和运行时的不同表现。聚集简单的说是对单个对象或对其负责的对象聚集。一般我们说一个对象或有另一个对象的一部分。聚焦意味着集合对象和它的主人有相同的寿命。

Acquaintance implies that an object merely knows of another object. Sometimes acquaintance is called"association" or the "using" relationship. Acquainted objects may request operations of each other, but they aren't responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much looser coupling between objects.

友元意味着一个对象知道另一个对象。有时候友元也被称作“合作”或“使用”关系。友元的对象相互之间可以发出运算的命令,但是他们相互之间是不负责的。友元是对象之间聚集和建议的更宽松的耦合关系。

In our diagrams, a plain arrowhead linedenotes acquaintance. An arrowhead linewith a diamond at its base denotesaggregation: It's easy to confuse aggregation andacquaintance, because they are often implemented in the same way. In Smalltalk,all variables are references to other objects. There's no distinction in theprogramming language between aggregation and acquaintance. In C++, aggregation canbe implemented by defining membervariables that are real instances, but it's more common to define them as pointers or references to instances. Acquaintance is implemented with pointers and references as well.

在我们下面的图表中,一个普通的箭头表示相互之间是友元。有菱形的箭头表示聚集。

设计方式之Relating Run-Time and Compile-TimeStructures

聚集和友元很容易是我们迷惑,因为它们经常用同样的方法实现。在Smalltalk中,所有的变量都是引用其他的对象。在程序语言中聚集和友元之间没有区别。在c++

中,聚集可以用定义的实例化的成员变量来实现,但是更普遍的定义是实例化的指针和引用。

友元也可以用指针和引用实现。

Ultimately, acquaintance and aggregationare determined more by intent than by explicit language mechanisms. Thedistinction may be hard to see in the compile-time structure, but it'ssignificant. Aggregation relationships tend to be fewer and more permanent thanacquaintance. Acquaintances, in contrast, are made and remade more frequently, sometimesexisting only for the duration of an

operation. Acquaintances are more dynamic as well, making them more difficult to discern in the source code.

最终,聚集和友元取决于比它们更明确的语言机制。这种区别在编译时可能更难看到,但是它是有意义的。聚集关系要比友元更长久。友元,相反,塑造和重新塑造的更频繁,有时只存在一个持续时间的操作。友元也体现更多的动态,使他们在源代码中更难区别。

With such disparity between a program's run-time and compile-time structures, it's clear that code won't revealeverything about how a system will work. The system's run-time structure must be imposedmore by the designer than the language.

The relationships between objects and their types must be designed with great care, because they determine how good or bad the run-time structure is.

程序的运行结构和编译结构有如此多的不同,我们很清晰的明白,代码不能揭示程序是如何起效的,系统的执行结构施加了更多的设计而不是语言。对象和类型之间的关系必须谨慎,因为它们决定了执行结构的好坏。

Many design patterns (in particular thosethat have object scope) capture the distinction between compile-time andrun-time structures explicitly. Composite(183) and Decorator (196) are especiallyuseful for building complex run-time structures. Observer (326) involvesrun-time structures that are often hard to understand unless you know the pattern.Chain of Responsibility (251) also results

in communication patterns that inheritance doesn't reveal. In general, the run-time structures aren't clear from the code until you understand the patterns.

很多的设计模式明确的捕捉了编译结构和执行结构的区别。综合模式和装饰模式明确了构建复杂的执行结构的作用。观测者模式涉及到的执行结构式很难理解的,除非你了解这个模式。职责链模式在通信时它的结果在继承时不能被揭示。一般说来,在你理解这些模式之前,从代码中理解执行结构是不清晰的。

 

 

热点排行