High Cohesion - all of a classes methods are related to a single
abstraction.
Completeness - All operations that are part of the abstraction
have been included.
Convenience - Makes common tasks easy for programmers.
Clarity - The names and protocols should be clear and reflect
underlying semantics.
Consistency - Methods should be consistent to each other with
respect to names, parameters, and behavior.
Exam Questions (and Solutions):
What is wrong with this interface?
public interface Mailbox { public void addMessage(Message m); public Message getCurrentMessage(); public Message removeCurrentMessage(); public void processCommand(String command); }
(Solution) While the first three methods are all cohesive the
last one is not. It
does not do only one thing and do it well. If processing messages is
necessary in the Mailbox class, then have a function per process. If it
is not, then move it out into another class.
In the String
class, what is wrong with the function regionMatches()?
Hint: Compare it to equals() and compareTo() methods
(Solution) regionMatches() has two versions. One has an
additional parameter, a
boolean, that tells it to ignore case or not. This would be fine except
that there are two other sets of methods in the String class that do
this differently.
String.equals(...) and String.equalsIgnoreCase(...)
compareTo(...) and compareToIgnoresCase(...)
Food for Thought:
When have you seen these 5 key points conflict?
Review the GridGame API to see how well it follows these principles.