I lurk on a couple of newsgroups, one of which being comp.object. Usually, I don't post much, simply because the collective IQ of that group frightens me. There'll be an 80 thread post on the intricacies of the Observer pattern in Smalltalk, and all I'll be able to say is, "Yeah, protected members rule." Anyway, I happened to check the group this afternoon right after an interesting question was posted. Someone posted the following question:
Hi All,I have a question on what should be a class and what should not be a class.
I have customer class, employee class [Employee who provides service to customer]. In this both the class has address in it. Do you think it is correct to create a address class and use the address class in both customer and employee class?Is this concept correct?
Thanks.
Simple question, but not a simple answer!Remember, a class is something with responsibilities. It's just an abstraction that allows you to say, "This object here is obligated to know/do something." Are there enough responsibilities for an Address to justify its own class?
The answer there depends on your needs. Taking it to an extreme, Address could know the street number, street name, city, state, zip, country, etc. It could also perform a certain number of validations, to see whether the information is populated, whether the specified zip code falls under the specified state, etc. That's a lot of responsibilities; you'd want an object there.
But maybe your idea of an Address is the polar opposite, just a string like "123 Main St." In that case, an Address object wouldn't be responsible for much of anything. Why have it then?
The answer to your question depends on the responsibilities that fall under Address in your application. It's an abstract answer and I'm sorry for that, but if you think it over a little bit, you should get a good idea of whether or not the class is needed.
Best of luck,