作为Statistic
(或一组Statistics
)描述了一种Achievement
不应该/这些Statistic
/秒被存储在Achievement
类?例如,改进的Achievement
类:
public class Achievement {
SparseArray<Statistic> mStatistics = new SparseArray<Statistic>();
// to get a reference to the statisctics that make this achievement
public SparseArray<Statistic> getStatics() {
return mStatistics;
}
// add a new Statistic to these Achievement
public void addStatistic(int statisticId, Statistic newStat) {
// if we don't already have this particular statistic, add it
// or maybe update the underlining Statistic?!?
if (mStatistics.get(statisticId) == null) {
mStatistic.add(newStat);
}
}
// remove the Statistic
public void removeStatistic(int statisticId) {
mStatistic.delete(statisticId);
}
// check to see if this achievment has a statistic with this id
public boolean hasStatistics(int statisticId) {
return mStatistic.get(statisticId) == null ? false : true;
}
// rest of your code
}
此外,Statistic
类应该存储它的目标(50值Statistic1)值在它作为一个字段。
的成就有很多相关的统计,这个关系必须 关联的每个成就所要求的统计(和它 值)。例如,Achievement1需要Statistic1的值为 50(或更高),Statistic2的值为100(或更高)。
的统计数据已经存储在个成就因此,所有你需要做的是保存个成就(或成就他们自己)的ID数组/列表,这样,你将有机会获得的统计数据取得了这些成就。
鉴于一个统计我还需要知道什么是相关的成就 (为了检查他们统计变化时。
你会使用的成就上述阵列/列表,迭代和检查,看是否实现认为特别Statistic
:
ArrayList<Achievement> relatedAchievements = new ArrayList<Achievement>();
for (Achievement a : theListOfAchievments) {
if (a.hasStatistics(targetStatistic)) {
relatedAchievements.add(a); // at the end this will store the achievements related(that contain) the targetStatistic
}
}
另一种选择是有地方静态映射,其存储成就有一个Statistic
,映射将在每次调用addStatictic
或removeStatistic
方法时得到更新。
关于你的代码,如果你不需要Statistic
对象,并很高兴与只是抱着它id
参考,那么你可以提高statisticsForAnAchievement
有:
SparseArray<SparseIntArray> statisticsForAnAchievement;
// the index of the SparseArray is the Achievement's id
// the index of the SparseIntArray is the Statistic's id
// the value of the SparseIntArray is the Statistic's value