Accumulate Kuibu, Poly stream------about UML class diagram

Source: Internet
Author: User
Tags dashed line

The existence of UML

Class diagram is a UML diagram with high frequency, which is used to describe the classes contained in the system and the relationship between them, to help people to simplify the understanding of the system, is also an important product of the system analysis and design phase, but also the important type of system coding and testing basis.


How to express UML


Its presentation method is also relatively simple, divided into three layers, the first layer is the class name, the second layer is the attribute, the third layer is the method.

The "+" used in properties and methods represents public, "-" for Private, "#" for protected, and for attributes: the permission modifier, the property name (method name), and then the data type (or return value), all of which are basic syntax, both obvious and simple.

There are, of course, representations of interfaces and abstract classes:

Used to describe the interface;

Used to describe abstract methods;

UML describes the relationship between class diagrams

There are four kinds of relationship between class diagram: Generalization relation, realization relation, dependency relation, association relation, and association relation is divided into common relation, aggregation relation and synthetic relation. In fact, these relationships are easy to understand, but more often than not, we need to be aware of how to translate into Java code:

Generalization RelationshipsThe generalization relationship is essentially our inheritance, class inheritance and another class, this code should be better, we just need to remember the UML representation method is good

(the pictures are reproduced from the internet freely)It is important to note that the direction of the integration, the Hollow arrow plus implementation of the expression of inheritance, the arrow pointing to whom the inheritance of who; As for the Java relational code, the public class Tiger extends Animal () {}; Implementing Relationshipsthis is to do with the interface, the description implementation interface, this we also come to the simple note:
(Pictures are reproduced from the Internet)as a comparison, similar to inheritance is the hollow arrow, and the inheritance is different from the dashed line is realized; The Java code here is the public class Penbrush implents Ibrush () {}; Dependent relationshipsThe dependency is placed in the third because it is the lightest relationship between classes and classes, it can be unidirectional or bidirectional, but we usually use one-way to avoid bidirectional.
(Pictures are reproduced from the Internet)
dependency interpretation is slightly more problematic than inheritance and implementation, because inheritance and implementation we often use, and relatively single, with a clear hierarchy, and dependency is not, just as the figure of people rely on the computer to do certain things, like in "Big talk design mode" in the animal relies on air and water, Air and water are separate things from animals, just as the computer is separate from the human, it is independent, and humans need computers in computing or other actions, just as the animal's breath needs air, water needs it, and its behavior may use another independent existence, And if you do not do this, you may not need to use this existence, like a dead animal, has no water and air, but it is still an animal, this class will not change. It may be a bit of a hassle to explain, but the way to say it is simple, a dashed line with a solid arrow is enough;As for the code implementation method, the specific situation needs to be treated concretely. generally speaking, it can usually be expressed as:①: The class that is directed, in the class that points to it, is the parameter of a method, which is the most common way;
public class Person{public void count (computer computer) {}}
②: You can also refer to the class, in the class that points to it, as a local variable in a method;
public class Person{public void count () {Computer computer=new computer ();}}
③: Another way is to have a static method in the class being pointed to, and the class that points to it calls its static method;
public class computer () {public       static void Test () {       }}public class person{computer.test ();}
Association relationship: The last is our relationship, saying that it is divided into three states: Ordinary Association, aggregation and synthesis, their greatest common is the relationship between, and the way the code appears to be pointed to the class as a pointer to the member variables appear, and we look at how they are different? General Association Relationship
(Pictures are reproduced from the Internet)
This is the most common correlation, and the code is easy to write:
public class customer{  private address address;}
And the corresponding also has self-correlation and bidirectional association, and these are the corresponding class into member variables can be, self-related to their own, two-way association made of each other. Aggregation relationship Relationship It's like an old man telling a story, to know how many years it will take, the background of that era might make you understand more deeply. But the aggregation relationship is not only this, not only that the times caused this, but the protagonist may be in an organization, the organization of the common people, he is the organization of people, organizations are composed of his such people.
(Pictures are reproduced from the Internet)
Its description is more a diamond than a simple association, and the diamond's class points to the other. It's code-writing, which is a lot more troublesome than a normal association:
public class car{   private engine engine, public   Car (engine engine) {       } public  void Setengine (engine Engine) {  }}
It will be directed not only to the object as a member variable, but also to the member object as a constructor and as a parameter to the set method or other business method.
Synthetic relationships, and others call it combinatorial relationships, it's different from aggregation because the associated objects are essentially together, like limbs for mobs, like numbers for calculations.
(Pictures are reproduced from the Internet)
Given its homology, the diamond has become solid, and the corresponding program has changed:
public class head{   private Mouth Mouth;   Public Head () {       mouth=new mouth ();   }}
It is a direct instantiation of a member in the overall construction method.


Accumulate Kuibu, Poly stream------about UML class diagram

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.