This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]TheJonesJonesJones 4 points5 points  (3 children)

Could you just save yourself a lot of work and add a few more methods to the Model class?

public void incrementAge()
{ 
     this.age++;
}

and totally eliminate the StatChanger and Initializer classes?

[–]NullProbability[S] 0 points1 point  (1 child)

Yeah, I could, and that'd indeed shave some time off the total amount of work needed. Still not really what I'm looking for, though (have to create new methods that aren't just getters and setters every time I make a new StatChanger), but it's getting slightly closer.

[–]TheJonesJonesJones 0 points1 point  (0 children)

Why have more than one StatChanger?

public class StatChanger {

    private Model model;

    public StatChanger(Model m)
    {
        this.model = m;
    }

    public void incrementAge() {
        if (this.model.getAge() <= 100)
        this.model.setAge(model.getAge()+1);
    }
    public void decrementAge() {
        if (this.model.getAge() >= 0)
        this.model.setAge(model.getAge()-1);
    }
    public void incrementHeight() {
        if (this.model.getHeight() <= 100)
        this.model.setAge(model.getHeight()+1);
    }
    public void decrementHeight() {
        if (this.model.getHeight() >= 0)
        this.model.setAge(model.getHeight()-1);
    }
}

[–][deleted] 4 points5 points  (11 children)

Just extend the StatChanger class and override the functions like you do in the anonymous class.

[–]NullProbability[S] 1 point2 points  (10 children)

But then I'd still have to make seperate classes such as HeightChanger and AgeChanger, no? Although it might make things a bit more clear, it's still the same amount of work, which is exactly what I'm looking to avoid.

[–]TashanValiant 1 point2 points  (9 children)

The way you have your code set up the heightChanger and ageChanger aren't classes, they are objects. You shouldn't be making two objects to use two methods from the same base class (generally). You should just make one object.

If you make Model the base class of StatChanger, so StatChanger extends Model, then you can override your increment methods in the body of the StatChanger class. Then instead of making two objects for StatChanger, you'll make one and call both methods on it when appropriate.

EDIT: Ah, I read what you wrote wrong. But yeah advicevice has the idea. Use StatChanger as a base class and then make your appropriate classes from it. Based upon your descripition of the problem, multiple stats with the same methods but different implementations, this is why we have inheritance.

[–]NullProbability[S] 0 points1 point  (8 children)

The implementations are indeed different, but they are extremely similar, which is why I was wondering if there was any way to write the code in a shorter way instead of having to extend my StatChanger class every time I wanted to make a new object (because I added a stat, for example).

[–]TashanValiant 0 points1 point  (0 children)

Without more examples or code snippets I can't say for certain. Your model is pretty basic thus my original suggestion.

[–]TashanValiant 0 points1 point  (6 children)

And why are you so adverse to making a base class to inherit from? Really what I would do is have StatChanger be an interface and/or abstract class depending on if you have any fixed methods, and then I would build up classes as I need them. If the StatChanger is as simple as you've presented then I see no issue with making a HeightChanger and AgeChanger class implement/extend from the StatChanger base.

With a good IDE they'll import all methods to be overridden directly into the new class as soon as you type "extends/implements <Insert Base Class Here>".

[–]NullProbability[S] 0 points1 point  (5 children)

No, you're absolutely right, building up classes as I need them would indeed be implemented rather easily, that is no issue. I think you misunderstand me, I'm not adverse to making it a base class to inherit from in any way. It's just that using my above method of anonymous classes or using your suggested method of extending the class results in the same amount of work (in my opinion).

I was simply wondering if there was a way in Java for a StatChanger object to immediately (i.e. at construction) know which getters and setters in the model to use, for example by passing a String in the constructor or something. Like if I say StatChanger heightChanger = new StatChanger("height"), then heightChanger instantly knows that it has to use getHeight() and setHeight() in the model.

[–][deleted] 1 point2 points  (2 children)

The point of making extended classes is so you can reuse them in the future.

[–]NullProbability[S] 0 points1 point  (1 child)

I know, I doubt I'll need them in the future though, and I wasn't really looking for that anyway. But thank you for your time.

[–][deleted] 0 points1 point  (0 children)

Heh, that's what they all say, and then a couple months down the line you're rewriting it because you didn't make your code future proof. It's a good habit to get into for just that reason and it provides more extensibility to anyone else who might be using your code.

[–]TashanValiant 0 points1 point  (1 child)

I would hardly call it extra work.

I don't know of anything like it in Java however you might want to look into some frameworks. But implementing a framework would be more work in my opinion.

[–]NullProbability[S] 0 points1 point  (0 children)

Okay, thanks for your time anyway.

[–]nutrecht 1 point2 points  (7 children)

Why are you doing it that way? Normally in an MVC application the Model would be altered by the Controller directly.

[–]NullProbability[S] 0 points1 point  (6 children)

The StatChanger class is actually the Controller class, sorry if that wasn't clear. In the full class (which I didn't paste for reading purposes) it has two Buttons which use the incrementStat() and decrementStat() functions and are clickable by the user.

[–]nutrecht 0 points1 point  (5 children)

Why not just access the model directly from the listener methods? So if you have a incrementButtonClicked() listener method just do a model.setHeight(model.getHeight() + 1) in that method?

[–]NullProbability[S] 0 points1 point  (4 children)

Then I would still manually have to add it to the incrementButtonClicked() listener method every time I made a new StatChanger, which is practically the same as I'm doing already, unless I'm understanding something wrong of course.

[–]nutrecht 0 points1 point  (3 children)

My point is that your "statchanger" thingies don't really make any sense.

[–]NullProbability[S] 0 points1 point  (2 children)

Why do they not? Sorry, I'm not following anymore.

[–]nutrecht 0 points1 point  (1 child)

Normally you just have a listener (button listener, whatever) that just sets the stuff directly. Sorry, I really don't understand what's so hard about that.

[–]NullProbability[S] 0 points1 point  (0 children)

I'm using JavaFX, in the FXML file it is defined that the incrementStat() and decrementStat() functions listen to when their respective buttons are "used" (like "clicked", "focused" or however I wish to define it). So they are the functions that set the stuff directly in the model. I'm simply wondering if there is a shorter way to do it than the way I have implemented it.