Design patterns have to know UML class diagrams

Time:2022-5-11

preface

Codewords are not easy. I hope you can give us more valuable opinions.
When we want to explain a problem to others, the most intuitive way is to explain it in a graphical way, rather than boring words. After all, when we were children, pictures can bring us more touch, while words can lead to some deviations in our understanding ability because of some cognitive problems. The UML class diagram in the design pattern is a way that allows you to explain the design concept to others in the form of class diagram instead of boring code and text.

In order to facilitate your understanding, I will give the corresponding class diagram and corresponding code implementation, so that you can have a preliminary concept and facilitate you to learn the boring thing 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 saves us a lot of development time. There are other ways to describe inheritance: generalization. It is a special dependency.

Inheritance refers to the ability of a subclass to inherit the functions of another class and add its own new functions.

Representation:

Inheritance is represented by a hollow arrow + solid line.

Example:

Design patterns have to know UML class diagrams

<?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()
    {

    }
}

Implementation type diagram

Implementation represents the function of a class to implement interface interface (multiple interfaces can be implemented).

Representation:

Represented by hollow triangle + dotted line

Example:

We have all used cameras. Some cameras can only take pictures, but some cameras, such as Polaroid, can not only take pictures, but also print photos.

Design patterns have to know UML class diagrams
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 "take a picture" PHP_ EOL;
    }

    public function printer()
    {
        Echo "develop photos" PHP_ EOL;
    }
}

Dependent class diagram

Introduction:

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

Use the dependent class as a parameter and pass it into the corresponding method.

Representation:

DependencyDashed arrowexpress

Example:

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

Design patterns have to know UML class diagrams

Code example:

<?php
declare(strict_types=1);

namespace Neilyoz\Example;

class Oxygen
{
}

class Water
{
}

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

Associated class diagram

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

Representation:

The association relationship is represented by the implementation arrow.

Example:

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

Design patterns have to know UML class diagrams

<?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;
    }
}

Aggregated class diagram

It represents a weak “ownership” relationship, i.e. has-a relationship, which reflects that object a can contain object B, but object B is not a part of object a (optional). Two objects have their own lifecycles.

Representation:

For aggregation relationHollow diamond + solid arrowexpress

Example:

We are all people. If we are people, we have to mix the circle. The life cycle of people is different from that of the circle. Some people live more than 100 years old, and some circles will dissolve soon, but the dissolution of the circle does not affect people’s life.

Design patterns have to know UML class diagrams

<?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;
    }

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

Combined class diagram

Combination is a strong “ownership” relationship, a kind of contains-a relationship, which embodies the strict relationship between part and whole, and the life cycle of part and whole is the same.

Representation:

Combination relationSolid diamond + solid arrowThe numbers at both ends of the line can also be used to indicate that there are several instances at one end.

Example:

Design patterns have to know UML class diagrams

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

<?php
declare(strict_types=1);

class BigHead
{
}

class SmallHead
{

}

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

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

summary

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

Original address

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Neilyoz fish don’t wave