Consider the following alternative interface for the counter ADT: public interfa
ID: 3742973 • Letter: C
Question
Consider the following alternative interface for the counter ADT:
public interface PoorlyDesignedCounter {
//Sets the value of the counter to the parameter.
void setCounter(int counter);
//Increments the counter by one.
void increment();
//Increments the counter by 42.
void increment42();
//Returns the number of increments since creation.
int tally();
//Returns an array of all values returned by tally.
int tallyHistory();
//Returns a string representation that counts number of increments and the ID of the counter.
String toString();
}
Sedgewick and Wayne discuss 7 basic ways an API may be poorly designed (p97; online). For example, an API may be:
1. "Too hard to implement, making it difficult or impossible to develop."
2. "Too hard to use, leading to complicated client code."
3. "Too narrow, omitting methods that clients need."
4. "Too wide, including a large number of methods not needed by any client."
5. "Too general, providing no useful abstractions."
6. "Too specific, providing an abstraction so diffuse as to be useless."
7. "Too dependent on a particular representation, therefore not freeing client code from the details of the representation."
Review PoorlyDesignedCounter for any of these issues. Identify at least two issues in PoorlyDesignedCounter, and describe them. (There are multiple correct analyses - any with a correct explanation will receive credit).
Explanation / Answer
Hi,
Please find below two points about poorly designed interface:
4. "Too wide, including a large number of methods not needed by any client."
For example, you are developing a client code with timer that you only need
setCounter() and increment() methods. Other methods are not needed by the client. Using this interface you are forced to provide implementations of the other methods that the client is not needed. like tally() , tallyHistory() etc.
7. "Too dependent on a particular representation, therefore not freeing client code from the details of the representation."
//Increments the counter by 42.
void increment42();
The method in the interface is too dependent on a particular representation. Not freeing client code from the details of the representation. Let consider the client wants to increment the counter by 60. The client has to perform some custom logic invoking the methods increment42() and increment() methods.
Instead we can have:
//Increments the counter by count.
void incrementBy(int count);
The client can simply invoke the method :
incrementBy(60);
Hope this helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.