JavaRush /Java Blog /Random EN /Using UML in the development and maintenance of java prog...
Javin
Level 7
Stockholm

Using UML in the development and maintenance of java programs

Published in the Random EN group
UML (Unified Modeling Language) is a unified modeling language. I learned about UML from a wonderful forum story by alex8894 Review of CASE tools for constructing UML diagrams. The book “ UML for Java Programmers ” by Robert Cecil Martin, as far as I understand, uses UML 1 standards, since the current UML came into effect after its publication. However, I believe that the basic principles of constructing such block diagrams remained without significant changes. Using UML in the development and maintenance of java programs - 1Teaching example from lecture:
/*
 * (c) Volodya Mozhenkov 2015
 * GPL version 3.0
 * Almost University http://www.almost-university.com/
 * VladimirMozhenkov@yahoo.com
 *
 */
// файл Main.java
public class Main
{
    private static void printRes(MyResult r)
    {
        if(r.hasResult())
        {
            System.out.print("Результат исполнения: ");
            System.out.println(r.getResult());
        }
        else
        {
            System.out.println("Результат отсутствует");
        }
    }
    public static void main(String[] args)
    {
        MyCalculator c1 = new MyCalculator('/');
        MyResult r = c1.calculate(4, 2);
        printRes(r);
        r = c1.calculate(40, 13);
        printRes(r);
        r = c1.calculate(1, 0);
        printRes(r);

        c1 = new MyCalculator('^');
        r = c1.calculate(2, 25);
        printRes(r);
    }
}

// файл MyCalculator.java
public class MyCalculator
{
    private class ResultClass implements MyResult
    {
        boolean valid;
        int result = 0;
        public ResultClass()
        {
            valid = false;
        }
        public ResultClass(int result)
        {
            this.result = result;
            valid = true;
        }
        public int getResult()
        {
            return result;
        }
        public boolean hasResult()
        {
            return valid;
        }
    }
    private char operation;

    public MyCalculator(char operation)
    {
        this.operation = operation;
    }

    public MyResult calculate(int a, int b)
    {
        MyResult res;

        switch(operation)
        {
            case '*':
                res = new ResultClass(a*b);
                break;
            case '/':
                if(b!=0)
                {
                    res = new ResultClass(a/b);
                }
                else
                {
                    res = new ResultClass();
                }
                break;
            case '+':
                res = new ResultClass(a+b);
                break;
            case '-':
                res = new ResultClass(a-b);
                break;
            default:
                res = new ResultClass();
                break;
        }
        return res;
    }
}

// файл MyResult.java
public interface MyResult
{
    public int getResult();
    public boolean hasResult();
}
And, using one of the free versions of the programs, I quickly drew the following block diagram of this program on a smartphone (with errors, of course, since I just started to become interested in UML): It would be interesting to see Using UML in the development and maintenance of java programs - 2and compare with what UML diagram/diagram of the same Intellij IDEA will build the programs.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION