Summary of four relationships in a UML diagram

Source: Internet
Author: User

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
 

 
 
  1. classAnimal{}  
  2.  
  3. classTigerextendsAnimal{}  
  4.  
  5. publicclassTest  
  6. {  
  7. publicvoidtest()  
  8. {  
  9. Animala=newTiger();  
  10. }  
  11. }  
  12.  

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
 

 
 
  1. PublicclassComputer {
  2. PrivateCPUcpu;
  3. PublicCPUgetCPU (){
  4. Returncpu;
  5. }
  6. PublicvoidsetCPU (CPUcpu ){
  7. This. cpu = cpu;
  8. }
  9. // Enable the computer
  10. Publicvoidstart (){
  11. // Cpu operation
  12. Cpu. run ();
  13. }

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
 

 
 
  1. PublicclassPerson {
  2. /** Screw */
  3. Publicvoidscrew (Screwdriverscrewdriver ){
  4. Screwdriver. screw ();
  5. }
  6. }

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
 

 
 
  1. PublicclassCompany {
  2. PrivateEmployeeemployee;
  3. PublicEmployeegetEmployee (){
  4. Returnemployee;
  5. }
  6. PublicvoidsetEmployee (Employeeemployee ){
  7. This. employee = employee;
  8. }
  9. // Company operation
  10. Publicvoidrun (){
  11. Employee. startWorking ();
  12. }
  13. }

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.