UML Class Diagram III

Source: Internet
Author: User
Tags dashed line

2. Dependency relationship

A dependency (Dependency) Relationship is a usage relationship in which a change in a particular thing can affect other things that use the thing, and use dependencies when it is necessary to indicate that one thing is using another. in most cases, a dependency is embodied in a method of a class that uses an object from another class as a parameter. in UML, a dependency relationship is indicated by a dashed line with arrows, and the relying party points to the relying party. For example: The driver drives, the car type Object car is passed as a parameter in the drive () method of the driver class, so that the car's move () method can be called in the drive () method, and the driver () method relies on the car's Move () method, So the class driver dependency class car,1 shows:

Figure 1 Dependency Instance

In the system implementation phase, the dependencies are usually implemented in three ways, the first and most commonly used way is 1, the object of one class as the parameter of the method in another class , the second way is to use the object of another class as its local variable in the method of one class. The third way is to invoke the static method of another class in the method of one class . The corresponding Java code snippet in Figure 1 is as follows:

[Java]View PlainCopy
  1. Public class Driver {
  2. public void Drive (car car) {
  3. Car.move ();
  4. }
  5. ......
  6. }
  7. Public class Car {
  8. public void Move () {
  9. ......
  10. }
  11. ......
  12. }

3. Generalization relationship

a generalization (generalization) relationship is also an inheritance relationship that describes the relationship between a parent class and a subclass, which is also known as a base class or superclass, and a subclass is also known as a derived class. In UML, the generalization relation is represented by a straight line with a hollow triangle. when the code is implemented, we use an object-oriented inheritance mechanism to implement generalization relationships, such as using the extends keyword in the Java language, and using the colon ":" in c++/c#. For example: The student class and the teacher class are subclasses of the person class, and the student class and the teacher class inherit the properties and methods of the person class, the property of the person class contains the name (name), and the Age, Each of the student and teacher also has these two attributes, and the student class adds the attribute number (STUDENTNO), the teacher class adds the attribute teacher number (Teacherno), and the person class method includes the walk Move () and speak Say (), student class and teacher class inherit both methods, and student class is also new method study (), teacher class is also new method teach (). 2 is shown below:

Figure 2 Generalization Relationship Example

The corresponding Java code snippet in Figure 2 is as follows:

[Java]View PlainCopy
  1. Parent class
  2. Public class Person {
  3. protected String name;
  4. protected int age;
  5. Public void Move () {
  6. ......
  7. }
  8. public Void Say () {
  9. ......
  10. }
  11. }
  12. Sub-class
  13. Public class Student extends person {
  14. Private String Studentno;
  15. Public Void Study () {
  16. ......
  17. }
  18. }
  19. Sub-class
  20. Public class Teacher extends person {
  21. Private String Teacherno;
  22. Public Void Teach () {
  23. ......
  24. }
  25. }

4. Interface and Implementation relationship

In many object-oriented languages have introduced the concept of interfaces, such as Java, C #, etc., in the interface, usually no attributes, and all operations are abstract, only the operation of the Declaration, there is no implementation of the operation. UML represents an interface in a manner similar to the representation of a class, as shown in 3:

Figure 3 UML diagram of the interface

Interfaces can also have inheritance relationships and dependencies similar to those between classes, but there is also an implementation (realization) relationship between the interface and the class, in which the class implements the interface, and the operations in the class implement the actions declared in the interface. in UML, the implementation relationship between a class and an interface is represented by a dashed line with a hollow triangle. For example: Defines a transport interface vehicle, which contains an abstract operation Move (), which implements the move () operation in both class ship and class car, but the specific implementation details will be different, as shown in 4:

Figure 4 Implementing a relationship instance

When implementing a relationship in a programmatic implementation, different object-oriented languages also provide different syntax, such as using the Implements keyword in the Java language, and using the colon ":" in c++/c#. The corresponding Java code snippet in Figure 4 is as follows:

[Java]View PlainCopy
    1. Public interface Vehicle {
    2. Public void Move ();
    3. }
    4. Public class ship implements Vehicle {
    5. Public void Move () {
    6. ......
    7. }
    8. }
    9. Public class Car implements Vehicle {
    10. Public void Move () {
    11. ......
    12. }
    13. }

UML Class Diagram III

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.