UML class diagrams that design patterns have to know

Keywords: PHP

Preface

It's not easy to code. I hope you can give me more valuable opinions. When we want to explain a problem to others, the most intuitive way is to explain it in a graphic way, rather than in boring words. After all, when we were children, we were more touched by pictures, while we were influenced by words because of some cognitive problems. The UML class diagram in the design pattern is a way to let you explain the design concept to others directly in the form of class diagram instead of boring code and words.

 

In order to facilitate your understanding, I will give you the corresponding class diagram and the corresponding code implementation, so that you have a preliminary concept, so that you can learn the boring things of design pattern.

 

We have several common relationships: Generalization, Realization, Association, Aggregation, Composition and Dependency.

Inherited class diagram

Inheritance is the most common way in OOP, which can save us a lot of development time. Inheritance can also be described in other words: generalization.

It is a special dependency.

Inheritance refers to the ability that a subclass inherits another function and can increase its own new function.

Representation method:

Inheritance is represented by a white arrow + solid line.

Example:

 

 

 

<?php
declare(strict_types=1);

namespace Neilyoz\Example;

abstract class BaseExample {
    public abstract function method01();
    public abstract function method02();
}

class ExtendsExample extends BaseExample
{
    public function method01()
    {
        // TODO: Implement method01() method.
    }

    public function method02()
    {
        // TODO: Implement method02() method.
    }

    public function method03()
    {

    }
}

Implemented type diagram

Implementation represents the function that a class implements an interface (multiple interfaces can be implemented).

Representation method:

Use white triangle + dotted line

Example:

We've all used cameras. Some of them can only take pictures, but some of them, such as Polaroid, can take pictures and print them.

 

 

 

 

Code implementation:

<?php

namespace Neilyoz\Example;

interface Camera
{
    public function photograph();
}

interface Printer
{
    public function printer();
}

class Polaroid implements Camera, Printer
{
    public function photograph()
    {
        echo "Photograph" . PHP_EOL;
    }

    public function printer()
    {
        echo "Develop films" . PHP_EOL;
    }
}

Dependent class diagram

Introduction:

For two relatively independent objects, when one object is responsible for constructing the instance of another object or depends on the service of another object, the dependency relationship between the two objects is mainly embodied.

Use the dependent class as the parameter to pass into the corresponding method.

Representation method:

Dependencies are indicated by dotted arrows

Example:

People can't live without air and water, right? We depend on these two things to survive. We can use class diagram to describe it.

 

 

 

Code instance:

<?php
declare(strict_types=1);

namespace Neilyoz\Example;

class Oxygen
{
}

class Water
{
}

class Person
{
    public function metabolism(Oxygen $oxygen, Water $water)
    {
        // Metabolism with oxygen water
    }
}

 

Associated class diagram

For two relatively independent objects, when there is a fixed correspondence between an instance of one object and some specific instances of another object, the two objects are associated.

Representation method:

The relationship is represented by an implementation arrow.

Example:

The maturity of our crops is related to weather and climate. When one class needs to know another class, we can use correlation.

 

 

 

<?php
declare(strict_types=1);

class Climatic
{
    public function getCondition()
    {
        return "Autumn";
    }
}

class Cropper
{
    private Climatic $climaticConditions;

    public function __construct(Climatic $climatic)
    {
        $this->climaticConditions = $climatic;
    }

    public function mature()
    {
        if ($this->climaticConditions->getCondition() === "Autumn") {
            echo "Mature" . PHP_EOL;
            return;
        }

        echo "Wait a minute." . PHP_EOL;
    }
}

Aggregate class diagram

It represents A kind of weak "ownership" relation, namely has-a relation, which embodies that A object can contain B object, but B object is not A part of A object (dispensable). Two objects have their own lifecycles.

Representation method:

The aggregation relationship is represented by a hollow diamond + a solid line arrow

Example:

Example:

We are all human beings. We have to mix circles if we are human beings. The life cycle of human beings is different from that of circles. Some people live for more than 100 years, and some circles will be dissolved soon, but the dissolution of circles will not affect human life.

 

 

 

<?php
declare(strict_types=1);

namespace Neilyoz\Example;

class Person
{
    private float $money;

    public function collectMoney(float $money)
    {
        $this->money += $money;
    }
}

class Group
{
    private array $members = [];

    public function addMember(Person $person)
    {
        $this->members[] = $person;
    }

    // Cents
    public function cents()
    {
        array_map(function ($person) {
            $person->collectMoney(rand(0, 100));
        }, $this->members);
    }
}

Combined class diagram

Combination is a strong 'ownership' relationship, a contains-a relationship, which embodies the strict relationship between parts and the whole, and the life cycle of parts and the whole is the same.

Representation method:

The combination relationship is represented by a solid diamond + solid line arrow, and the numbers at both ends of the line can also be used to indicate that there are several instances at one end.

Example:

 

 

 

A man must have a big head and a small head to be complete. This shows our strong ownership relationship.

<?php
declare(strict_types=1);

class BigHead
{
}

class SmallHead
{

}

class Man
{
    private BigHead $bigHead;
    private SmallHead $smallHead;

    public function __construct()
    {
        // In the code, here is our fixed and irreplaceable
        $this->bigHead = new BigHead();
        $this->smallHead = new SmallHead();
    }
}

summary

In fact, the form of UML class diagram is very complex, but with these basic composition, we are just building blocks. Peace!!!

For more information, please visit:

Tencent T3-T4 standard boutique PHP architect course catalog, as long as you read it to ensure a higher salary (continuous update)

Posted by co.ador on Sat, 18 Apr 2020 00:43:44 -0700