UML class diagram in layman's (I.)

Source: Internet
Author: User
Tags abstract visibility

In the 13 graphs of UML 2.0, class diagrams are one of the most frequently used UML diagrams. Martin Fowler in his book "UML Distilled:a Brief Guide to the Standard object Modeling Language, Third Edition" (UML Essence: A concise reference to the model language South (3rd edition)) There is a paragraph in this: "If someone were to come up to you in a dark alley and say, ' psst, wanna see a UML diagram? '" that diagram W Ould probably be a class diagram. The majority of UML diagrams I See is class Diagrams. " ("If someone comes to you in the dark alley and says to you, ' Hey, want to see a UML diagram. ' Then this picture is probably a class diagram, and most of the UML diagrams I've seen are class diagrams, which shows the importance of class diagrams.

class Diagram is used to describe the classes contained in the system and the relationship between them, to help people simplify the understanding of the system, it is an important product of the system analysis and design phase, but also the system coding and testing important model basis.

1. Class

class encapsulates data and behavior, is an important part of object-oriented, and it is the collective name of a collection of objects with the same properties, operations, and relationships. in the system, each class has a certain responsibility, the responsibility refers to the class to complete what kind of function, to undertake what kind of obligation. A class can have multiple responsibilities, and well-designed classes generally have only one responsibility. When defining a class, the responsibilities of the class are decomposed into the properties and operations of the class (that is, methods). The property of a class is the data responsibility of the class, and the operation of the class is the behavior responsibility of the class. Design classes are the most important part of object-oriented design and the most complex and time-consuming part.

When the software system is running, the class is instantiated as an object, which corresponds to a specific thing and is an instance of the Class (Instance).

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

In the system analysis and design phase, the class can usually be divided into three types, namely entity class, control class and boundary class (boundary Class), and the following three types are briefly described:

(1) entity class: The entity class corresponds to each entity in the system requirement, which usually needs to be stored in a persistent storage body, typically recorded with a database table or file, and the entity class includes both the class that stores and passes the data, and the class that operates the data. The entity class originates from the nouns in the requirement description, such as students, commodities, etc.

(2) control class : The control class is used to embody the execution logic of the application, provide the corresponding business operation, abstract the control class can reduce the coupling degree between the interface and the database. The control class is usually a noun that is transformed by a verb-structure phrase (verbs + nouns), such as adding a commodity to a product that has an added class, registering a user registration class, and so on.

(3) boundary class : The boundary class is used to abstract the interaction objects between the external user and the system, including the interface classes, such as dialog boxes, windows, menus, etc.

In the early stages of object-oriented analysis and design, it is common to identify entity classes first, and to draw initial class diagrams, where class diagrams can also be called domain models, including entity classes and their interrelationships.

2. UML Diagrams for Classes

In UML, a class is represented by a rectangle that contains the class name, attributes, and operations with a separator line , such as defining an employee class that contains the property name, age, and email, and the Operation Modifyinfo (), as shown in the UML class diagram in Figure 1:

Figure 1 UML diagram of the class

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

public class Employee {
	private String name;
	private int age;
	private String Email;
	
	public void Modifyinfo () {
		...
	}
}

In UML class diagrams, classes are generally made up of three 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 the property of the Class (Attributes): The property refers to the nature of the class, that is, the member variable of the class. A class can have any number of attributes, or it can have no properties.

The UML specifies that the attribute is represented as follows:

Visibility name: Type [= default value]

Where: "Visibility" indicates whether the property is visible to elements outside of the class, including three kinds of public, private, and protected (protected), denoted by symbol +,-and # In the class diagram, respectively.       "Name" represents the property name, represented by a string.       A type represents the data type of a property, either a base data type or a user-defined type. The default value is an optional, the initial value of the property.

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

The UML specifies that the operation is represented as follows:

Visibility name (parameter list) [: return type]

Where: the definition of visibility is the same as the visibility definition of the property.       "Name" is the method name, represented by a string.       "Parameter list" means the parameters of the method, whose syntax is similar to the definition of a property, the number of arguments is arbitrary, and multiple parameters are separated by a comma ",". A return type is an option that represents the return value type of a method, depends on a specific programming language, can be a basic data type, can be a user-defined type, can be an empty type (void), and no return type if it is a construction method.

In class Figure 2, the visibility of the operation Method1 is public (+), with an object type of parameter par, the return value is null (void), the visibility of the Operation Method2 is protected (#), no argument, and the return value is string type The visibility of the operation Method3 is private (-), contains two parameters, one of the arguments is of type int, the other is the int[] type, and the return value is type int.

Fig. 2 schematic diagram of Class diagram operation description

Because inner classes are allowed in the Java language, a class diagram with four parts may appear, as shown in Figure 3:

Figure 3 Class diagram containing the inner class

"Author: Liu Wei Http://blog.csdn.net/lovelion"

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.