简体   繁体   中英

UML to java code

I trying to understand UML class diagrams, but i am not sure if I do ... Lets assume that we have an UML class diagram like this (only required attributes are shown in each class):UML类图

The java code for that UML class diagram above should look like this (if I'm not wrong)

class Car {
    private String color;
    private int weight;
    private Gearbox gearbox;
    private Brand brand_name;
}

class Brand {
    private String Skoda;
    private String BMW;
    private Location location;
}

class Location {
    private String US;
    private String EU;
}

class Gearbox {
    enmu Gearbox {
    automatic, manual
    }
}

The question is : Am I correct ? Do I udnerstand it well ?

Here:

class Gearbox {
    enum Gearbox {
    automatic, manual;
    }
}

That better be:

enum Gearbox { AUTOMATIC, MANUAL; }

There is "no" point in wrapping an enum into a class of the same name in Java.

And from a "modeling" perspective, I would use enums also for location and brand. There is again "no" point in having a brand "class" that has fields called BMW or Skoda. A car has exactly "one" brand out of a known disjunkt set of possibilities and enums exist to express exactly such situations.

Your implementation (except that enum thing) matches the diagram. Yes, But for me correctness also implies that the "big picture" works out. And as explained: your input is inherently flawed. You managed to "correctly" express that input with Java source code. So you solved the assignment (imho). But in the real world I would sent you back home and have you rework the diagram first.

Coming from there, the real answer is: review the UML diagram first. It doesn't make sense this way. So instead of blindly implementing what is given to you - challenge requirements that are obviously inconsistent.

Apart from the Gearbox not needing a class wrap (use a simple enum), the implementation is correct, however the diagram is not correct to begin with.

In your proposed implementation you can notice that you are actually treating Location and Brand as a pseudo-enums; either use a class meaningfully, or use an enum. This kind of pseudo concept is just confusing.

Try to follow Java naming conventions when writing code (so brandName instead of brand_name, etc.)

1) No association to Gearbox

You shouldn't specify an association to an enumeration (the same for primitive types such as String, int, etc.), instead use an attribute.

Furthermore you should not the visibility (public, private, ...) for enum literals. In fact UML doesn't even allow you to specify that, because the "attributes" in enumerations are actually different concepts from attributes in classes.

在此处输入图片说明

2) brand_name is odd

A car is manufactured under a brand, which has a name. However it is better to model the brand as a separate entity with a name attribute, rather than having a "brand_name" association, or an enum.

在此处输入图片说明

If you want to preserve the brand_name "access", you can add a method that will get you the value, eg

在此处输入图片说明

3) The same for location; location might have more attributes than a country; maybe address, maybe points of contacts, etc. So treat it as yet another modeling concept.

4) Do not use shared association if you don't specify what it means

Shared association (empty rhombus) in UML has no specified meaning, which makes it flexible, but if you use it without specifying the meaning, it means nothing. So use association instead.

在此处输入图片说明

I hope I understood object oriented design and programming with UML correct in the following way.:

class Car {
    /*+-----+     +-------+
      | Car |<>---| Brand |
      +-----+     +-------+*/
    private Brand brand;

    /*+-----+1    1+--------+
      | Car |◆----| Gearbox |
      +-----+      +--------+*/
    /*Understood as composition (black diamond operator).
      That means if there is no car there can't be a gearbox (inside of the car).*/
    private Gearbox gearbox;

    private String color;
    private int weight;
    private Brand brand_name;
}


class Brand {
    /*+-------+  1    1..n +----------+
      | Brand |<>----------| Location |
      +-------+            +----------+*/
    List<Location> locations; //at least one location

    private String Skoda;
    private String BMW;
    private Location location;
}

class Location {
    private String US;
    private String EU;
}

class Gearbox {
    enmu Gearbox {
    automatic, manual
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM