Association, Aggregation, Composition and Dependency
Association vs. Dependency
Association
Association的定义:
描述两个类之间的结构关系:一个类的实例连接到另一个类的实例
An association is a structural relationship between classes that specifies that objects
of one class are connected to objects of another class。
#严格的说,An association represents a structural relationship that connects two classifiers
通常也可以用class泛指其他3类。
Classifiers: class、interface、data type、component。
Association有5种类型:
1. Bi-directional:
2. Uni-directional associations:
3. Association class:
4. aggregation(Basic aggregation):
5. Composition (Composition aggregation)
Reflexive associations
Association举例:
Bi-directional:
Association class:
Dependency
Dependency的定义:(From UML Spec)
A dependency signifies a supplier/client relationship between model elements where the modification of the supplier may impact the client model elements. A dependency implies the semantics of the client is not complete without the supplier. The presence of dependency relationships in a model does not have any runtime semantics implications; it is all given in terms of the model-elements that participate in the relationship, not in terms of their instances (runtime).
Dependency is a model-level relationship and not a run-time relationship—describing the need to investigate the model definition of the client element for possible changes if the model definition of the supplier element is changed.
Dependency的类型:
Type of dependency | Keyword or Stereotype | Description |
Abstraction | «abstraction», «derive», «refine», or «trace» | Relates two model elements, or sets of model elements, that represent the same concept at different levels of abstraction, or from different viewpoints. |
Binding | «bind» | Connects template arguments to template parameters to create model elements from templates. |
Realization | «realize» | Indicates that the client model element is an implementation of the supplier model element, and the supplier model element is the specification. |
Substitution | «substitute» | Indicates that the client model element takes the place of the supplier. The client model element must conform to the contract or interface that the supplier model element establishes. |
Usage | «use», «call», «create», «instantiate», or «send» | Indicates that one model element requires another model element for its full implementation or operation. |
Dependency的图例:
Notes: instantiate stereotype
A stereotyped usage dependency among classifiers indicating that operations on the client create instances of the supplier
Dependency的适用场合(Guideline: when to use Dependency)
Model a Dependency When The Relationship is Transitory。
Connect two classes to indicate that the connection between them is at a higher level of abstraction than an association relationship. The dependency relationship indicates that the client class performs one of the following functions:
¡ Temporarily uses a supplier class that has global scope
¡ Temporarily uses a supplier class as a parameter for one of its operations
¡ Temporarily uses a supplier class as a local variable for one of its operations
¡ Sends a message to a supplier class
Composition vs. Aggregation
Composition:部分的生命周期依赖于整体的生命周期,也就是说部分不能脱离整体而存在。
整体的个数不会超过1,也就是说“部分”是不共享的。这是Composition的关键特性。而Aggregation的“部分”是可以共享的。在“拥有”关系上,Composition比Aggregation更强,更“自私”。
在早期UML Draft中也叫aggregation-by-value,在语义上等价于类的属性。
代码举例:
public class Engine
{
. . .
}
publicclassCar
{
Engine e = new Engine();
.......
}
Aggregation:用来表示拥有或者整体/部分的关系,但是部分的存在可以不依赖整体。
可以视为“aggregation-by-reference”
代码举例:
public class Address
{
. . .
}
publicclassPerson
{
private Address address;
public Person(Address address)
{
this.address = address;
}
. . .
}
Address address =new Address();
Person personA = new Person(address);
Person personB =new Person(address); // A和B两人合住在相同的地址
Composition can be used to model by-value aggregation, which is semantically equivalent to an attribute. In fact, composition was originally called aggregation-by-value in an earlier UML draft, with “normal” aggregation being thought of as aggregation-by-reference.
参考资源:
http://faq.javaranch.com/java/AssociationVsAggregationVsComposition
http://www.jguru.com/faq/view.jsp?EID=51520
http://www.atomicobject.com/pages/Aggregationaggregation by value
http://www.c-sharpcorner.com/UploadFile/pcurnow/compagg07272007062838AM/compagg.aspx