Design Pattern (4) class diagram one

Source: Internet
Author: User

    • Introduction
    • Class
      • Definition of Class
      • Introduction to Classes
    • Class diagram
      • Definition of class diagram
      • Representation of a class diagram
        • Class and Object representations sample diagram
        • Class Diagram example
          • Java code for the corresponding class
        • Class name
          • Class name Example
        • Property Atttributes
          • Example
        • Operation of the class
          • Example
        • Class representation within
          • Example
    • Conclusion

Introduction

Learn class diagrams, without exception, to learn classes, and the relationships between classes and the drawing of class diagrams.
In general, the static model is depicted by UML class diagram notation.

Definition of Class 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 task that the class holds, namely the class wants to complete what kind of function, must undertake what kind of obligation.

A class can have a variety of responsibilities, well-designed classes generally have only one responsibility (single responsibility principle), when defining classes, the class's responsibilities 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.

When the software system is running, the class is instantiated as an object, and the object corresponds to a specific thing. A class is an abstraction of a set of objects that have the same properties and behave the same behavior, and the object is an instance of the Class (Instance).

Introduction to Classes

Class is the concept of object-oriented programming, and is the basis of object-oriented program.

The essence of a class is a data type , similar to a basic type such as int, char, or a complex data type. Because its nature is the type, not the data, it does not exist in memory, cannot be manipulated directly, and only becomes operable if it is instantiated as an object.

class is the abstraction of a kind of thing with common characteristics in real life . If the type provided in a program corresponds directly to the concept in the application, the program will be easier to understand and easier to modify. A well-chosen set of user-defined classes makes the program more concise. In addition, it makes it easier to conduct various forms of code analysis. In particular, it also makes it possible for the compiler to check the illegal use of objects.

The inside of the class encapsulates a method that is used to manipulate the members of itself. A class is a definition of an object with a behavior (Be-havior) that describes what an object can and does (method), which are programs and procedures that can manipulate the object. It contains information about how an object acts, including its name, method, properties, and events.

The composition of a class includes data members and member functions. Data members correspond to the properties of the class, and the data members of the class are also a data type and do not need to allocate memory. The member function is used to manipulate the properties of the class, which is a unique operation of a class, such as "Students" can "class", and "fruit" is not. The operation of the class and the outside world is called an interface.

Definition of Class Diagram class diagram

Class diagrams use different classes that need to appear within the system to describe the static structure of the system, which contains classes and their relationships, which describe the classes declared within the system, but it does not describe the behavior of the system runtime classes.

A class diagram shows a set of classes, interfaces, collaborations, and relationships between them.

Representation of a class diagram

In UML, a class is represented by a rectangle that has a class name, a property, and an operation split.

Class and Object representations sample diagram

Class Diagram example

Java code for the corresponding class

The corresponding Java code for this class

/*********************************************************************** * Module:Employee.java * AUTHOR:TQW * Purpose:defines the Class Employee ***********************************************************************/Importjava.util.*; Public  class Employee {   PrivateString name;Private intAgePrivateString email; Public Employee() {//Todo:implement} PublicStringGetemail() {returnEmail } Public void Setemail(String Newemail)   {email = newemail; } Public int Getage() {returnAge } Public void Setage(intNewAge) {age = NewAge; } PublicStringGetName() {returnName } Public void SetName(String newName)   {name = NewName; }}
Class name

Each class must have a name, and the class name is a string. For example, order, customer is a valid class name, according to the Java language naming convention, the first letter of each word in the class name needs to be capitalized.

In a UML class diagram, the class name is placed at the top of the box that represents the class.

{Abstract} indicates that the class is an abstract class
Use "..." to denote a class's property representation type
Use one class to point to another class, specifying that bind indicates a boundary class

Class name Example

This figure shows 7 different types of classes

Classes named class are abstract classes
Class named Class2 is a generic class
Boundary classes for generic class classes named CLASS3
Class with class name Class4 as JavaBean type
Class with class name CLASS5 is a class of type annotationtype (annotation)
Class with class name CLASS6 is a class of Enum (enum) type
The class name CLASS7 is a generic class. Can be public, private, or protected type.

Properties (atttributes)

A property is the nature of a class, which is a member variable of a class. A class can have any number of attributes, or it can have no properties.

Representation of defined attributes in UML

Visibility name: Type [= default value]

which

(1) Visibility indicates whether the property is visible to outside elements of the class, including public, private, and protected (protected) three, denoted by the symbol "+", "-", "#" in the class diagram. The Java language adds an in-Package visibility (pack). denoted by the symbol "*" in UML. In order to ensure the encapsulation of data, the visibility of attributes is generally private, which is used by the public getter method and setter method for the outside world.

(2) The name represents the property name, represented by a string, followed by the Java language Naming convention, the first letter of a property name is generally lowercase, followed by the first letter of each word capitalized.

(3) A type represents a data type that defines a property, either a basic data type or a user-defined type.

(4) The default value is an optional, that is, the initial value of the property.

Example

The example of this figure, the class name is student, has four attributes;

The Name property has a private (-), type string, and no default value;
The Age property visibility is public (+), the type is integer (int), and the default value is 18;
The Address property has a visibility of protected (#), a type of string type (string), and no default value;
e-mail attribute visibility is package (*), type is string type (string), no default value;

Operation of the class

An action is a behavior that can be used by any instance object of a class, and the action is a member method of the class.

UML Specifies how operations are represented

Visibility name (parameter list): return type

which

(1) The definition of visibility is the same as the property definition.

(2) The name is the operation name or method name, in a string representation, according to the Java language naming convention, the first letter of the method name is generally lowercase, followed by the first letter of each word capitalized.

(3) The parameter list represents the parameters of the operation, whose syntax is the same as the representation of the property, the number of arguments is arbitrary, and multiple parameters are separated by a comma ",".

(4) The return type represents the return value type of the operation, depends on the specific programming language, can be a basic data type, can be a user-defined type, can also be a null type (void), and if it is a construction method, no return value type.

Example

This diagram class is named demo, which has four methods of operation:

The visibility of the action method is private (-), with a parameter par of type object, and a return value of type int;
Operation Method1 Visibility is public (+), no parameters, return value is string type;
Operation METHOD2 Visibility is package (*), contains two parameters, one argument is of type int, the other is int[] type, and the return value is Boolean type;
Operation Method3 Visibility is protect (#), no parameters, return value is float type;

Class representation within

The Java language allows internal classes to appear, so a class diagram with four parts may appear. A Java can have more than one inner class.

Example

This is a class named container:

There is the attribute data, the visibility is private (-), the data type is int;
There is an action method, no parameter, the visibility is public (+), the return value type is int;
There are two internal classes, member and Member2;

Conclusion

This section describes the drawing of class diagrams, and the next section describes the types of classes and the relationships between classes ~ ~

Design Pattern (4) class diagram one

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.