What is a UML class diagram and what does it do?

UML 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 stage, but also an important model basis for system coding and testing. So I suggest you can learn a wave of UML class diagram, for their own, you can clarify the relationship between classes and classes in the system they design. For others, just look at your UML class diagram to understand your design ideas.

Representation of UML class diagram visibility symbols

Visibility:

Public “+”

Protected #

Private (private)

There are six types of relationships between classes: inheritance, implementation, composition, aggregation, association, and dependency, all of which are represented by different arrows

Inheritance relationships

Inheritance: Inheritance refers to the creation of a new derived class that inherits data and functions from one or more previously defined classes. New data and functions can be redefined or added to establish a hierarchy or hierarchy of classes.


      
class A
{
    public $param1;
    protected $param2;
    private $param3;
    
    public function method1(a): string
    {
        return "test code"; }}class B extends A
{
     public $test;
     public function method2(int $num): int
     {
        return$num; }}Copy the code

Realize the relationship between

Interface Implementation: Implementation relationship (Implementation), mainly used to specify the interface and Implementation class relationship.


      

interface animal
{
    public function move(int $distance);
    public function jump(int $height);
}

class cat implements animal
{
    public function move(int $distance)
    {
        return $distance;

    }
    public function jump(int $height)
    {
         return$distance; }}class cow implements animal
{
    public function move(int $distance)
    {
        return $distance;
    }
    
    public function jump(int $height)
    {
         return$distance; }}Copy the code

Combination relationship

Composition: The relationship between the whole and the parts, but the whole and the parts cannot be separated.

The combinatorial relationship represents the relationship between the whole and the parts between classes, and the whole and the parts have the same lifetime. Once the whole object does not exist, part of the object will not exist, it is the relationship of life and death.

For example, a person consists of a head and a body, which are inseparable and co-exist.


      

class Head
{
    public function eye(a)
    {
        echo "this is eye"; }}class Body
{
    public function tummy(a)
    {
        echo "this is tummy"; }}class Human
{
    private $head;
    private $body;

    public function setHead(Head $head)
    {
        $this->head = $head;
    }

    public function setBody(Body $body)
    {
        $this->body = $body;
    }

    public function show(a)
    {
        $this->head->eye();
        echo "<br>";
        $this->body->tummy(); }}Copy the code

The aggregation relationship

Aggregation: The relationship between the whole and the parts. The whole and the parts can be separated.

Aggregation relationships also represent the whole and part relationships between classes. Member objects are part of the whole object, but they can exist independently of the whole object.

For example: car = engine + wheels. When a car is scrapped, the engine and wheels are not necessarily scrapped and may end up on the second-hand market.

The relationship between aggregation and composition is a little tricky


      

class Engine
{
    public function getEngine(a)
    {
        echo "this is Engine"; }}class Wheel
{
    public function getWheel(a)
    {
        echo "this is getWheel"; }}class Car
{
    private $engine;
    private $wheel;

    public function setEngine(Engine $engine)
    {
        $this->engine = $engine;
    }

    public function setWheel(Wheel $wheel)
    {
        $this->wheel = $wheel;
    }

    public function show(a)
    {
        $this->engine->getEngine();
        echo "<br>";
        $this->wheel->getWheel(); }}Copy the code

correlation

Association is the most common relationship between classes. It is a structural relationship used to indicate that one class of objects is related to another class of objects, such as cars and tires, masters and apprentices, classes and students, and so on.

(1) Bidirectional association

By default, the association is bidirectional. For example, a Customer buys a Product and owns it. Conversely, a Product sold always has a Customer associated with it. Therefore, there is a bidirectional association between the Customer class and the Product class.


      

class Customer
{
    public $product = [];
}

class Product
{
    public $customer;
}
Copy the code

(2) One-way association

Class associations can also be one-way, with one-way associations represented by solid lines with arrows. For example, if a Customer owns an Address, the Customer class has a one-way association with the Address class.


      

class Customer
{
    public $address = [];
}

class Address
{}Copy the code

(3) the correlation

In the system, there may be some classes whose attribute object type is the class itself. This special association is called self-association. For example, the singleton mode.


      
final class Singleton
{
    / * * *@var Singleton
    */
    private static $instance;

    /** * Get the instance by lazy loading (created when first used) */
    public static function getInstance(a): Singleton
    {
        if (null= = =static::$instance) {
            static::$instance = new static(a); }return static::$instance;
    }

    /** * Calls from outside are not allowed to prevent the creation of multiple instances * to use singletons, instance */ must be obtained through the Singleton::getInstance() method
    private function __construct(a)
    {}/** * prevents instances from being cloned (this creates a copy of the instance) */
    private function __clone(a)
    {}/** * prevents deserialization (this will create a copy of it) */
    private function __wakeup(a)
    {}}Copy the code

(4) Multiple correlation

Multiplicity relation, also called Multiplicity relation, refers to the corresponding relation of two related objects in quantity. In UML, multiplicity between objects can be directly represented by a number or a range of numbers on an association line.

There can be a variety of multiplicity association relations between objects. The common representation of multiplicity is shown in Table 1:

Said method Multiplicity specification
1.. 1 An object representing another class is related to only one object of that class
0.. * An object representing another class is related to zero or more objects of that class
1.. * An object representing another class is related to one or more objects of that class
0.. 1 Represents an object of another class that has no or only relationship to an object of that class
m.. n An object representing another class is associated with at least m and at most N objects of that class (m≤n)

For example, a Form can have zero or more buttons, but a Button can belong to only one interface. Therefore, a Form object can be associated with zero or more Button objects, but a Button object can be associated with only one Form object.


      

class Form
{
    private $button = [];

}

class Button
{}Copy the code

dependencies

Dependence: It is said that class B depends on class A if the change of class A causes the change of class B.

In most cases, dependencies occur when a method of one class takes an object of another class as an argument. A dependency is a “use” relationship, where changes to a particular thing may affect other things that use that thing, and dependencies are used when there is a need to indicate that one thing uses another.

For example: When a Driver drives a Car, the object Car of Car type is passed as a parameter in the drive() method of the Driver class, so that the move() method of Car can be called in the drive() method, and the drive() method of the Driver depends on the move() method of the Car. Therefore, the class Driver depends on the class Car.


      

class Driver
{
    public function drive(Car $car)
    { $car->move(); }}class Car
{
    public function move(a)
    {}}Copy the code

conclusion

Finally, I recommend an online drawing tool:

processon.com