Easy introduction to UML-classes and objects

Source: Internet
Author: User

Class is an abstraction of the same thing in nature. The history of the development of human software development technology is also the history of the origin of things. The development technology and terminology are getting closer and closer to the world, "object-oriented" and "class" are such products.

  Class 1 Graph

In UML, a class diagram shows a group of classes, interfaces, collaboration, and relationships between them. In the static mechanism of UML, class diagrams are a key point. They are not only concerned by designers, but also concerned by implementation personnel. modeling tools also generate code (positive) projects based on class diagrams. Therefore, class diagrams play an important role in various UML diagrams.

  Class

In a class chart, the class is represented by a rectangle. Its Attributes and operations are listed in cells. If you do not need to express details, the cells can be omitted. A class may appear in several figures. The attributes and operations of the same class are only listed in one graph, which can be omitted in other graphs. Figure 1 shows the Student class.

Figure 1 class Representation

  Inter-class relationship

In the class diagram, apart from describing the names, attributes, and operations of individual classes, we also need to describe the relationships between classes because no classes exist independently, they usually need to work with other classes to create more semantics than working independently. In the UML class diagram, the relationship is represented by a line between the class boxes, and different modifiers between the line and the line end indicate different relationships. The relationships between classes include inheritance (generalization), Association, aggregation, and combination.

(1) Inheritance: A class (called a subclass) inherits the functions of another class (called a base class) and adds its own new functions, inheritance is the most common relationship between classes. The representation inherited from the class diagram is to pull a closed, single key header (or triangle) solid line from the subclass to the base class. For example, Figure 2 shows the inheritance relationship between students and a_member members of a school group.


Figure 2 Inheritance

Class inheritance is displayed in C ++ as follows:

Class B {}
Class A: public B {}

(2) Association: A Semantic Link between model elements and a weak link between classes. The association can be unidirectional or bidirectional. You can add an association name to the Association to describe the role of the Association. Classes at both ends of the association can also be associated with a role. The role can have multiple features, indicating how many objects can be involved in the association. You can use the association class to further describe the associated attributes, operations, and other information. The join class uses a dotted line to connect to the join class. Some constraints can be added to the Association to enhance the meaning of the association.
 
The Association is displayed in C ++ as follows:

Class {...}
Class B {...}
A: Function1 (B & B) // or A: Function1 (B B) // or A: Function1 (B * B)

That is, a class acts as a parameter of another class method.

(3) aggregation: refers to the relationship between the whole and the part. After defining a whole class, we usually analyze the composition structure of the whole class. In this way, an aggregation relationship is formed between the overall class and the composition class. For example, a car includes tires and engines. The requirement descriptions include "include", "Composition", and "... Partial "and other words often mean the aggregation relationship.


Figure 3 Aggregation

(4) combination: it also indicates the relationship between the whole and the part of the class, but the partial and the whole of the combination have a unified survival period. If the overall object does not exist, some objects do not exist. Some objects have a symbiotic relationship with the entire object.

The difference between aggregation and combination is that the aggregation relationship is "has-a", and the combination relationship is "contains-a". The aggregation relationship indicates that the relationship between the whole and the part is relatively weak, the combination is relatively strong; the Objects representing some things in the aggregation relationship are irrelevant to the lifetime of the objects representing the aggregation things. Once an aggregation object is deleted, the Objects representing some things are not necessarily deleted. Once a composite object is deleted in the composite, the object representing some things is also deleted.

Let's use simple examples to illustrate the differences between clustering and combination. "The country is broken," the country is destroyed, and the family is lost. "The country" and "home" are clearly a combination. On the contrary, the computer and its peripherals are in an aggregation relationship, because the relationship between them is relatively loose, the computer is gone, the peripherals can also exist independently, and can be connected to other computers. In the aggregation relationship, some of them can exist independently of aggregation, and some ownership can be shared by several aggregates. For example, printers can be shared by colleagues in the office.

In C ++, aggregation can be expressed:

Class {...}
Class B {A * ;.....}

Class B contains the pointer of Class;

The combination can be expressed:

Class {...}
Class B {A ;...}

Class B contains objects of Class.

Accurate UML class diagrams distinguish aggregation and combination with hollow and solid diamond.

Figure 4 combination


Figure 5 bank management system diagram

Figure 3.5 shows a class chart of the bank management system. You can see it at the first glance:

The bank class aggregates the Account Database (AccountSet), customer database (mermerset), and employee database (EmployeeSet );

The account library contains n accounts, the customer library contains n customers, and the employee library contains n employees );

Both the employee class and the customer class inherit from the person class, and the employee class has another special employee, namely the employee manager class (employeeAdmin );

There is an association between the account class and the customer class;

Next, we will show the steps for creating a class chart:

(1) Research and analysis of problem domains to determine system requirements;

Class recognition is a work that requires a lot of skills. Some skills for searching classes include the noun recognition method, determining classes based on case descriptions, and using CRC analysis; the class in the analysis system is assisted by the division of the boundary class, control class, and entity class. The class is determined based on the design pattern. The class is obtained by analyzing the domain or using the existing domain analysis results; use the steps for finding classes in analysis and design in RUP.

(2) determine the category, clarify the meaning and responsibilities of the class, and determine the attributes and operations;

(3) determine the relationship between classes.

  2 object graph

An object graph shows the relationship between objects at a certain time. It is a class graph change. An object graph can be seen as an example of a class graph (example ), the object graph indicates the object instance of the class rather than the real class. Because the object has a lifecycle, the object graph can only exist in a certain period of time in the system.

There is no new notation in the object graph (except the name of the object must be underlined). Like the notation in the class graph, it can be considered that, only an object without a class graph is an "object graph ".


Figure 6 object Diagram

In the object graph, the object name can have three forms:

(1) Object Name: Class Name

(2): Class Name

(3) Object Name

In Figure 6, there are 1st types, namely the "Object Name: Class Name" format.

Essentially, an object graph is rarely used (it contains a small amount of information and is only used to present the value of an object at a certain time, which is hardly helpful for software design ), it is widely used without class diagrams. We can compare the relationship between a class chart and an object chart as follows:

"Verb + noun" constitutes "dynamic noun". For example, the verb "love" + the noun "country" constitutes the Dynamic Term "Patriotic ".

We can see that the amount of information in a noun is very large and abstract, while the "Patriotic" information as an example of a noun is very small and very specific.

Noun-> class chart

Patriotic--> Object chart

The "term" is regarded as a class chart, and the "Patriotic" is regarded as an object chart. Obviously, an object chart is just an example of a class chart.

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.