Re: Coupling in OOP design?
andrewmcdonagh wrote:
On May 28, 9:21 am, howa <howac...@gmail.com> wrote:
Consider the simplfied code below, do you think the design is good, or
bad?
class Person {
...
setMother(Person p) {
p.setSon(this);
this.mother = p;
}
...
}
any suggestions on improving the design?
Nothing wrong with this.
Its not 'coupling', as there is only one class 'Person' that
references (given the code above) other 'instances' of the same class.
Its a data (runtime) relationship.
OTOH there are many ways to model the relationahips implied in the example.
Not shown is whether the "son" attribute also handles "[step]daughter", or
"mother" encompasses "guardian/[step]father" as well.
Modeling every kind of relationship as an attribute in Person is valid but
potentially unwieldy. What if you decide to model aunts later? Cousins? At
least you have incest covered - if a girl marries her son and becomes her own
mother you can model it.
Other approaches include modeling the relationship as a composed object, or
member of a Collection of composed objects:
class Person
{
Collection<Relationship> relationships;
....
}
where Relationship has attributes of a Relation Enum and a target Person (or
tuple of Persons).
There are more ways to skin that cat as well.
--
Lew