This article is reproducedArticle: Http://zjzkiss.cnblogs.com/
The world is universally connected, soProgramClasses in the 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. Shown inCodeFor a method of the dependent class, the dependent class is used 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) {
4Boat. 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 employee;
3 Public Employee getemployee () {
4ReturnEmployee;
5}
6 Public Void Setemployee (employee Employee) {
7This. Employee=Employee;
8}
9 // Company operations
10 Public Void Run () {
11Employee. 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:
CombinationUMLFigure:
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 () {
4ReturnCPU;
5}
6 Public Void Setcpu (CPU) {
7This. CPU=CPU;
8}
9 // Turn on your computer
10 Public Void Start () {
11//CPU operation
12CPU. Run ();
13}
14 }
15