0

I have java code which implements aggregation however when UML diagram is generated in IntelliJ Idea, it is generated with a composition symbol (filled diamond).

Code is given below:

public class Address {

    private String city;
    private String state;
    private String country;

    public Address(String city, String state, String country) {
        this.city = city;
        this.state = state;
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", state='" + state + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}

public class Employee {

    private String name;
    private int age;
    private Address address;

    public Employee(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}


public class Practice {

    public static void main(String[] args) {

        Address address = new Address("City", "State", "Country");
        Employee employee = new Employee("Name",22, address);


        System.out.println(employee);
        employee = null;
        System.out.println(employee);
        System.out.println(address);
    }
}

enter image description here

When code is run, it meets the requirements of aggregation concept.

My question(s) are:

  1. Does not my code implement aggregation correctly?
  2. If it implements correctly then why my UML diagram tool detects java code implementation as composition between Employee and Address?

I am looking forward for precise explanation.

Thanks

Sam
  • 122
  • 1
  • 12
  • @qwerty_so I do not understand why you closed that question like that, the 'duplicate' is not a duplicate of the OP question, please reread OP question – bruno Nov 11 '20 at 18:20
  • 1
    In the absolute your reverse is wrong to use a composition, and in fact it cannot use an aggregation too because that supposes a knowledge a tool cannot have, but may be your tool does that because of settings ? In Java it is not possible to decide from the code there is a composition because there are only pointers, but for instance in C++ it is possible to detect compositions reversing code – bruno Nov 11 '20 at 18:33
  • @qwerty_so My question does not really ask for explanation of how to implement aggregation or composition, I am looking for answers regarding UML diagram. Either my code implementation is wrong or UML is not correct. It shall help to learn right way. Thanks. – Sam Nov 11 '20 at 18:34
  • *Does not my code implement aggregation correctly* : yes, but there is no difference between the implementation of an aggregation and an simple association – bruno Nov 11 '20 at 18:36
  • Just try to understand what these connectors mean (from the referenced answer). If that isn't clear come back with that specific question. Also see @bruno's comments. – qwerty_so Nov 11 '20 at 18:50
  • @qwerty_so the OP does not ask the differences between association, aggregation and composition (subject of the 'duplicate'), he asks why the used tool produces a composition and is its implementation correct for an aggregation – bruno Nov 11 '20 at 18:53
  • @bruno I know. But actually the question comes up because he does not know what these connectors mean. Would you like to give an answer? – qwerty_so Nov 11 '20 at 20:17
  • @qwerty_so I shall agree with bruno. I am aware of UML but issue is the way it is converted by tool as composition. However, If you look into posted code, it's clearly not the implementation of composition but aggregation. I looked into tool configurations so far I don't see any specific configs. It's confusing with actual code and its conversion to UML or visa verse. Tool: https://www.jetbrains.com/help/idea/class-diagram.html#analyze_class – Sam Nov 11 '20 at 21:53
  • 1
    @Sam in the UML model you got just replace the composition by an aggregation by editing it (I suppose not needed to delete composition then to add aggregation) – bruno Nov 11 '20 at 22:28
  • 1
    Once again: Composition is about life time of objects. Whether or not the composition applies can not (or only very hard) be recognized by any tool. When designing you use a composition to tell that references must be treated in a certain way. Usually this is not needed and you can well live without it (and just use an association). In short: your tool is just cheating (or badly guessing; depending on how you look at it). – qwerty_so Nov 12 '20 at 00:12
  • Btw: the arrows used in the page you linked are just plain wrong. Forget about this tool being helpful with UML. It's just not right. – qwerty_so Nov 12 '20 at 00:15

0 Answers0