Summary of the following four relationships
1. Generalization
2. Aggregation
3. Dependency
4. Join
1. Generalization
Is the integration relationship between the parent class and the Child class.
[Specific performance]
Parent class instance = new subclass ()
[UML diagram] (Fig. 1.1)
Figure 1.1generalized relationship between animal class and tiger class and Dog class
[Code performance]
[Code performance]
Java code
- classAnimal{}
-
- classTigerextendsAnimal{}
-
- publicclassTest
- {
- publicvoidtest()
- {
- Animala=newTiger();
- }
- }
-
2. Aggregation relationship
Include, integrate, not inherit.
For example, if Michael's computer is stolen, all the components of the computer do not exist, unless Michael has removed some computer components (such as hard disks and memory) beforehand.
[UML diagram] (Fig. 1.4)
Figure 1.3 computer and component Aggregation
[Code performance]
Java code
- PublicclassComputer {
- PrivateCPUcpu;
- PublicCPUgetCPU (){
- Returncpu;
- }
- PublicvoidsetCPU (CPUcpu ){
- This. cpu = cpu;
- }
- // Enable the computer
- Publicvoidstart (){
- // Cpu operation
- Cpu. run ();
- }
3. Dependency
Other objects or objects are required.
[Specific performance]
Dependencies are manifested in local variables, method parameters, and calls to static methods.
[Real-world example]
For example, if you want to screw the screw, do you want to use (that is, rely on) the screw driver to help you complete the screw (screw) work?
[UML representation] (Figure 1.2)
Figure 1.2person class and screwdriver class dependency
[Code performance]
Java code
- PublicclassPerson {
- /** Screw */
- Publicvoidscrew (Screwdriverscrewdriver ){
- Screwdriver. screw ();
- }
- }
4. Join
When it does not belong to the preceding relationship, the two entities have some specific relationships, which are associated
[Specific performance]
Associations are implemented using instance variables.
[Real-world example]
For example, for customers and orders, each order corresponds to a specific customer, and each customer corresponds to a specific order. For example, for companies and employees, each company corresponds to a specific employee, each employee corresponds to a specific company
[UML diagram] (Fig. 1.3)
Figure 1.3 association between companies and employees
[Code performance]
Java code
- PublicclassCompany {
- PrivateEmployeeemployee;
- PublicEmployeegetEmployee (){
- Returnemployee;
- }
- PublicvoidsetEmployee (Employeeemployee ){
- This. employee = employee;
- }
- // Company operation
- Publicvoidrun (){
- Employee. startWorking ();
- }
- }