Criei uma aplicação de exemplo com netbeans, AnagramGame. Ele gerou todo o código para mim com uma classe para definir a interface e outra para implementar.
Neste caso, foi importante/bom sob qual aspecto criar classe WordLibrary.java? Não havia necessidade de criar essa classe, poderia-se resolver o problema apenas com a StaticWordLibrary.java.
O que me dizem?
/*WordLibrary.java*/
/* Anagram Game Application */
package com.toy.anagrams.lib;
/**
* Interface defining logic for the Anagram Game application.
*/
public abstract class WordLibrary {
/**
* Constructor for subclasses.
*/
protected WordLibrary() {
}
/** Getter for the default implementation of the WordLibrary.
* @return some default implementation of WordLibrary
*/
public static WordLibrary getDefault() {
return StaticWordLibrary.DEFAULT;
}
/**
* Gets the word at a given index.
* @param idx index of required word
* @return word at that index in its natural form
*/
public abstract String getWord(int idx);
/**
* Gets the word at a given index in its scrambled form.
* @param idx index of required word
* @return word at that index in its scrambled form
*/
public abstract String getScrambledWord(int idx);
/**
* Gets the number of words in the library.
* @return the total number of plain/scrambled word pairs in the library
*/
public abstract int getSize();
/**
* Checks whether a user's guess for a word at the given index is correct.
* @param idx index of the word guessed
* @param userGuess the user's guess for the actual word
* @return true if the guess was correct; false otherwise
*/
public abstract boolean isCorrect(int idx, String userGuess);
}
/*StaticWordLibrary.java*/
/* Anagram Game Application */
package com.toy.anagrams.lib;
/**
* Implementation of the logic for the Anagram Game application.
*/
final class StaticWordLibrary extends WordLibrary {
private static final String[] WORD_LIST = {
"abstraction",
"ambiguous",
"arithmetic",
"backslash",
"bitmap",
"circumstance",
"combination",
"consequently",
"consortium",
"decrementing",
"dependency",
"disambiguate",
"dynamic",
"encapsulation",
"equivalent",
"expression",
"facilitate",
"fragment",
"hexadecimal",
"implementation",
"indistinguishable",
"inheritance",
"internet",
"java",
"localization",
"microprocessor",
"navigation",
"optimization",
"parameter",
"patrick",
"pickle",
"polymorphic",
"rigorously",
"simultaneously",
"specification",
"structure",
"lexical",
"likewise",
"management",
"manipulate",
"mathematics",
"hotjava",
"vertex",
"unsigned",
"traditional"};
private static final String[] SCRAMBLED_WORD_LIST = {
"batsartcoin",
"maibuguos",
"ratimhteci",
"abkclssha",
"ibmtpa",
"iccrmutsnaec",
"ocbmnitaoni",
"ocsnqeeutnyl",
"ocsnroitmu",
"edrcmeneitgn",
"edepdnneyc",
"idasbmgiauet",
"ydanicm",
"neacsplutaoni",
"qeiuaveltn",
"xerpseisno",
"aficilatet",
"rfgaemtn",
"ehaxedicalm",
"milpmeneatitno",
"niidtsniugsiahleb",
"niehiratcen",
"nietnret",
"ajav",
"olacilazitno",
"imrcpoorecssro",
"anivagitno",
"poitimazitno",
"aparemert",
"aprtcki",
"ipkcel",
"opylomprich",
"irogorsuyl",
"isumtlnaoesuyl",
"psceficitaoni",
"tsurtcreu",
"elixalc",
"ilekiwse",
"amanegemtn",
"aminupalet",
"amhtmetacsi",
"ohjtvaa",
"evtrxe",
"nuisngde",
"rtdatioialn"
};
final static WordLibrary DEFAULT = new StaticWordLibrary();
/**
* Singleton class.
*/
private StaticWordLibrary() {
}
/**
* Gets the word at a given index.
* @param idx index of required word
* @return word at that index in its natural form
*/
public String getWord(int idx) {
return WORD_LIST[idx];
}
/**
* Gets the word at a given index in its scrambled form.
* @param idx index of required word
* @return word at that index in its scrambled form
*/
public String getScrambledWord(int idx) {
return SCRAMBLED_WORD_LIST[idx];
}
/**
* Gets the number of words in the library.
* @return the total number of plain/scrambled word pairs in the library
*/
public int getSize() {
return WORD_LIST.length;
}
/**
* Checks whether a user's guess for a word at the given index is correct.
* @param idx index of the word guessed
* @param userGuess the user's guess for the actual word
* @return true if the guess was correct; false otherwise
*/
public boolean isCorrect(int idx, String userGuess) {
return userGuess.equals(getWord(idx));
}
}