UML class diagram Learning

Source: Internet
Author: User

Unified Modeling Language (UML. It includes the following important functions:

  • Visualizing)
  • Specifying)
  • Construting)
  • Document creation (documenting)

UML includes:

  • User case diagrams)
  • Class Diagrams)
  • Sequence Diagram (sequence diagrams)
  • Collaboration diagrams)
  • Activity diagram (activitiy diagrams)
  • Component diagram (Component diagrams)
  • Deployment Diagrams)

The use case diagram, class diagram, and sequence diagram are the most important.

I will summarize the class diagrams:

Class diagram: shows the class, interface, and static structure and relationship between them. The most basic elements of a class chart are classes and interfaces.

As shown in:

The first layer: class name. The second layer is the attribute layer, including public (+), protected (#), and private (-). The third layer is the public (+) method, protected (#) method, and private (-) method. According to the class diagram, the code can be changed to a specific Java code, so I will not write it here.

Class Diagram describing the interface:

Relationship in the class diagram: there is a line between the class and the class to indicate the relationship between them. The following relationships can be established between classes and classes, classes and interfaces, interfaces, and interfaces: General relationships, associations, aggregation relationships, and dependencies. These relationships are static.

  • Generalization: indicates the inheritance relationship between the class and the class, the inheritance relationship between the interface and the interface, or the Implementation relationship between the class and the interface.

As shown in:

Figure 1-inheritance relationship between classes and classes (represented by a real-line white triangle) Figure 2: Implementation of classes on interfaces (represented by a dotted line white triangle)

  • Association: a connection between a class and a class. It allows a class to know the attributes and methods of another class. An association can be bidirectional or unidirectional. An arrow is used to indicate the association direction. A bidirectional association can have two arrows or no arrows. For example:

            

It is equivalent to the following Java code:

Class driver {

Priavte car; // a car-type instance variable is displayed in the driver class.

}

Class car {

}

Each association has a name. In the preceding figure, the Association name is drivers.

Each associated endpoint can also have a base number, indicating that the class at one end can have several instances. For example, Tang Miao has an association with his apprentice. In this relationship, Tang Miao has only one, but there are several disciples. For example:

 

An association can be further determined as an aggregation or merging relationship. For example, the relationship between Tang Seng and his apprentice can be further determined as an aggregation relationship.

  • Aggregation: a type of strong association. Aggregation is the relationship between the whole and the individual. For example, the relationship between automobile, engine, tire, and other parts is the relationship between the whole and the individual.

A car object consists of an engine object and four tire objects, such:

Like the association relationship, the aggregation relationship is also implemented through instance variables:

Java code: The car class should have an attribute of engine type and an array attribute of tire type.

Class car {

Private engine;

Private tire [] tires;

}

The two classes involved in the Association are at the same level, while the two classes in the aggregation class are at an unequal level. One represents the whole, and the other represents the part.

  • Composition: a type of association, which is stronger than an aggregation relationship. It requires that the object representing the whole in a common aggregation relationship be responsible for representing the lifecycle of some objects. The compositing relationship cannot be shared. See the following figure:

The relationship between monkeyking and limb and goldringedstaff used by monkeyking is displayed. It can be seen that monkeyking is in an aggregation relationship with goldringedstaff, And that Monkey King is in a synthetic relationship with his four legs (limb. Because the limbs of the Monkey King are completely under the responsibility of the Monkey King and cannot be shared, the life cycle of the Monkey King is related to the life cycle of his legs. Once the Monkey King fails, his legs will not survive. But the golden hoop is less important than his legs.

  • Dependency: it is also the relationship between classes, and dependency is always one-way. Dependency indicates that one class depends on the definition of another class. For example, a person can buy a car to buy a house ). The person class depends on the car class and house class.

Unlike associations, the person class does not have the attributes of the car and house classes. The car and house instances are stored in the buy () method as parameters.

Java code:

Class person {

Public void buy (car ){

......

}

Public void buy (house ){

......

}

}

In general, dependencies are reflected in the local variables, method parameters in Java, and calls to static methods. In other words, if one local variable type of a Class A is another class B, Class A depends on Class B. If the parameter of a method is another type of B, the Class A of this method depends on Class B. Or if a Class A calls the static method of another class B, Class A depends on Class B.

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.