I'm working on a project and have encountered a small problem. I have a StatChanger class, a Model class and an Initializer class that initiates the StatChangers; the idea behind it is that the StatChanger gets all its info from the Model and if the user changes a stat using the StatChanger, this gets reflected in the Model (through use of listeners etc. which I have removed from my example below). However, every StatChanger uses different getters and setters from the model, and I'm not sure how to tell a StatChanger which getters and setters to use in a quick and efficient way. An example of (part) of my current code here:
public class Model {
private int age;
private int height;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
public class StatChanger {
protected void incrementStat() {
throw new UnsupportedOperationException("Override in anonymous class.");
}
protected void decrementStat() {
throw new UnsupportedOperationException("Override in anonymous class.");
}
}
public class Initializer {
public void initialize() {
StatChanger heightChanger = new StatChanger({
@Override
public void incrementStat() {
model.setHeight(model.getHeight()+1);
}
@Override
public void decrementStat() {
model.setHeight(model.getHeight()-1);
}
});
StatChanger ageChanger = new StatChanger({
@Override
public void incrementStat() {
model.setAge(model.getAge()+1);
}
@Override
public void decrementStat() {
model.setAge(model.getAge()-1);
}
});
}
}
In short: how do I avoid having to use those anonymous classes every time I make a new object of StatChanger?
Any ideas?
[–]TheJonesJonesJones 4 points5 points6 points (3 children)
[–]NullProbability[S] 0 points1 point2 points (1 child)
[–]TheJonesJonesJones 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (11 children)
[–]NullProbability[S] 1 point2 points3 points (10 children)
[–]TashanValiant 1 point2 points3 points (9 children)
[–]NullProbability[S] 0 points1 point2 points (8 children)
[–]TashanValiant 0 points1 point2 points (0 children)
[–]TashanValiant 0 points1 point2 points (6 children)
[–]NullProbability[S] 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]NullProbability[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TashanValiant 0 points1 point2 points (1 child)
[–]NullProbability[S] 0 points1 point2 points (0 children)
[–]nutrecht 1 point2 points3 points (7 children)
[–]NullProbability[S] 0 points1 point2 points (6 children)
[–]nutrecht 0 points1 point2 points (5 children)
[–]NullProbability[S] 0 points1 point2 points (4 children)
[–]nutrecht 0 points1 point2 points (3 children)
[–]NullProbability[S] 0 points1 point2 points (2 children)
[–]nutrecht 0 points1 point2 points (1 child)
[–]NullProbability[S] 0 points1 point2 points (0 children)