The UML class diagram of Java tool

Source: Internet
Author: User
Tags dashed line visibility

Tag: Mode add phase Complete key OSI Test Interface System Analysis

Objective

The UML (Unified Modeling Language) Chinese Unified Modeling Language is an open approach for explaining, visualizing, building, and writing an open approach to an object-oriented, software-dense system of artifacts. UML presents a series of best practices that are validated for modeling large-scale, complex systems, especially at the software architecture level. "Wikipedia"

Recently look at some open source projects, always see the UML class diagram, the beginning of their own practice is to skip the part of the class diagram, but later encountered several times, decided to learn, can no longer choose to escape, which is also a good program to explore the necessary quality. Today I have learned the contents of the record (afraid of forgetfulness ...) , believe that the class diagram is not clear friends, through reading this article can also have a clearer understanding, let you see understand, draw out.

Class Diagram action

Class diagram is a static structure diagram of a unified Modeling language for software engineering, which describes the system's class collection, the class's properties, and the relationships between classes. To help people to simplify the understanding of the system, it is an important product of the system analysis and design stage, and also is the important model basis for system coding and testing. Learning to draw a good class diagram is a skill that a qualified software engineer should have.

UML Diagrams for classes

In a UML class diagram, a class uses a rectangle that contains the class name, attributes, method names and their arguments, separated by a split line. For example, the simplest class of the person class has two attributes, name and age, respectively, and provides the corresponding get and set methods. The Java class code is as follows

/*********************************************************************** * Module:Person.java * Author: Code4android * Purpose:defines the Class person ******************************************************************** ***/Importjava.util.*;/** @pdOid 4615fa10-38a3-446e-a7d5-59dcb3e786b5 * / Public  class  person {   /** @pdOid f314a698-c3c1-4ec7-a8ea-f8b2df107a29 * /   PrivateString name = Zhangsan;/** @pdOid 271c633b-87e5-4b41-9f70-2ce320635014 * /   Private intAge = at;/** @pdOid ccf5b7c1-d005-4a5f-b823-9988f2dd6f91 * /    PublicStringGetName() {//Todo:implement      returnName }/** @pdOid 83E01FBA-B004-498E-B7AB-778DE8BE6DFA * /    Public int Getage() {//Todo:implement      returnAge }/** @param name * @pdOid 9525895a-11bf-44a3-afed-b4a014540a98 */    Public void SetName(String name) {//Todo:implement       This. name=name; }/** @param Age * @pdOid 16fd66cc-2af1-4fef-ae98-2a37f495a487 * *    Public void Setage(intAge) {//Todo:implement       This. age=age; }}

Then the class diagram is represented as follows, it is very simple to represent all the information of the class.

Through the class diagram of the person above, you should see that the class diagram consists of three parts, the class name, the properties, and the method of operation.

Class name

As the name implies, the class names, corresponding to the class name in the Java class, if the class is an abstract class, in the lower right corner of the class name will have an (abstract), if the person is an abstract class, then the UML class diagram is as follows

Property name

The attribute names in UML are member variables in Java, and of course a class can have no attributes or any number of properties. modifiers, types, and default values for member variables in Java classes can be represented in UML class diagrams. The general presentation method is as follows

可见性  名称:类型 [ = 默认值 ]

The visibility in Java classes is divided into three categories, private,public and protected, which are represented by symbols-, +, and #, respectively, in class diagrams. The name of the generic representation method is the name of the member variable, and the type is the type of the member variable, such as the string type, int type, and, of course, the custom type. The default value is optional, and if we do not set the initial value for the member variable, the UML class diagram does not display, such as the person class diagram above, the variable name,age does not set the initial value, if at this time we set the name and vaule the default value zhangsan,23. The UML class diagram is as follows

Operation method
可见性  [ : 返回类型]

The methods of operation of the class are generally represented as above, the method visibility and property visibility are consistent, the name is the method name, the parameter list is optional, can be a parameter is also no parameter, if multiple parameters separated by commas, the return type is an optional, the return value type of the method, depends on the specific programming language, Can be a basic data type, a user-defined type, or an empty type (void), and no return type if it is a construction method.

The relationships between classes

There are many relationships between classes and classes in UML class diagrams, such as generalization (generalization) relationships, implementation (realization) relationships, dependency (dependence) Relationships, Association (association) relationships, Aggregation (Aggregation) relationships, combined (composition) relationships.

Generalization (generalization) relationship

Generalization is the inheritance in Java, classes and classes, interfaces and interfaces can be inheritance relationships, the parent class is also known as a base class or a superclass, a subclass is also called a derived class, the class inherits the parent class can implement the function of the parent class, and can have the function that the parent class does not have. In UML, the generalization relation can be represented by a straight line with a hollow triangle; for example, we create two Java classes Teachers and students class code like this

/*********************************************************************** * Module:Teachers.java * Author: Code4android * Purpose:defines the Class Teachers ****************************************************************** *****/Importjava.util.*;/** @pdOid b54e2d34-d17e-4f2d-993d-563b8e007db4 * / Public  class Teachers extends  person {   /** @pdOid 55b38630-1e30-449b-9cb7-f8ef9de59412 * /   Private intTnumber;/** @pdOid f603d47d-a51f-4b0c-b10b-881842374f8a * /    PublicStringTeach() {//Todo:implement      return "I am Teaching"; }}/*********************************************************************** * Module:Students.java * Author: Code4android * Purpose:defines the Class Students ****************************************************************** *****/Importjava.util.*;/** @pdOid 6b8ac239-000e-46d3-8233-962a612c12bd * / Public  class Students extends  person {   /** @pdOid 0c7627fd-fa78-4f60-a859-2b90274323e1 * /   Private intSnumber;/** @pdOid 1f929347-b84a-4a54-9ca9-144de66c742b * /    PublicStringstudy() {//Todo:implement      return "I am Learning"; }}

The UML class diagram is as follows

Implementing (Realization) relationships

Implementing a relationship is a relationship between a class and an interface in Java, where there are generally no member variables, all operations are abstract (abstract adornments), and only declarations do not have specific implementations, which are implemented in the class that implements the interface. In UML, the interface is represented in a manner similar to that of a class, and the difference can be seen in UML class diagrams. As we create a transport class interface ivehicle, and have a formal speed method declaration Travelspeed,java code as follows

/*********************************************************************** * Module:  IVehicle.java * Author:  Code4Android * Purpose: Defines the Interface IVehicle ***********************************************************************/import java.util.*;/** @pdOid 7cbe9f91-a7d9-44b5-a743-280cbc464e61 */publicinterface IVehicle {   /** @pdOid 5e87ebbc-5647-4dc6-8c3c-d23857662584 */   int travelSpeed();}

For transport, there are many different modes of transport have a speed, we give two examples, such as highspeedrail (high-speed rail, speed 288KM) and bicycle (bicycle class speed 20KM), then two implementation class code for

/*********************************************************************** * Module:HighSpeedRail.java * Author: Code4android * Purpose:defines the Class highspeedrail ************************************************************* **********/Importjava.util.*;/** @pdOid 0ae31b48-07b0-4bcd-83ff-a8a7c7d94518 * / Public  class highspeedrail implements ivehicle {   /** @pdOid a7693fd4-d6d4-4e93-a946-380f69011b13 * /    Public int Travelspeed() {//Todo:implement      return 288; }}/*********************************************************************** * Module:Bicycle.java * Author: Code4android * Purpose:defines the Class Bicycle ******************************************************************* ****/Importjava.util.*;/** @pdOid c212a8c3-88db-48d8-a2a0-d381d2ee7f91 * / Public  class Bicycle implements ivehicle {   /** @pdOid 9ac52769-7489-47b0-9079-4f63f84f1fde * /    Public int Travelspeed() {//Todo:implement      return  -; }}

Implementing an interface in UML is represented by a dashed line and a hollow triangle. The UML class diagram above corresponds to the following

Dependency (dependence) relationship

Dependencies are the weakest relationship between classes and classes, which can easily be understood by a class using another class, which has temporary characteristics, but one class is affected by the change of another class, such as in the example above, if our students class has a bicycle, Then to get the moving speed, you need to use the method Travelspeed () method to get the speed in bicycle. At this point the students code is as follows

/*********************************************************************** * Module:Students.java * Author: Code4android * Purpose:defines the Class Students ****************************************************************** *****/Importjava.util.*;/** @pdOid 6b8ac239-000e-46d3-8233-962a612c12bd * / Public  class Students extends  person {   /** @pdOid 0c7627fd-fa78-4f60-a859-2b90274323e1 * /   Private intSnumber;/** @pdOid 1f929347-b84a-4a54-9ca9-144de66c742b * /    PublicStringstudy() {return "I am Learning"; }/** @param bicycle * @pdOid 82bd76ff-f70c-4e25-bea5-8de19db4699c * *    Public int Movespeed(Bicycle Bicycle) {returnBicycle.travelspeed (); }}

In UML, a column with arrows is used to indicate dependency, the corresponding UML class diagram is as follows

Association (Association) relationship

Association relationships indicate that a class is associated with another, for example, in the example above, where each teachers has a home address corresponding to it, and teacher and address form a one-to-one relationship. As follows

An association relationship is a containment relationship that is represented in UML with a solid line with arrows pointing to the contained class. In the you may find that there are 1 online. 1, this is a description of the inclusion relationship. There are several types of UML classes.

1..1 表示另一个类的一个对象只与该类的一个对象有关系0..* 表示另一个类的一个对象与该类的零个或多个对象有关系1..* 表示另一个类的一个对象与该类的一个或多个对象有关系0..1 表示另一个类的一个对象没有或只与该类的一个对象有关系* 任意多个对象关联
Aggregation (Aggregation) relationships

An aggregation relationship is a relationship between a whole and a part, but some can exist from the whole. For example, a teachers object has a car, and car is part of teachers, but car can be separated from the teachers. The aggregation relationship in a UML class is represented by a straight line with a hollow diamond.

Combination (composition) relationship

The combinatorial relationship is also a part-to-whole relationship, but part of the survival cycle is affected by the whole, if the whole does not exist the part will not exist. This section needs to be created in the overall construction method. In a UML class, a composite relationship is represented by a straight line with a solid diamond.

At the end of the article, attach the complete UML class diagram drawn in this article.

To this end, this article is really over, if the article has insufficient or wrong place, welcome to correct, to prevent other readers to guide the wrong. Finally thank Lovelion for the series of articles.

The UML class diagram of Java tool

Related Article

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.