|
|
|
|
One clue to help you choose whether to use a class or an interface has to do with parts of speech. If you recall, in English, there are the following parts of speech:
If you are designing your program in an Object-Oriented way, you should be thinking about the objects that your program will be dealing with. These objects can be relatively concrete, such as Person, Employee, Dog, Gorilla, etc., or they can be more ethereal, such as Idea, Relationship, Reputation, and so on. Each of these objects is therefore somewhat naturally represented by a name. A class represents such an object, and so the most natural name for such a class is a noun -- the name of the object. Sometimes, it is easy to determine an appropriate name, and sometimes less so. (I am a firm believer in spending time choosing an appropriate name for a class, because it represents what my program is trying to model. If I choose wisely, many of the questions that arise can be answered relatively easily; if I do not, things get harder.) You may have noticed that the interfaces we've looked at so far have names like Cloneable, Sortable, Serializable, and so on. These are adjectives, rather than nouns, and this convention is fairly typical. So, here are some simple rules of thumb about naming:
These aren't iron-clad rules, but I would definitely think twice before violating them -- and fully justify my choice! Appropriate choices (which come more easily with experience and an awareness of need) will often make things easier for you as you progress in the design and implementation of your program. |
|
This page was last modified on 02 October, 2007 |