views:

143

answers:

9

Suppose you have two objects, Person and Address and they have a relationship, that is a Person has an address. Obviously the person object has a reference to the address object but does the address object really have to know about the Person object? What gives that it has to be a two way relationship?

Thanks!

+1  A: 

It all depends on the use of the objects. If you have a situation where you must take an address and show (or use) its person (or persons) it will be a two-way relationship. But if you never need to access a person given an address then you will not need a two way relationship.

Other example: if those objects are associated with database tables and those table have a relationship (say Person.id == Address.IdPerson) then it will be useful to have a two way relationship in the classes for inserts and updates.

fbinder
+2  A: 

In this case it looks like your Address object doesn't need to have a relation to the Person object.

As a rule, you can think whether an object "needs to know" the other in order to work. A Person needs to know its Address, while an Address doesn't need to know the Person it belongs to.

"need to know" here reflects the need to interact with the object in a method.

pgb
A: 

In my opinion the Address object doesn't know about the Person object at all.
If it does then it interduces thight coupling to the system.
If you have to find a Person by it's Address you create a Pepole contianer that has a find method that searches by Address.

the_drow
A: 

An Address doesn't have to know of the existance of Person, I think this is not logic in a real world: an address is just a group of information (say street, town, etc), it can be used by a letter or by a person, but in each case it still remain what it is, without knowing its surroundings. It should only care of information strictly related to him.

Alberto Zaccagni
+1  A: 

The only type of situation I can think of that it would be beneficial is if you had an external reference to an Address and you wanted to know who lived there. Even then, I think a separate associative data structure that maps Address -> Person would be a better design.

Other than that, in the relationship you describe there is no reason it has to be a two-way relationship. There's no rule that says an Address needs a reference to a Person.

Bill the Lizard
A: 

What gives that it has to be a two way relationship?

The only thing I can come up with that would be a motivation for an Address to know about other objects would be if you are in dire need of caching. Let's say you have some form of application where searches are made all the time. But that's pretty much it.

Magnus Skog
+1  A: 

They way to think about it is does my object depend on a property to exist. So in this particular scenrio, Person depends on an Address therefore it needs to know about the address details. The Address on the other hand does not depend on the Person hence can exist without knowledge of the Person.

If you need to access the person via the Address I would suggest implementing a function in the Address object that you can call which will retrieve the Person(s) relating to that particular address.

James.

James
A: 

The complexity of this problem depends on the business domain and what you mean by address. In the majority of systems this is simplified to be a person object who has a relationship to an address object representing where they stay and if two people stay at the same address they address will be duplicated.

If you want to have a more complex real world model then consider changing to terminology to locations (which may even be represented spatially).

Considerations

  • A person may be related to zero or more locations (consider summer and winter homes)
  • A location exists regardless of having people related to it
  • If a person moves home consider changing the relation ( used to live at ) rather than the location. This provides a great deal of flexibility it the future
  • Multiple people can be related to the same location. This makes it very simple to find everyone who currently (or previously) stayed at a location

Storing locations and relationships provides great flexibility but it comes with an overhead, so you need to decide if your design needs this level of flexibility.

Mark
A: 

In this case I would start with defining a root object that contains a list of persons and a list of addresses. Every person and address would be allowed to access this root (parent) object. Every Person object would also contain a link to address. For an address to find all persons living at that place, I would go to the root object and filter the Person list on all persons that link to this address.

But what if a person lives at two addresses? Or has no address? :-)

Btw, if you use a root/parent class then you can create a base class "Child" that links to the "Parent" and inherit "Person" and "Address" from this child class.

Workshop Alex