Suppose I have game achievement class that stores achievement information like name, popup message, etc.
class GoldMedal implements GameAchievement {
@Override
String getName() { ... }
@Override
String getAchievementDisplayMessage() { ... }
}
Then when the game ends, I want to record current achievements to local storage (SharedPreferences
) via static keys like ACHIEVEMENT_GOLD_MEDAL
:
editor.putBoolean(ACHIEVEMENT_GOLD_MEDAL, isAchievedGoldMedal);
Question: Where should these keys be stored at?
It's tempting to put it inside GoldMedal
since it's directly relevant and intuitive, but feel like it violates SRP (or no?) since it's related to storing mechanism:
class GoldMedal implements GameAchievement {
// ...
@Override
String getStorageKey() { ... }
}
// Usage:
editor.putBoolean(achievement.getStorageKey(), isAchieved(achievement));
Another way I thought of is to hide it under an interface:
interface LocallyStorable {
String getStorageKey();
}
class GoldMedal implements GameAchievement, LocallyStorable {
@Override
String getStorageKey() { ... }
}
I guess my question in general is what's the best practice to separate responsibilities that seem to be related to a shared entity like GameAchievement
?
For example, when there's a new property of a GameAchievement
in another domain, say in a tutorial page (String achievementTutorialText
), should we make another interface out of it?:
interface GameTutorialComponent {
String getTutorialText();
}