Difference between dependency and composition?(依赖和组合之间的区别?)
问题描述
此处的定义
依赖
一个类的结构或行为的变化会影响其他相关的类,那么这两个类之间存在依赖关系.它需要不一样,反之亦然.当一个类包含另一个类时发生这种情况.
Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.
作曲
组合是聚合的一种特殊情况.在更具体的方式,受限聚合称为组合.当一个物体包含另一个对象,如果包含的对象不存在不存在容器对象,则称为作文.
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
此处 和 这里
依赖
class Employee {
private Address address;
// constructor
public Employee( Address newAddress ) {
this.address = newAddress;
}
public Address getAddress() {
return this.address;
}
public void setAddress( Address newAddress ) {
this.address = newAddress;
}
}
作曲
final class Car {
private final Engine engine;
Car(EngineSpecs specs) {
engine = new Engine(specs);
}
void move() {
engine.work();
}
}
推荐答案
可以看出两个构造函数的区别:
The difference can be seen in the two constructors:
依赖:
Address
对象来自外部,它被分配到别的地方.这意味着Address
和Employee
对象是分开存在的,并且只是相互依赖.
Dependency: The
Address
object comes from outside, it's allocated somewhere else. This means that theAddress
andEmployee
objects exists separately, and only depend on each other.
Composition:在这里您可以看到在inside Car
中创建了一个新的Engine
.Engine
对象是 Car
的一部分.这意味着 Car
由 Engine
组成.
Composition: Here you see that a new Engine
is created inside Car
. The Engine
object is part of the Car
. This means that a Car
is composed of an Engine
.
这篇关于依赖和组合之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:依赖和组合之间的区别?


基础教程推荐
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01