UML diagram and code representation of class-to-class relationships

Source: Internet
Author: User
UML diagram and code representation of class-to-class relationships Keywords:Oo uml, generalization, dependency, association, aggregation

The relationship between classes plays an important role in understanding object-oriented objects. I was often asked this question during interviews. Here I will introduce it.
There is a relationship between the class and the class:
(1) Generalization)
(2) Association)
(3) Dependency)
(4) Aggregation)

UML diagram and application code example:
1. Generalization)
[Generalization]
Represents the inheritance relationship between classes, the inheritance relationship between interfaces, or the Implementation relationship between classes on interfaces. The general relationship is directed from the subclass to the parent class, which is opposite to the inherited or implemented method.
[Specific performance]
Parent class instance = new subclass ()
[UML diagram] (Fig. 1.1)

Fig 1.1 Generalization relationship between animal class, Tiger class, and dog class

[Code performance]

  1. ClassAnimal {}
  2. ClassTiger extends animal {}
  3. PublicClass Test
  4. {
  5. Public void test ()
  6. {
  7. Animal A = new tiger ();
  8. }
  9. }

2. Dependency)
[Dependency]
For two relatively independent objects, when one object is responsible for constructing an instance of another object or dependent on the service of another object, the two objects are mainly dependent on each other.
[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.2 dependency between the person class and the screwdriver class

[Code performance]

  1. PublicClass person {
  2. /** Screw */
  3. Public void screw (screwdriver ){
  4. Screwdriver. Screw ();
  5. }
  6. }

3. Association)
[Association]
For two relatively independent objects, when the instance of one object has a fixed ing relationship with some specific instances of the other object, the two objects 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)

Fig 1.3Association between companies and employees


[Code performance]

  1. PublicClass 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. }

(4) Aggregation)
[Aggregation]
When object A is added to object B and becomes an integral part of object B, object B and object a are clustered. Aggregation is a type of association. It is a strong association, emphasizing the relationship between the whole and the part.
[Specific performance]
Like the association relationship, the aggregation relationship is also implemented through instance variables. There is no way to distinguish between associations and aggregation syntactically. In terms of semantics, the difference between the two can be better distinguished.
[Differences between Association and aggregation]
(1) The two objects involved in the Association are at the same level. For example, people and bicycles are associated rather than aggregated, because people are not composed of bicycles.
The two objects involved in an aggregation relationship are at an unequal level. One represents the whole and the other represents the part. For example, the computer and its display, keyboard, motherboard and memory are clustered, because the motherboard is part of the computer.
(2) For two objects with a clustering relationship (especially a strong clustering relationship), the overall object will restrict the lifecycle of the object that consists of it. Objects of a partial classification cannot exist independently. The lifecycle of an object depends on the lifecycle of the object of the entire class. When the entire lifecycle disappears, the partial lifecycle disappears. 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]

  1. PublicClass computer {
  2. Private CPU;
  3. Public CPU getcpu (){
  4. Return CPU;
  5. }
  6. Public void setcpu (CPU ){
  7. This. CPU = CPU;
  8. }
  9. // Enable the computer
  10. Public void start (){
  11. // CPU operation
  12. CPU. Run ();
  13. }
  14. }

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.