Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Navigable properties

 
René van der ven
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cameron,

Thanks for your great book. By far better than the Mc Graw Hill. I know because I have read that one first.
Especially the Association part. I did not find that in "Head First 5" and not in "Mc Graw Hill". So keep doing the good work!

Concerning page 134.

Example is about
Customer(name, gender, income age, Address, getAddress, setAddress)
Address(street, city, state, country)

If I extend your example with Supplier
Where do I place the getAddress and setAdress methods.

Supplier(name, gender, income age, Address, getAddress, setAddress)
or
Address(street, city, state, country, getAddress, setAddress)

If I choose the first: I do have twice the same methods in different classes. Is this correct?
When do I place the methods in the Supplier class and when do I place those methods in the Address class?


Thanks in advance for your reply.

Rene'
 
Keith Wegner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
René van der ven wrote:
Example is about
Customer(name, gender, income age, Address, getAddress, setAddress)
Address(street, city, state, country)

If I extend your example with Supplier
Where do I place the getAddress and setAdress methods.

Supplier(name, gender, income age, Address, getAddress, setAddress)
or
Address(street, city, state, country, getAddress, setAddress)



Think about this conceptually.

Does a Customer have an address? In literal terms, does he have a physical residency that something is being delivered?
Does a Supplier have an address? In literal terms, does he have a physical place of business that he sends his products from, or that a Customer sends his payment to?

The answer to both of those is Yes.

And would it make sense for an address to getAddress() or setAddress()? You're asking the class to set itself and get itself.
An Address would have methods like:

public void setStreet(String streetName){...}
public String getStreet(){...}

public void setZipCode(String zipCode){...}
public String getZipCode(){...}


 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll quite often see two classes with the same sets of methods. That's completely normal, and completely correct. After all, a person and a supplier might share the same address. Both point to the same data in memory, so only the methods are duplicated, but the data is all stored in the same place, so it's efficient.

You wouldn't put set and get Address in the Address class. The address already knows about itself. It's others that use it that need to updated it and perhaps change it.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic