Understand UML diagrams for design patterns in 10 minutes

single-image

UML diagrams are very often used in the context of design patterns and it may turn out that without knowing them it will be difficult for you to implement a pattern.

Why use UML Diagrams

UML Diagrams are used when presenting design patterns. If you are not fluent in using UML you will need some experience. As you learn, everything will become clearer and you will be able to implement the patterns directly from the diagrams. In some less typed languages ​​like PHP this may be more difficult, so you will need to base on examples.

Class diagrams

The class diagrams in the patterns show the relationship and communication between participants (classes and interfaces). Interfaces are classic object-oriented interfaces, but the concept can also be applied to abstract classes and it doesn’t matter how you approach it.

Client class

Some diagrams show the client like other classes, others show grayed out and others do not show him at all. You must remember that the Client class is something that uses the pattern directly – it can be a class, an API client or even another Design Pattern and does not have to be called “Client”

Factory Method Diagram

The factory method allows the creation of class instances through the factory – thanks to this the creation process takes place outside the method that performs the order. Without the class diagram, it would not be possible to clearly define how the client should perform the request, and the remaining relationships between participants would be impossible to determine.

Thanks to the class diagram that you can quickly check the relationships between the members of the pattern.

Membership symbol

The square symbols indicate a member of the design pattern. Interfaces and abstract classes are in italics. The names of key methods are located under the class name.

Rember that “interface” can be abstract class and interface as well

Because in the diagram above we see concrete method AnOperation() in the interface, we are sure that this is not an interface, but an extended abstract class.

Conrete class with extension of Creator

Relation note

Relationships between objects and their meanings may vary depending on the UML version. The above drawing will present those described for the first time in the book “Design Patters: Elements of Reusuable Object-Orientet Software”, in which you can find a more detailed description.

Association relationship

Association is the simplest and most-used relationship. It is a relationship in which one participant stores references to another participant.

From the diagram above, the simplest association relationship can be seen between Class A and Interface.

In the code it will look like this:

<?php

class ClassA
{
    private Interface $interface;

    public function process() :void
    {
        $this->interface = new Interface;
    }
}

Aggregation relationship

The first creators of UML diagrams claim that no set of codes can demonstrate aggregation relationships, so it’s important to remember that an aggregation relationship is similar to an association relationship, but stronger. As they say, aggregation means that the aggregate object and its owner have the same life cycle, where one is dependent on the other.

Let’s take a look at the relationship between ClassE and ClassF and see how the implementation in PHP could look.

<?php

class ClassE
{
    private ClassF $classF;

    public function __construct()
    {
        $this->classF = new ClassF
    }

    public function process() :void
    {
        // ..
    }
}

Remember that aggregation is the most difficult relationship to explain and understand.

Implementation and inheritance relations

The original symbol uses the same symbol that you can see in the figure above to represent the implementation and extend.

Single extend or implementation are marked with single triangles, while multiple inheritance is marked with a triangle connected on the common part of lines running to specific classes.

Creation relationship

When one object creates an instance of another in a design pattern, it is illustrated by a dashed arrow pointing towards the object being created. You will find such a relationship in all creating patterns, such as Method Factory or Abstract Factory.

When considering association relationships between classes in PHP, in some cases it may be necessary to create an instance of an object to store a relationship of another class or interface. Such an instance is created to create a flagship to store references, not to use the object or return it in the method for other classes. In this case, do not use the broken arrow in PHP. It is an association or aggregation.

Multiple relations

Sometimes you encounter a class diagram in which the association or aggregation arrows will appear with a circle.

This means that the class operates on more than one object of a given type, and it is not specified how these references are stored.

In this case, use multiple relationships.


Please leave me comment if you have any question. If you liked this post, go to my instagram and follow me to stay up to date!

You may like