类之间的6种关系

类与类之间的关系

继承(Generalization)关系、又叫作泛化
实现(Realization)关系
组合(Composition)关系
聚合(Aggregation)关系
关联(Association)关系
依赖(Dependency)关系

类关系从强到弱依次是: 继承>实现>组合>聚合>关联>依赖


A is-a B 泛化(继承,实现)
继承关系 中,子类继承父类的所有功能,父类所具有的属性、方法,子类都应该有。
除了与父类一致的信息,子类中还包括额外的信息。

例如,公交车、出租车都是车
接口(包括抽象类)是方法的集合
实现关系 中,类实现了接口,类中的方法实现了接口声明的所有方法。

例如,人、动物都吃饭都睡觉

A has-a B 包含(紧密程度:组合 > 聚合 > 关联)
关联(Association)关系
Association is a relation between two separate classes which establishes through their Objects.

Association can be
1、one-to-one,
2、one-to-many, i.e. Bank can have many employees
3、many-to-one,
4、many-to-many.

In Object-Oriented programming, an Object communicates to another object to use functionality and services provided by that object.
比如说:人有汽车,人有电脑
Composition and Aggregation are the two forms of Association.
聚合(Aggregation)关系
It is a special form of Association where:

1、It represents Has-A’s relationship.
2、It is a unidirectional association i.e. a one-way relationship.
For example, a department can have students but vice versa is not possible and thus unidirectional in nature.
3、In Aggregation, both the entries can survive individually which means ending one entity will not affect the other entity.
When do we use Aggregation ?
Code reuse is best achieved by aggregation.
比如说:工人和工作服,汽车和车轮,电脑和主板
组合(Composition)关系
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.

1、It represents part-of relationship.
2、In composition, both entities are dependent on each other.
3、When there is a composition between two entities, the composed object cannot exist without the other entity.
For example, Book is Part-of Library. If Library gets destroyed then All books within that particular library will be destroyed. i.e. books can not exist without libraries. That’s why it is composition.
比如说:人和脑袋,人和大脑,人和心脏

Aggregation vs Composition
1. Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human

2. Type of Relationship: Aggregation relation is “has-a” and composition is “part-of” relation.

3. Type of Association: Composition is a strong Association whereas Aggregation is a weak Association.

A need-a B 依赖(依赖)
依赖关系 是一种“使用”关系,特定事物的改变有可能会影响到使用该事物的其他事物,当需要表示一个事物使用另一个事物时,使用依赖关系。
在大多数情况下,依赖关系体现在某个类的方法使用另一个类的对象作为参数

例如,汽车依赖汽油,如果没有汽油,则汽车将无法行驶。

Java程序体现的形式为
has a :一个类的对象当做另一个类的属性来存储
对于赋值 :
1. 可以在构造方法里赋值
2. 可以通过set方法赋值;
need a:一个类的方法中使用到了另一个类的对象
对于赋值:
1. 可以在方法中自己创建
2. 可以在方法中传递进来

-----------------------------------------------------------------------------读书笔记摘自 书名:《设计模式就该这样学:基于经典框架源码和真实业务场景》(谭勇德)

-----------------------------------------------------------------------------读书笔记摘自 书名:Article–《Association, Composition and Aggregation in Java》
-----------------------------------------------------------------------------读书笔记摘自 书名:自己总结

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值