正如一个Statistic
(或一组Statistics
)描述的Achievement
那样/那些Statistic
/s不应该存储在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
该类应将其目标(Statistic1 的 50 值)值作为字段存储在其中。
一个成就有许多相关的统计数据,这种关系必须将每个成就与所需的统计数据(及其价值)相关联。例如,Achievement1 需要值为 50(或更大)的 Statistic1 和值为 100(或更大)的 Statistic2。
统计数据已经存储在成就中,因此您所要做的就是存储成就(或他们自己的成就)的 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
orremoveStatistic
方法之一时都会更新该映射。
关于您的代码,如果您不需要该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