UML (Unified Modeling Language) Literacy

1. Overview of UML

            UML (Unified Modeling Language, Unified Modeling Language) is the current standard language for object-oriented software system modeling. It combines the advantages of many software modeling technologies and describes the system through a series of standard graphic symbols.

            UML is a general-purpose visual modeling language , which is different from programming languages. It models the system through some standard graphic symbols and text, and is used to describe the software, visually process, construct and establish the documentation of the software system products. UML is suitable for various software development methods, various stages of the software life cycle, various application fields, and various development tools. UML is a set of standard modeling methods that summarize the experience of past modeling technologies and absorb the best results today. .

2. UML structure composition

            UML is a modeling language mainly expressed by graphic symbols, and its structure mainly includes the following four parts.

    (1) View : UML view is used to represent the system to be modeled from different angles. A view is an abstract collection composed of many graphics. When building a system model, only by defining multiple views, each view showing a specific aspect of the system, can the complete blueprint of the system be constructed, and the view also uses the modeling language Link to the method and process chosen for development. UML view includes user view, structure view, behavior view, realization view and environment view.

    (2) Diagram : UML diagram is a diagram describing the content of UML views. UML2.0 provides 13 kinds of diagrams, namely Use Case Diagram, Class Diagram, Object Diagram, Package Diagram, Composite Structure Diagram, and State Diagram (State Diagram), Activity Diagram (Activity Diagram), Sequence Diagram (Sequence Diagram), Communication Diagram (Communication Diagram), Timing Diagram (Timimg Diagram), Interaction Diagram (Interaction Diagram), Component Diagram (Component Diagram) and Deployment Diagram (Deployment Diagram), through their mutual combination can provide all views of the system to be modeled.

    (3) Model Element : Model elements refer to some concepts used in UML diagrams. They correspond to common object-oriented concepts, such as classes, objects, attributes, and the relationships between these concepts, such as association relationships, Dependency, generalization, etc. The same model element can be used in multiple different UML diagrams, but no matter which diagram is in, the same model element must maintain the same meaning and have the same symbol.

    (4) General Mechanism : The general mechanism provided by UML provides additional annotations, semantics and other information for model elements. These general mechanisms also provide extension mechanisms that allow users to extend UML, such as defining new modeling Element, expand the definition of the original element, the new special information of Tianjian to expand the rule description of the model element, etc., so as to apply to a specific method or process, organization or user.

3. UML representation of classes and classes

       Among the 13 graphics in UML 2.0, the class diagram is one of the most widely used graphics, which is used to describe the classes included in the system and the relationship between them. Class diagrams help people to simplify the understanding of the system. It is the product of the system analysis and design stage, and it is also an important model basis for system coding.

       1. Class

           In the system, each class has certain responsibilities. Responsibilities refer to what kind of functions the class must complete and what kind of obligations it must undertake. A class can have multiple responsibilities, and a well-designed class usually has one and only one responsibility. When defining the class, the responsibilities of the class are decomposed into the attributes and operations (ie methods) of the class. The attributes of the class are the data responsibilities of the class, and the tiring operations are the behavior responsibilities of the class.

           Class Diagram uses different classes that appear in the system to describe the static structure of the system. It is used to describe the different classes and the relationships between them.

       2. UML diagram of the class

           In UML, classes are represented by rectangles containing class names, attributes, and operations with dividing lines. As shown below:

                                                  

          The figure shows a Person class, which contains the attributes name, age, and address, and the operations work() and eat(). The code corresponding to the figure is as follows:          

public class Person {

    public String name;
    private int age;
    private String address;

    public void work() {

    }

    public String eat() {
        return "rice";
    }
}

          In UML class diagrams, classes generally consist of 3 parts.

        (1) The first part is the class name: each class must have a name, and the class name is a string.

        (2) The second part is tired attributes: attributes refer to the nature of the class, that is, the member variables of the class. A class can usually have any number of attributes, or no attributes.

                 UML stipulates that the attribute representation is as follows:

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

                 among them:

                    ① "Visibility" indicates whether the attribute is visible to elements outside the class, including three types: public, private, and protected. Use "+" and "-" in UML class diagrams. And "#" means. In the Java language, the default package visibility is also added. The package visibility is represented by the symbol "*" in the UML modeling tool.

                    ② "Name" means the attribute name, which is represented by a character string.

                    ③ "Type" indicates the data type of the attribute, which can be a basic data type or a user-defined type.

                    ④ "Default value" is an optional item, that is, the initial value of the attribute.

         (3) The third part is the operation of the class: the operation is the behavior that any instance object of the class can use, and it is the member method of the class.

                   UML stipulates that the operation is expressed as follows:

[ 可见性 ] 名称 ([参数列表]) [ : 返回类型 ]

                  among them:

                      ① The definition of "visibility" is the same as the definition of attribute visibility

                      ② "Name" is the method name or operation name, which is represented by a character string.

                      ③ "Parameter list" means the parameters of the method. Its syntax is similar to the definition of attributes. The number of parameters is arbitrary, and multiple parameters are separated by commas ",".

                      ④ "Return type" is an option, which means the return value type of the method, which depends on the specific programming language. It can be a basic data type, a user-defined type, or an empty type. If it is a construction method, there is no Return type.

        3. Relationship between classes

             In a software system, classes do not exist in isolation. There are various relationships between classes. For different types of relationships, UML provides different representations.

             1. Association

                 Association relationship is the most commonly used relationship between classes, which means that there is a connection between one type of object and another type of object. Such as car and tire teachers and students. In the UML class diagram, a solid line is used to connect the classes corresponding to the associated objects. In Java, the objects of one class are usually used as member variables of another class or appear in the form of attributes of the class. When using the class diagram to represent the association relationship, you can mark the role name on the association line. The two ends of the relationship represent two different roles. Therefore, two role names can be included in an association relationship. The role name is not necessary and can be based on needs. Increase, the purpose is to make the relationship between the classes more clear.

                 For example: between students and teachers, each teacher can teach many students, each student may also have multiple teachers, they are two-way association.

                    

                 The code is as follows:

public class Teacher {

    private String name;
    private List<Student> stus;

    public void teaching(){}
}

public class Student {

    private String name;
    private List<Teacher> teas;

    public void study(){}
}

              2. Aggregation

                  The aggregation relationship represents the relationship between the whole and the part. In the aggregation relationship, the member object is a part of the overall object, but the member object can exist independently from the overall object. In UML, the aggregation relationship is represented by a straight line with a hollow diamond. For example, the car engine is a component of the car, but the car engine can exist independently, so the car and the engine are in an aggregated relationship.

                 The aggregation relationship is shown in the figure:

                       

                 When the code implements the aggregation relationship, the member object is usually used as the construction method, the parameter of the Setter method or the business method is injected into the overall object.

                 code show as below:

public class Car {

    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }
    
    public void setEngine(Engine engine){}
    
}

public class Engine {
    ........
}

               3. Combination relationship

                   The composition relationship also represents the overall and partial relationship between the classes, but in the composition relationship, the overall object can control the life cycle of the member object. Once the overall object does not exist, the member object will not exist, and there is the same relationship between the member object and the overall object. The relationship of life and death. In UML, the combination relationship is represented by a straight line with a solid diamond. For example, the human head and mouth, the mouth is one of the components of the head, and if the head is gone, the mouth is gone, so the head and the mouth are in a combined relationship. UML is shown in the figure:

                      

                   When the code realizes the composition relationship, the member class is usually instantiated directly in the construction method of the overall class. The code is as follows:

public class Head {
    
    private Mouth mouth;
    
    public Head(){
        mouth = new Mouth();
    }
}

public class Mouth{
    ......
}

                  4. Dependencies

                      Dependency is a kind of usage relationship. The change of a specific thing may affect other things that use the thing. The dependency relationship is used when it is necessary to indicate that one thing uses another thing. In general, the dependency relationship is reflected in the method of a certain class using an object of another class as a parameter. In UML, a dotted line with an arrow is used to indicate a dependency relationship, from the dependent party to the dependent party. For example, when the driver drives a car, the Car type object is passed as a parameter in the drive() method of the Driver class, so that the move() method of the car can be called in the drive() method. The drive() method of the driver depends on the car’s The move() method, so the Driver class depends on the Car class. As shown below:

                      

                        In code implementation, dependencies are usually implemented in 3 ways: the first way is to use the object of one class as the parameter of a method in a class; the second way is to use the object of another class as its method in the method of one class. Local variables; the third way is to call a static method of another class in a method of another class.

                   5. Generalization 

                       The generalization relationship is the inheritance relationship, which is used to describe the relationship between the subclass and the parent class. In UML, the generalization relationship is represented by a line with a hollow triangle. For example, both Teacher and Student can be subclasses of Person, and they can both move () and speak say (). UML looks like this:

                           

                         The corresponding code is as follows:

public class Person {

    public String name;
    private int age;

    public void move() {
        ......
    }

    public void say() {
        ......
    }
}

public class Teacher extends Person {

    private String teacherNo;

    public void teach(){
        .......
    }
}

public class Student extends Person{

    private String studentNo;

    public void study(){
        ......
    }
}

                  6. Interface and implementation relationship

                      The Realization relationship is the relationship between the interface and the implementation class. In this relationship, the class implements the interface, and the operations in the class implement all the abstract operations declared in the interface. In UML, the realization relationship between a class and an interface is represented by a dashed line with a hollow triangle. For example, define a vehicle interface Vehicle, which contains an abstract operation move(). The move() operation is implemented in the Ship and Car classes, but the specific details may be different. UML is shown in the following figure:

                       

                       code show as below:

public interface Vehicle {

    void move();
}

public class Ship implements Vehicle {

    @Override
    public void move() {

    }
}

public class Car implements Vehicle{

    @Override
    public void move() {

    }
}

The above is the content I want to share with you today. UML has too many things. Class diagrams are only a small part of the most commonly used ones. Other various diagrams need to be studied and used.

       Thank you for browsing, and welcome to leave a message to exchange and make progress together.

 

Guess you like

Origin blog.csdn.net/zhourui_1021/article/details/100174535