Reputation: 995
Is there a corresponding representation in programming language such as java for the following sentence :
Arrow on both ends means bidirectional relation, where both classes know about each other
?
and exactly classes know about each other
?
Upvotes: 2
Views: 101
Reputation: 9952
"Know about each other" means that objects of each class participating in the relationship hold reference(s) to their related parties. e.g.:(*)
class Dog {
private Person owner;
}
class Person {
private Dog[] dogs;
}
That would correspond to a 1:Many Association among People and Dogs:
owner
is null).Note that bi-directionality means the write accessors have to ensure consistency at both ends. So, for example, Dog.setOwner()
would also have to ensure that Person.dogs
was correctly updated (by calling appropriate methods on Dog). That's the price you pay for bi-directional navigation.
If you don't need navigation both ways you can remove one of the references. For example:
class Dog {
//no reference to owner
}
class Person {
private Dog[] dogs;
}
In this example it's not possible to navigate from a Dog to its owner: but the write accessors for Person.dogs are correspondingly simpler.
hth.
--
(*) Note this is the defacto way to implement associations. There is another way: declare the relationship as a class itself. This is much less used - although can be handy for Association Classes where there are attributes of the Association itself; e.g.
class DogOwnership {
private Person owner;
private Dog dog;
private License license; // license for this Person owning this Dog
}
The same rules apply on bi-directional access however.
Upvotes: 1