simple question; cross referencing classes
Hello, I have 2 classes. Address & User. The relationship is Address belongs to User (& is a 1-1 in my case). As such I have an Address reference in my User class.
Class User {
Address addr;
...
}
Under what circumstances should I also include a reference to User within Address ?
Class Address {
User user;
...
}
Thanks
[399 byte] By [
Bhishmaa] at [2007-11-27 8:53:29]

# 1
When you need to find a user from their address. Not really often a good idea, since many users may live at the same address
# 2
Hi Bhishma,in classes with 1:n associations I usually endow them with bidirectional access. But behind this I learned the rule from XP-programming: If you need it, code it; if you don抰 have a use case, forget it :-)Best wishes, Christian Ullenboom |
# 3
> Hi Bhishma,
>
> in classes with 1:n associations I usually endow them
> with bidirectional access. But behind this I learned
> the rule from XP-programming: If you need it, code
> it; if you don抰 have a use case, forget it :-)
>
> Best wishes,
>
> Christian Ullenboom | http://www.tutego.com/
XP considers use-cases too formal :-) we work from user stories. Of course, no feature exists until you have test cases for it :p
# 4
> is a 1-1 in my caseThen just merge the both classes? (which does not represent the real world, but that's another story).
# 5
> Hello, I have 2 classes. Address & User. The
> relationship is Address belongs to User (& is a 1-1
> in my case). As such I have an Address reference in
> my User class.
"The relationship is Address belongs to User"
This relationship is based on something. This is a design that is based on something that has been established. It is unknown where you got this relationship from. However, typically in software design, there are business domain/requirements that govern the technical design.
> Under what circumstances should I also include a
> reference to User within Address ?
A reference to User within Address is also a design. You should have such a reference if there is/are business/domain requirement(s) that make such a design reasonable or required.
If there are no such business/domain requirements that mandate such an approach, then you would not design the classes in this way.
If there is a business/domain requirement that would require this type of relationship, then go for it.
These are the circumstances.
# 6
Just to labour the point further :-)
Address-user is rarely a 1:1 relationship. Consider an e-commerce site, where both you and your partner have accounts, and have goods delivered both to home and work. Your home address would have 2 users attached to it, and each of those users would have multiple addresses attached to them. Further, it is more than likely your work addresses would have other users attached to it, too, if any of your colleagues had an account with the site.
# 7
true; in my case even though the relationship between User & Address is 1-1, I have other objects such as a Shipment that also has an address associated with it, and hence I'm keeping Address separate as it's reusable. Thanks for all your answers; much appreciated.Ciao