If I have two classes with a one-to-one relationship, when’s a good time to use mapping.
class A { ... }
class B { ... }
class C {
Map<A,B> a2b;
}
And when’s a good time to use composition?
class A {
B myB;
}
class B { ... }
Would the answer change if the relationship was one-to-many and many-to-many?
This depends on how widespread this
AtoBrelationship is used throughout the project.If lots of different areas of the project will need to know the
Bassociated withA, then it is best if this is simply a part of theAobject. On the other hand, if this association is only used in one part of the project, then you should create theHashMapin that part of the project to store the relationship.This answer does not change if it’s a one-to-many or many-to-many relationship.