Dependency, association, aggregation, combination, and Generalization

Source: Internet
Author: User
Dependency, association, aggregation, combination, and Generalization

This article is reprinted article: http://zjzkiss.cnblogs.com/
The world is universally connected, so classes in the program world cannot be isolated. UML defines the relationships between them, that is, dependency, association, aggregation, combination, and generalization.

The general relationship is better understood, that is, the inheritance relationship between classes. Obfuscation is the relationship between dependency, association, aggregation, and combination. Here we will do some screening:

1. Reverse dependency and association

I found the difference between dependency and association on the Internet, saying "association itself is a kind of dependency", and "dependency is a weak Association". In fact, it is a sub-task. Dependency and Association indicate that one class uses another class. The difference is that one is to use and the other is to own.

Dependency: Has a certain chance. For example, if I want to cross the river without a bridge, I will borrow a boat to cross the river. The relationship between me and the boat is just the relationship between use (borrow. In code, a method of the dependent class takes the dependent class as its parameter. Or a method of Class A creates an instance of Class B or calls a static method of Class B. If a depends on B, it means that the change of B may require that a also change;

This is the dependency shown in the UML diagram:

Code performance:

 

1 public class person {
2/** rowing */
3 Public void oarage (boat ){
4 boat. oarage ();
5}
6}
7

Association: Relationships between famous customers and orders and between companies and employees are all associated. There is also an example of my bike and I. They all have a "own" relationship. As shown in code, a class contains instances of another class,Generally, the associated class appears in the class definition of the associated class in the form of class attributes. It can also be displayed that the associated class references a global variable of the associated class. Association can be unidirectional or bidirectional.

UML diagrams and codes of companies and employees found on the Internet:


Association between companies and employees

1 public class company {
2 private employee;
3 Public Employee getemployee (){
4 return employee;
5}
6 Public void setemployee (employee Employee ){
7 This. Employee = employee;
8}
9 // company operation
10 public void run (){
11 employee. startworking ();
12}
13}
14

It can be seen that there is a dynamic relationship between the dependent and associated classes. The associated classes reference the instance variables of the associated classes statically, and the dependency contingency also shows its dynamic nature.

2. Aggregation and combination are the same as output.

In fact, aggregation and combination are all associated special cases and are all relations between the whole and the part. Their difference is that the two aggregated objects are separated and they have their own lifecycles. However, a combination usually represents a dependency relationship between lips and teeth.

Aggregation: a kind of relationship that can hold or contain data, such as the relationship between airports and airplanes, cars, and tires. In fact, the relationship between the company and its employees is also aggregated.

Combination: it can also be called forced aggregation. The whole and part are inseparable. The end of the entire lifecycle is the end of the partial lifecycle. What is famous is the relationship between tables and legs.

Aggregate UML diagram:

Combined UML diagram:


However, the Code representations of aggregation and combination are the same and can all be represented in the following forms. They only have semantic differences.

Code representation of the relationship between computers and CPUs found on the Internet:

 

1 public class computer {
2 Private CPU;
3 Public CPU getcpu (){
4 return CPU;
5}
6 Public void setcpu (CPU ){
7 This. CPU = CPU;
8}
9 // turn on the computer
10 public void start (){
11 // CPU operation
12 CPU. Run ();
13}
14}
15

The above is from, http://zjzkiss.cnblogs.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.