I have this basic design with a circular dependency. Is there any solution for this? The problem is that a Machine
cannot be created if a Model
for that Team
has been submitted.

- 131
- 6
-
Does this answer your question? [How to solve circular dependency?](https://softwareengineering.stackexchange.com/questions/306483/how-to-solve-circular-dependency) – πάντα ῥεῖ Jul 11 '20 at 16:36
1 Answers
Design
From a design point of view, circular dependencies are usual business:
- A
team
works withmodels
to build or testmachines
. There is an ambiguity sincemany
is not defined in the UML notation. It suggests a multiplicity of either0..*
or1..*
. - Conversely, every
machine
is build/tested based on a singlemodel
and every machine is build/tested by oneteam
. Here, 1 unambiguously means exactly one. - In absence of any constraints, a
machine
can be build by ateam
based on amodel
of anotherteam
.
Implementation
You'll have to cope with circular dependencies, since each class has to know about the others. The solution depends on the language: there are already plenty of them on StackOverflow. Search for the tag circular-dependency combined with your favorite language. In general, with reference semantic (i.e. java object, C++ pointer or reference, C# class object) it is no particular difficulty.
Instantiation
You cannot enforce all the associations, if they are all mandatory at the same time. It's the circularity issue of the chicken and the egg, since there would always be a mandatory associated object missing for whatever you want to instantiate.
Possible solutions:
Relax the constraints: If
many
is understood as0..*
, you could create ateam
without having neither amodel
nor amachine
. You could then create amodel
associated with theteam
, but with nomachine
yet. Finally, you could create the machine for the both other elements. This appears to me the most natural solution.Create the egg first: Allow the creation of the objects without having all the associations from the start. Make a chicken out of the egg: use a variant of the builder pattern to make sure the that after the build process, all the mandatory constraints/associations are consistent.
Decouple time and constraints: Design explicitly the life-cycle of your objects, and enforce contraints in their behavior with the state pattern. You can then keep the object in an unusable state "Not ready" until all the mandatory associations are provided. But in your situation, this seems like a bulldozer rolling over a fly, and I'd still strongly suggest to consider optinon 1.

- 74,672
- 10
- 115
- 187
-
thank you, regarding "In absence of any constraints, a machine can be build by a team based on a model of another team." is there a way to implement this contraint in the class diagram? – Antonio Santoro Jul 13 '20 at 17:42
-
@AntonioSantoro yes: just link both associations to a [constraint](https://www.uml-diagrams.org/constraint.html?context=class-diagrams) (these can be expressed in plain text) – Christophe Jul 13 '20 at 17:52