Search code examples
javaassociationsumlclass-diagram

UML Is it possible to use Aggregation and Composition with two classes?


I have just began learning UML, and I was wondering if a class could composite one way and aggregate the other way (if you understand what I mean). Let's make an example:

My example

Maingui.java:

private controller;

public Maingui() {   
  controller = new Controller(this); 
}

Controller.java

private maingui;

public Controller(Maingui gui) {
  maingui = gui;
  doSomethingWithMainGui();
}

private void doSomethingWithMainGui() {
  maingui.doSomeThing();
}

Is this the correct way to show the association?


Solution

  • It's correct way to show assocation, but it's example of bad style app architecture. One of principles of good style architecture is loose coupling. You can read here about it: What is the difference between loose coupling and tight coupling in the object oriented paradigm?