1 UML Basics

Source: Internet
Author: User

In the process of learning the design pattern, it is found that the relevant authors will use UML class diagrams to represent the whole context of a pattern, which is really intuitive and clear, which can embody both macroscopic thinking and realizing details. It's really a wonderful tool. It is necessary to have a general understanding of UML before beginning to formally learn the design pattern. Then, hopefully, you'll unlock more of your UML skills in the future, such as modeling. Haha, a little excited about it.

UML Full name unfied Modeling Language (Unified \ Standard modeling language), designed to provide a visual, model-based tool for software development. It can be seen that UML is both a modeling tool and an "exchange language".

First, the basic elements of UML class diagram

Structure of Class 1

UML represents a class with a rectangular box containing three layers of "lattice",

  

The top layer is the name of the class, the fields and properties of the middle class, and the behavior and methods of the class.

If it is an abstract class, the class name is identified in italics.

2 access Modifiers

Public, private, protected are represented by +-# respectively.

As for the internal, protected internal modifiers in C #, there is no corresponding symbol. (UML is a generic markup language, and the latter two modifiers are unique to C #)

3 Interface Representation

There are two methods of rectangle notation and lollipop notation (from "Big Talk design Mode")

  

II. Mutual relations

In UML class diagrams, the relationship between classes and classes, interfaces and classes is generalized (Generalize), implemented (realization), dependent (Dependency), associative (association), aggregated (Aggregation), combined ( Composition) Six kinds. The interaction of these representations is enhanced in turn.

1 generalization (Generalize)

Generalization is the ability of subclasses, sub-interfaces to inherit the parent class, the parent interface, and to expand their new functionality, which is a is-a (general and special) relationship. Cats inherit the animals, then it can be said that cats are the generalization of animals, cat is a animal.

UML uses a hollow triangular arrow + solid line to represent generalization or inheritance.

  

2 Implementation (realization)

That is, the class implements the functionality of the interface.

For the rectangle notation, the hollow triangular arrows and dashed lines are used, and for the lollipop notation, the lollipop is inserted directly on the class that implements the interface (the Jude Lollipop is not very good-looking).

  

3 Dependence (Dependency)

A class relies on the definition of another class. For example, people need to use mobile phone calls, then people rely on mobile phones. Dependencies are always one-way. Dependence is contingency, temporary, and very weak. Depending on the specific code level, the expression is (Class A depends on Class B):

Class B is used as a parameter by Class A;

Class B exists in the method of Class A in the form of a local variable;

Class A calls a static method of Class B.

The UML uses a simple arrow with a dashed line to indicate dependency:

  

4 Association (Association)

A class needs to know the state (properties, methods) of another class. Correlation is a strong dependency between a class and a class or interface, which is long-term compared to a dependency, and the relationship between the two is generally equal. At the code level (a association B), B comes out in the form of a class attribute in a, or a refers to a global variable of type B.

In UML, a one-way association is represented by a simple solid arrow, with a double arrow or a non-arrowhead representation of a bidirectional association. However, in order to reduce coupling, bidirectional correlation is not recommended.

A cardinality representation can be added to the head-end of the associated arrow to indicate several instances.

  

5 Aggregation (Aggregation)

A special case of association relationship is a strong association and a relationship between the whole and the individual (HAS-A). The two classes of a common association are at the same level, but the two classes of the aggregation relationship are at different levels, such as companies and employees. It is also a weak possession (has-a) relationship. Since the whole and the individual are separable, they have their own independent life cycle. An individual can belong to more than one whole, or it can be shared by multiple entities. With regard to the implementation at the code level, there is no specific standard, and the most reliable method of identification is through semantics. The following is a code that implements aggregation:

public class Company

{

List<employee> list;

}

  

The use of hollow diamond solid lines in UML is used to represent

  

6 combination \ combination (composition)

The combination is also a special case of association, a stronger association than aggregation, and the whole is consistent with the life cycle of the part (CONTAINS-A). Like the arms and the human body. The following is a code implementation:

public class Body

{

private arm arm;

Public Body ()

{

Arm=new Arm ();

}

}

  

In UML, a solid diamond line is used to represent

  

Iii. Summary

Six relationship programs from low to High: generalization < Implementation < Dependency < Association < aggregation < combination

Generalization is a is-a relationship, which is related to has-a relationship, in which aggregation and composition are the special cases of association, and the combination represents the closest relationship, and it is a contains-a relationship.

The difference between aggregation and composition:

1) is mainly reflected in the relationship between the members of the life cycle is the same;

2) The classes that are aggregated can also continue to be aggregated by other classes, but the grouped classes no longer belong to other classes.

Differences between associations and aggregations:

The main difference is the abstraction hierarchy, which is associated at the same level of abstraction and aggregated at different levels.

  

About UML, now learn this fur bar.

  

  

  

  

1 UML Basics

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.