[译]OOSE第3章:面向对象知识入门 3.2 Object

来源:互联网 发布:淘宝进口税要买家交吗 编辑:程序博客网 时间:2024/05/17 01:26

3.2 Object

   The first and most important concept that we describe is,of course the concept of object.  What we mean by an object is an entity able to save a state (information) and which offers a number of operations (behavior) to either examine or affect this state.

   我们首先要描述的最重要的概念就是对象,我们认为对象是一个实体, 他可以保存一个状态信息(information), 并能够提供一系列的操作(behavior) 来检查或者影响这个状态信息.

An object is characterized by a number of operations and a state which remembers the effect of these operations. 个对象是以一组特定的操作(operations)和一个记录这些操作执行结果的状态信息为典型特征

图3.2

                                 图3.2

 

An example of the information which we might wish to save for each object is age, address, male/female and so on. To access or be able to affect the information, we must, for each object, define a set of operation which can affect or read the saved information. We can also define operations which perhaps need not affect any internal information, but only perform a behavior. The only part of the object we can see is its operations; the inside is hidden to us.

 3-2 是一个面向对象的案例,我们可以把John, Mary,Tom当作三个对象。在这个案例中,我们可能希望为每个对象保存一些信息,比如年龄,地址,男生/女生,诸如此类的信息。为了能够有效的管理每个对象的特定信息列表,我们就必须针对每一个对象定义一组操作,利用这一组操作,外部用户可以修改或者读取已经保存的信息。此外,我们还可以定义其他一些操作,这些操作可能根本不会改变对对象内部的信息取值,只是仅仅执行一个特定行为。对于一个对象而言,他提供的操作接口是外部用户唯一可见的部分,而内部的信息列表都是隐藏,不可见的。

图3.3

                    图3.3

 

Within the information, any associations to other object are also specified. The relation can be of two sorts: firstly , static relations, namely relations existing over longer period , which mean that two object know each other’s existence; secondly dynamic relations , namely relations by which two objects actually communicate with each other.

在一个对象的信息列表之内,也需要定义这个对象和其他任何对象的关联关系。对象之间的关系可以有两种,首先是静态关系,静态关系用于针对对象之间的长期存在的绑定关系进行命名对象之间的静态关系意味着两个对象都知晓对方的存在;其次是动态关系,动态关系用于对两个对象之间的实时通讯关系进行命名。

An object can thus be composed from other objects and this is described using this relations.  

一个对象可以由其他一些对象组成,这种关联关系是通过静态关系来加以描述

图3.4

                    图3.4

 

By means of this composition, we can structure the object Tom in parts.   It often depends on a combination of wishing to describe the object in detail , increase understanding and obtain reusable parts.

借助构成这种组合方法,我们可以把对象TOM规划成不同的部分。在对象建模的过程中,应用这种方法的好处是能够把一个对象描述的更加具体,增加对象的可读性,同时最重要的一点是获取可重用的部分。

A similar method of joining different parts together is through use of partition hierarchies. This means that an object can be constructed from other objects; these relations are therefore often called consist of relations. The word 'partition' originates from the Latin partitos and means divide. Figure 3.5 illustrates an example of a partition hierarchy.

图3.5

                      图3.5

有一种类似的方法可以把不同的部分连接在一起,这种方法是使用层次化分割('partition')方法。这就意味着一个对象可以通过其他一些对象来构成;所以这种静态关联关系也常常被叫做包含关系。单词'partition'起源自拉丁语partitos,这个单词的含义就是分割。图3-5描述了这样一种层次化的分割关系。

A similar possibility for showing how something is interrelated is through the use of an aggregate. Partition hierarchy and aggregate are used as synonyms, but there is a difference. The word 'aggregate' comes from the Latin verb aggregare, meaning 'to join together', which is the opposite of 'to partition'. As it is difficult to express this grouping in a single way, a new object and a partition hierarchy can be used to express the aggregate. In a family relationship, the grouping of Man, Woman and Child establishes the aggregate Family.  Since we cannot represent this triangular relationship, an object family is added to express the joining of several objects. This object thus represents the aggregate, but it is not the aggregate itself. An aggregate is a union of several objects, and the union as such is often represented by an object of its own.

在描述对象之间的静态关联关系的方法是使用聚合关系aggregate)。在对象建模的过程中,层次化分割Partition hierarchy关系和聚合关系aggregate是作为同义词来使用,但是这两种关系还是有一些微小的区别。单词'aggregate'是来自拉丁单词aggregare。他的含义是“连接在一起”,这和“分割,划分”'to partition'是完全相反的含义。聚合关系aggregate是层次化分割Partition hierarchy关系中的一种特例,因为这种组合关系难以用单一的方式来描述,所以我们定义了一个新的对象来表达这种聚合关系。在一个家庭关系中,父亲,母亲,儿童的连接关系形成了一个新的对象,我们把这个对象命名为家庭,他代表了三种对象通过连接整体形成的对象实体。

If we look at the inside of the object, we shall see both its information structure and how its operations work (see Figure 3.6). We can see the attributes that the object needs to store, the parts the object consists of and how the behavior for the operations is defined.

如果我们能够深入到对象的内部来观察,我们不但能够看到对象内部的信息结构,而且可以看到对象的操作行为方式。我们能够发现一个对象需要保存的所有属性列表,一个对象和其他对象的层次化包含关系,以及面向这个对象所拥有全部操作所定义的行为(behavior。图3-6详细的描述了TOM这个对象的内部结构。

图3.6

                图3.6

 

The dynamics in an object-oriented model are created through the dynamic relations, by means of objects sending stimuli to other objects. Here 'stimulus' means the event of one object communicating with another. In a programming context, the word 'message' is often used instead but, in order to avoid the message semantics, we use the stimulus concept. Additionally, the word 'stimulus' indicates that it stimulates some behavior to take action and does not necessarily include any message information. A stimulus, which is received by an object, causes an operation to be performed in the receiving object. This operation can in turn cause new stimuli to be sent. Hence, if we wish an object to perform a behavior, we send a stimulus to this object. For example, if we wish Tom to jump, then we send the 'Jump' stimulus to him. When Tom receives the stimulus, he interprets it and performs the actions which have been defined for him to do when this stimulus is received. Tom uses his legs and arms and therefore sends stimuli to his legs and arms (see Figure 3.7).

 借助对象之间互相发送激励(stimuli)这种模式,我们可以描述对象之间的动态关联关系。在这里激励(stimuli)的含义是对象之间互相通信而使用的事件信息。在一个程序设计的上下文中,常常是使用消息'message'这个单词来代替,但是为了避免和消息在语意上的混淆,我们使用激励(stimuli)的概念。另外,我们使用激励(stimuli)的概念是因为这种动态通信方式会激发对象来执行一些行为,在某些场景下是不需要包含任何消息信息的。在对象的动态通信过程中,接收到激励(stimuli)的对象会触发一个操作,有些场合下这个操作会在接收对象内部完成,有些场合下,这个操作会后续发送新的激励(stimuli)。. 所以,如果我们期望一个目标对象执行一个期望的行为,我们可以通过向对象发送激励的方式来实现。举个例子来说,如故我们希望对象TOM来做跳跃这个动作,我们就向TOM发送一个跳跃的激励。当TOM接收到这个激励以后,他首先会解释“跳跃”这个激励的含义,同时他会根据事先规定的响应要求(当接收到跳跃刺激的情况下)执行特定的行为。TOM使用他的手和脚(层次化分割的关联对象),因此向他的手和脚发送激励。详细的解释,可以参考图3-7

图3.7

                 图3.7

 

We can also consider more complicated case.  If we want the object Mary to dance, we send a stimulus “Dance” to the object Mary. When she receives this, she performs the behavior associated with dancing. She, perhaps, will only dance with her friend. She will therefore send a stimulus to this friend to start dancing (see Figure 3.8).我们还可以考虑一种更加复杂的场景。如何我们希望对象Mary来跳舞,我们回发送一个刺激“跳舞”给Mary。当她接收到这个激励以后,就会执行和跳舞有关的行为。根据事先规定的响应要求,她很有可能和她的朋友一起跳舞,所以她有再发送一个新的激励给她的朋友来开始跳舞。这种行为模式的过程见图3-8来显示。

图3.8

 

                                图3.8

All information in an object-oriented system is stored within its objects and can only be manipulated when the objects are ordered to perform operations. The behavior and information are encapsulated in the object. The only way to affect the object is to perform operations on it. Objects thus support the concept of information hiding, that is, they hide their internal structure from then surroundings (see Parnas (1972)). Every one of the object's operations performs part of the object's behavior, and can modify information in the object. In order to use an object, we do not need to know how the object's behavior or information is represented or implemented internally. We only need to know which operations it offers.

在面向对象的系统中,所有的信息都是在对象集合中存储,只有当这些对象被命令执行特定操作时候,对象管理的这些信息才会被控制和管理。因此,这些行为和信息被封装在对象的内部。影响和改变特定对象的唯一方法是针对他执行一些操作。对象就是通过这种方式来支持信息隐藏的建模概念,换句话说就是在面向对象的环境中,对象针对他的外部环境隐藏自己的内部结构对象的每一个操作可以执行对象的一部分行为,并有可能改变对象的内部信息。为了使用一个对象,我们并不需要了解对象的行为或者信息是如何描述,以及内部实现。我们唯一需要了解对象提供的操作列表。

Note that we describe the problem in terms of items or objects taken from real life and what we wish these objects to perform. We do not know how John, Tom and Mary perform their tasks or how they look inside. We have used the concepts object, operation and encapsulation to understand the problem. Encapsulation means that all that is seen of an object is its interface, namely the operations we can perform on the object.

注意在这里我们是通过真实世界中提取的对象或者条目的建模方式来描述这个信息隐藏这个概念,以及我们希望这些对象来执行一些操作。我们不知道John, Tom Mary详细执行任务的方法,我们也不知道他们内部的结构。为了更好的描述这种现实世界中的实体和行为,我们采用对象object,操作operation和封装encapsulation的概念来理解这个特定问题。封装encapsulation的概念指的是对象提供的接口是我们能够看到对象的全部(无法看到对象的内部),利用这些接口我们可以面向对象执行一组操作

These important concepts have their roots in abstract data types (see Aho et al. (1983)). An abstract data type is a model (structure), with a number of operations which affect this model. It is similar to our definition of an object and, in fact, they are closely related. Both are abstractions and are defined in terms of what they perform, not how they perform it. They are both generalizations of something specific, where encapsulation is the central concept. One of the advantages of abstract data types is that they should be able to be used independently of their implementation (information hiding), which means that even if the implementation is modified, we should not need to modify how we use the abstract data types. Another advantage is, quite simply, the reduced complexity since it is impossible to become involved in their internal structure; you can use them only according to their specifications.

些重要的概念起源于抽象数据类型这一理论模型(abstract data types)。一个抽象数据类型是一个模型(结构),他拥有一组操作,利用这组操作可以改变模型内部的数据结构。这种结构和我们针对一个对象的定义,同时,从实践的角度来看,两者之间是紧密相关的。两者都是对模型的一种抽象,他们都是从一组操作执行结果的角度来定义,而不是从操作执行的内部细节的角度来定义。他们都是基于封装这一个核心概念对客观世界实体外部特性的一种高度概括(generalizations.使用抽象数据类型的一个最大的好处就是他们能够独立于各自的实现方式(信息隐藏)而自由的应用,这就意味着即便是即便模型的实现方式已经发生了修改,而外部用户对抽象数据类型的应用方式不会发生变化。另外一个好处,非常明显,这种定义方式减低了应用的复杂度,因为外部用户不可能参与到抽象数据类型的内部结构当中,你只需要根据他们的定义来使用他们。

So far, we have only introduced the object concept. Many people using the term 'object-orientation' mean only this, but, as we have seen, we have actually not come much further than abstract data types. To be truly object-oriented, we need some further properties and concepts.

现在,我们仅仅介绍了对象这个概念。很多人在应用‘面向对象’这一概念是仅仅指的就是‘对象’本身,但是从刚才的讨论中,因为我们针对面向对象的描述是从抽象数据类型的角度来描述的。为了能够更加准确的揭示面向对象的本质,我们需要其他进一步的概念和特性。