Many programmers, faced with a difficult task, neglect the design stage, referring to the fact that design is a waste of time, and in this case it will only bother me.
Often this statement turns out to be true if the task is really small and the qualifications of the programmer are sufficient to determine the most optimal solution.
Programmers who do not use UML are divided into several groups:
')
- I'll start writing code, and in the process I will understand that yes how;
- I read forums, habr, medium, stack overflow, book, writing on the walls, signs over ...;
- asking around from my colleagues, maybe someone knows how to solve a similar problem;
- I'll start to draw the squares and show schematically what vision of the task was formed in my mind.
But in dealing with more complex tasks, advance planning and modeling greatly simplify programming. In addition, making changes to class diagrams is easier than making source code.
You can draw an analogy with the construction of the house. When someone wants to build a house, he doesn’t just hit with a hammer and starts work. He needs to have a plan - a design plan so that he can analyze and modify his system.
If you have already started to describe your task on paper, this is already a huge plus.
What is UML
The official definition of wikipedia.
UML - Unified Modeling Language (Unified Modeling Language) - is a notation system that can be used for object-oriented analysis and design. It can be used for visualization, specification, design and documentation of software systems.
Simply put, if you look at the pictures in the search engines, it becomes clear that UML is something about schemes, arrows and squares.
It is important that UML translates as Unified Modeling Language. The main word here is Unified. That is, our pictures will be understood not only by us, but also by others who know UML. It turns out that this is such an international language for drawing schemes.
Pros and cons of UML design
Minuses:- waste of time;
- the need for knowledge of various diagrams and their notations.
Pros:- opportunity to look at the task from different points of view;
- It is easier for other programmers to understand the essence of the task and the method of its implementation
- diagrams are relatively easy to read after a fairly quick familiarization with their syntax.
In order to figure out whether you need to use UML, you need to consider the basic diagrams. Thanks to them, the overall picture is formed, giving an idea of ​​the possibilities of expressing architectural ideas within the framework of business tasks.
All diagrams below are related. By combining them, we can achieve the required level of decomposition of individual tasks.
I propose to get acquainted with some of the most useful and frequently used diagrams.
The discussion deals with the diagrams of sequence, states, activity and the most difficult of them - the class diagram.
First, I <...>, and then <...>, and then ... Sequence diagram
Imagine that you need to describe the sequence of actions for ordering goods in the online store. Who should be involved in the process? What phases does an order go through before it is issued?
Usually, we write a long list of stages that an application must go through in order to receive the proud status of “Issued”. Then we describe exactly who will perform the specific action. And only after that we start programming.
What is the disadvantage of this approach? He is not clear.
Imagine, you have a long list of the stages described earlier and comments to them. How easy will you be to sort it out? How much time may it take? I suppose that's enough.
An alternative to this approach is to use a sequence diagram, shown in the figure below.
Sequence diagramAbove shows the actors, and each arrow is a specific action associated with them. Read more about this chart
here.State diagram We adjust old electronic clock
The state diagram allows you to describe the behavior of an individual object under certain conditions. It will also show us all possible states in which an object can be, as well as the process of changing states as a result of external influence.
Suppose we are programming a Soviet electronic watch.
To configure, we are given only a few buttons. Pretty sparsely. At the same time, we know that one of the buttons switches the clock setting mode. Another button in the first mode changes the minutes, and in the second the clock.
Instructions for setting up are rather small, but thanks to the state diagram, it is visually perceived much easier.
State diagramRead more about the state diagram
here .
Class diagram, or how to talk about your code without code
Class diagrams are used in PS modeling most frequently. They are one of the forms of a static description of a system from the point of view of its design. The class diagram does not display the dynamic behavior of the objects of the classes depicted on it. Class diagrams show classes, interfaces, and relationships between them.
In various documentation, the description of design patterns, and also, reading habr, all of us often meet the class diagram. Why is it so often used?
Suppose you need to design a system. Before you start implementing several classes, you will want to have a conceptual understanding of the system - which classes do I need? What functionality and information will these classes have? How do they interact with each other? Who can see these classes? And so on.
This is where class diagrams appear. Class diagrams are a great way to visualize classes on your system before you start coding them. They are a static representation of the structure of your system.
It is the class diagram that gives us the most complete and detailed idea of ​​the structure and relationships in the program code. Understanding the principles of construction of this diagram allows you to briefly and transparently express your thoughts and ideas.
Consider how to describe the well-known Visitor design pattern using a class diagram.
A “visitor” is a behavioral design pattern that allows you to add new operations to the program without changing the classes of objects on which these operations can be performed.
Class diagramThe most significant advantages of this diagram are:
- saving time in explaining the problem to other programmers;
- more accurate and visual representation of the structure of the main elements of the system.
The disadvantages include significant time costs, provided there is a lack of experience with this diagram.
You can read more about the class diagram
here , and about the “Visitor” pattern
here .
Activity Chart
An activity diagram is a technology that allows you to describe the logic of procedures, business processes and workflows. In many cases, they resemble flowcharts, but the fundamental difference between activity diagrams and flowchart notation is that the former support parallel processes.
In short, the activity diagram helps us describe the logic of the system behavior. It is possible to construct several activity diagrams for the same system, each of which will focus on different aspects of the system, showing the different actions that are performed inside it.
It is in the activity diagram that transitions from one activity to another are presented. This is, in essence, a kind of state diagram, where all or most of the states are some activities, and all or most of the transitions are triggered when a certain activity is completed and allow you to proceed to the next.
Activity ChartThe meaning of the diagram is quite understandable. It shows the work with the web application, which solves a problem in a remote database. Pay attention to the location of the activities on this diagram: they are scattered in three columns, each of which corresponds to the behavior of one of the three objects - the client, the web server and the database server. This makes it easy to determine which of the objects each of the activities is performed with.
Read more about the activity diagram
here .
Conclusion
I hope that after this article you look at the UML in a different way. Now, when reading the literature or sites devoted to this topic, it will be easier for you to understand what purpose UML pursues, and find opportunities for its application. Try to start using it and you will feel all the strength and power hidden behind a set of arrows and squares.
Leave a comment if you think (or know) that something is wrong or could be described better.