Reputation: 1503
I have a Contact
class, the sole purpose of which, at the moment, is to have a name and address (which itself is split into a street and city). Should I:
A) Have name, street, and city fields in my Contact
class, or
B) Have separate Name
andAddress
classes and attach them to Contact
with a composition relationship?
Currently I can see option A as justifiable because of the simplicity of the situation, but B also seems justifiable as extra operations could be added to Name
and Address
later without unnecessarily complicating the Contact
abstraction. It also seems to make sense to group street and city data together in a more specific abstraction than just Contact
.
The context for this is an exam question, so the focus is creating a small application with sensible classes.
Which option is better? Thanks for any advice.
Upvotes: 1
Views: 164
Reputation: 14413
The question is , what represents a name? Has behaviour? can't be represented just like a String?
You can make a class Contact and a class Person, person is the delegate in class Contact for example
class Contact {
Person person;
public String getContactName(){
return person.getContactName();
}
}
Upvotes: 1
Reputation: 1231
If Name is a person name I would go for separate class for Person with name, surname etc and then class for contact with street, postcode, email, number, phone.
If name is a name of the building on whatever related with contact details I would go for one Contact class with all the fields in it.
Upvotes: 2