如果你确定两件对象之间是is-a的关系,那么此时你应该使用继承;比如菱形、圆形和方形都是形状的一种,那么他们都应该从形状类继承而不是聚合。
如果你确定两件对象之间是has-a的关系,那么此时你应该使用聚合;比如电脑是由显示器、CPU、硬盘等组成的,那么你应该把显示器、CPU、硬盘这些类聚合成电脑类,而不是从电脑类继承。
网上关于此类的讨论非常多,发现对于该问题的理解各有各的说法,而各个说法中又相去甚远。通过浏览这些讨论以及对《O'Reilly - UML 2.0 In A
Nutshell (2007)》的参考,发表一下自己的看法
类间关系有很多种,在大的类别上可以分为两种:纵向关系、横向关系。
纵向关系就是继承关系,它的概念非常明确,也成为OO的三个重要特征之一,这里不过多的讨论。
横向关系较为微妙,按照UML的建议大体上可以分为四种:
- 依赖 (Dependency)
- 关联 (Association)
- 聚合 (Aggregation)
- 组合 (Composition)
它们的强弱关系是没有异议的:依赖 < 关联 < 聚合 < 组合
然而它们四个之间的差别却又不那么好拿捏,需要好好体会。
-
依赖:
- UML表示法:虚线 + 箭头
- 关系:" ... uses a ..."
- 此关系最为简单,也最好理解,所谓依赖就是某个对象的功能依赖于另外的某个对象,而被依赖的对象只是作为一种工具在使用,而并不持有对它的引用。
- 典型的例子很多,比如:
class Human
{
public void breath()
{
Air freshAir = new Air();
freshAir.releasePower();
}
public static void main()
{
Human me = new Human();
while(true)
{
me.breath();
}
}
}
class
Air
{
public void releasePower()
{
//do sth.
}
}
- 释义:一个人自创生就需要不停的呼吸,而人的呼吸功能之所以能维持生命就在于吸进来的气体发挥了作用,所以说空气只不过是人类的一个工具,而人并不持有对它的引用。
-
关联:
- UML表示法:实线 + 箭头
- 关系:" ... has a ..."
- 所谓关联就是某个对象会长期的持有另一个对象的引用,而二者的关联往往也是相互的。关联的两个对象彼此间没有任何强制性的约束,只要二者同意,可以随时解除关系或是进行关联,它们在生命期问题上没有任何约定。被关联的对象还可以再被别的对象关联,所以关联是可以共享的。
- 典型的例子很多,比如:
class Human
{
ArrayList friends = new
ArrayList();
public void makeFriend(Human human)
{
friends.add(human);
}
public static void main()
{
Human me = new Human();
while(true)
{
me.makeFriend(mySchool.getStudent());
}
}
}
- 释义:人从生至死都在不断的交朋友,然而没有理由认为朋友的生死与我的生死有必然的联系,故他们的生命期没有关联,我的朋友又可以是别人的朋友,所以朋友可以共享。
-
聚合:
- UML表示法:空心菱形 + 实线 + 箭头
- 关系:" ... owns a ..."
- 聚合是强版本的关联。它暗含着一种所属关系以及生命期关系。被聚合的对象还可以再被别的对象关联,所以被聚合对象是可以共享的。虽然是共享的,聚合代表的是一种更亲密的关系。
- 典型的例子很多,比如:
class Human
{
Home myHome;
public void
goHome()
{
//在回家的路上
myHome.openDoor();
//看电视
}
public static void main()
{
Human me =
new Human();
while(true)
{
//上学
//吃饭
me.goHome();
}
}
}
- 释义:我的家和我之间具有着一种强烈的所属关系,我的家是可以分享的,而这里的分享又可以有两种。其一是聚合间的分享,这正如你和你媳妇儿都对这个家有着同样的强烈关联;其二是聚合与关联的分享,如果你的朋友来家里吃个便饭,估计你不会给他配一把钥匙。
-
组合:
- UML表示法:实心菱形 + 实线 + 箭头
- 关系:" ... is a part of ..."
- 组合是关系当中的最强版本,它直接要求包含对象对被包含对象的拥有以及包含对象与被包含对象生命期的关系。被包含的对象还可以再被别的对象关联,所以被包含对象是可以共享的,然而绝不存在两个包含对象对同一个被包含对象的共享。
- 典型的例子很多,比如:
class Human
{
Heart myHeart = new Heart();
public static void main()
{
Human me = new Human();
while(true)
{
myHeart.beat();
}
}
}
- 释义:组合关系就是整体与部分的关系,部分属于整体,整体不存在,部分一定不存在,然而部分不存在整体是可以存在的,说的更明确一些就是部分必须创生于整体创生之后,而销毁于整体销毁之前。部分在这个生命期内可以被其它对象关联甚至聚合,但有一点必须注意,一旦部分所属于的整体销毁了,那么与之关联的对象中的引用就会成为空引用,这一点可以利用程序来保障。心脏的生命期与人的生命期是一致的,如果换个部分就不那么一定,比如阑尾,很多人在创生后的某个时间对其厌倦便提前销毁了它,可它和人类的关系不可辩驳的属于组合。
在UML中存在一种特例,就是允许被包含对象在包含对象销毁前转移给新的对象,这虽然不自然,但它给需要心脏移植的患者带来了福
相关推荐
可参考链接:is-a和has-a的区别 1、has a 关系 has a :一个类中使用了另外一种自定义的类型 类型: 系统类型 str、 int、 float、 list 、dict 、 tuple、 set 自定义类型 算是自定义的类,都可以将其当成一...
hprof-conv.exe The connection to adb is down, and a severe error has occured
Kenneth has developed this is a great little application which reads a Microsoft Exchange mailbox and lets you know via playing a .WAV file when you have mail.
IPython is a powerful interactive shell that features easy editing and recording of a work session, and supports visualizations and parallel computing. The Software Carpentry Course teaches basic ...
NULL 博文链接:https://navylee.iteye.com/blog/2076985
03-11_面向对象与is-a_has-a_7题定义.pdf
03-11_面向对象与is-a_has-a_7题[归纳].pdf
Fortunately, Bill has a working stopwatch, so they can record their speed and the total time they have driven. Unfortunately, their record keeping strategy is a little odd, so they need help ...
A8105 Data Sheet, 2.4GHz FSK/GFSK SOC ... A8105 has various operating modes, making it highly suited for systems where ultra-low power consumption is required. The device is in QFN5X5 40 pin package
It is mostly SVR4 compatible although it has some extensions (for example, handling more than 9 positional parameters to macros). GNU m4 also has built-in functions for including files, running shell...
Here_is_a_collection_of_repository_that_has_applie_Software-CopyrightSoftware-Copyright.zip
in the demo mode, or the value 1, when the driver has found a valid key, and therefore is licenced. This function is also available via the OPC server interface. The same item-ID address format has...
- My mother has a beautiful nose. → My mother doesn't have a beautiful nose. - We have a big house. → We don't have a big house. - They have a happy family. → They don't have a happy family. - ...
The text has a large number of diagrams and helpful margin notes, and uses many graded examples and exercises, often with complete solutions, to guide students through the tricky points. It is ...
You’ll take a deep dive into HTTP/2 details, and learn how this updated protocol is changing the web landscape. HTTP/1.1 has been the primary means of communicating data across the web for the past...
in mathematics, it is beautiful and compelling, and there is a plethora of material. The literature in complex variables is vast and diverse. There are a great many textbooks in the subject, but each ...
However, Niels Jørgenssen has suggested a model of how written code is integrated into the project. Figure 4-1. Jørgenssen's model for change integration The “development release” is the ...
Not to rest on success, the latest version of Delphi, Version 5, includes further expansion and refinement of the 3-tier application framework introduced in Delphi 4 and has resulted in a prize-...