《The Object-Oriented Thought Process》读书笔记6
9 Building Objects
When inheritanceis used, the end result is, at least conceptually, a single class thatincorporates all of the behaviors and attributes of the inheritance hierarchy. Whencomposition is used, one or more classes are used to build another class.
Composition Relationships
Is-a and Has-a
Building in Phases
Keep things assimple as possible.
In a 1962article titled “The Architecture of Complexity,” Nobel Prize winner HerbertSimon noted the following thoughts regarding stable systems:
n “Stable complex systems usually take theform of a hierarchy, where each
systemis built from simpler subsystems, and each subsystem is built from
simplersubsystems still.”
n “Stable, complex systems are nearlydecomposable.”
decomposable
英 [di:kəm'pəʊzəbl] 美 [dikəm'poʊzəbl]
adj. 可分解的
n “Stable complex systems are almostalways composed of only a few different
kindsof subsystems, arranged in different combinations.”
n “Stable systems that work have almostalways evolved from simple systems
thatworked.”
Types of Composition
association andaggregation.
In this book, an association is a form of composition.
In an aggregation, you normally seeonly the whole,
and in associations, you normally seethe parts that make up the whole.
Aggregations聚合
Figure 9.4 An aggregation hierarchyfor a car.
Similarly, whenyou go to buy a car, you do not pick and choose all the individual componentsof the car.
Association关联
A computer and a mouse,
The computer boxneeds the service of a mouse, but does not have
the capability to provide thisservice by itself.
关联类通过一条虚线与关联连接。
Aggregation Versus Association
An aggregation is a complex object composed of otherobjects. An association is used when one object wants another object to performa service for it.
Using Associations andAggregations Together
One thing youmight have noticed in all the examples is that the dividing lines between whatis an association and what is an aggregation are blurred.
For example, thecomputer system example used to describe association also contains someaggregation.
Design is not anexact science.
Avoiding Dependencies
You need todetermine what is more important in specific situations: whether you wantconvenience or stability. There is no right answer. It all depends on theapplication and the environment.
Cardinality基数
Mandatory
英 [ˈmændətəri] 美 [ˈmændətɔri]
adj. 强制的;命令的;受委托的
n. 受托者
Cardinality Notation
Thenotation of 0...1 means that an employee can have either zero or one spouse.
Figure 9.7 Cardinality in a UMLdiagram.
Multiple Object Associations
Note that theclasses that have a one-to-many relationship are represented by arrays in thecode:
private Child[] child;
privateJobDescription[] jobDescriptions;
Optional Associations
The bottom lineis that the code must check for a null condition, and must handle this as a valid condition.
Try It All Together: An Example
Figure9.9 AUML diagram for the Dog example.
Conclusion
Whereasinheritance represents a new kind of already-existing object, compositionrepresents the interactions between various objects.