Question

(Still studying now and then the OO principles to get a better understanding of OOP.)

Three (JPA) entities have the following has-a relation:

A has a collection of B and B has a field 'a' pointing to A (bidirectional relation)
B has a collection of C and C has a field 'b' pointing to B (bidirectional relation)

(I would say by using JPA you get tight coupling for free. Which is not so great of course.)

A, B and C have a field 'nr'. Those fields together form a id divided by dots. Something like this: ####.####.####

Now I would like to ask A to build that number. What is the proper way to do that following the OO-principles? In my opinion C should not have any knowledge of B and B not of A.

Was it helpful?

Solution

I'd say this is a new class ID with a constructor ID(A a, B b, C c).

Depending on the real meaning of your objects it may be appropriate to give A the responsibility of creating these if you wish but clearly - as you point out - its should not be done by B or C.

i.e.

class A {
  ID newID(B b, C c) {
    return new ID(this, b, c);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top