4
\$\begingroup\$

I'm working on a game with Slick2D and am just working through the Entity class. I'm making every variable protected and then accessing them with getters and setters. Yet it feels like I'm over-complicating things and it is more efficient to simply let some of these properties be public.

public abstract class Entity {
    protected Point location;
    protected Vector2f vector;
    protected boolean visible;
    protected Image image;

    public Entity() {
        location = new Point();
        vector = new Vector2f();
    }

    public void setLocation(int x, int y) {
        location.setX(x);
        location.setY(y);
    }

    public void setVelocity(int vx, int vy) {
        vector.x = vx;
        vector.y = vy;
    }
    public void update(){
        location.setX(Math.round(location.getX() + vector.x));
        location.setY(Math.round(location.getY() + vector.y));
    }

    public void setImage(String path) {
        try {
            image = new Image("res/" + path + ".png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getX(){
        return location.getX();
    }
    public int getY(){
        return location.getY();
    }


    public Image getImage(){
        return image;
    }

Then in the main Game class:

public void render(GameContainer gc, Graphics g) throws SlickException {
    g.drawImage(player.getImage(), player.getX(), player.getY());
...
}

It just feels like additional work using so many methods as opposed to directly accessing the field.

Additionally, is there any particular reason Vector2f()'s x and y fields are public while Point's x and y must be accessed through getters/setters?

\$\endgroup\$
  • 1
    \$\begingroup\$ This question appears to be off-topic because it is about an OOP design decision, not about review of specific code. \$\endgroup\$ – Bobby Jan 3 '15 at 22:26
  • 2
    \$\begingroup\$ I'd suggest to read a good book about OOP design and patterns. But the short answer is that encapsulating fields behind getters allows to change the behavior of the getters/fields without the need to change the clients that use that object. \$\endgroup\$ – Bobby Jan 3 '15 at 22:28
  • 1
    \$\begingroup\$ Personally, I find this question on-topic. \$\endgroup\$ – Simon Forsberg Jan 3 '15 at 22:34
4
\$\begingroup\$

It just feels like additional work using so many methods as opposed to directly accessing the field.

Is it really? You have this:

g.drawImage(player.getImage(), player.getX(), player.getY());

With direct access, it would be:

g.drawImage(player.image, player.location.getX(), player.location.getY());

Which actually seems more complicated to me. It's longer, and you have to know of what type location is. Additionally, any decent IDE can generate fields plus getters and setters for you.

But the actual point of using getters and setters is that it encapsulates your code. For example, if you want to switch from using Point to using some other class. With getters/setters, you only have to change your Entity class, instead of that class plus your main plus maybe others.

Misc

  • correct indentation makes your code easier to read
  • your fields actually should not be protected, but private
  • I would not just print the stack trace inside the entity class, but thrown an exception and handle it elsewhere (just swallowing the exception will cause problems further on, because now the image is null). You could also try to recover by using a generic generated error image instead, so your application still works if one image file is damaged/lost
\$\endgroup\$
  • \$\begingroup\$ That actually makes a lot more sense. I'm just not used to a big project like this I think. Thanks! \$\endgroup\$ – MattDs17 Jan 3 '15 at 22:50
  • \$\begingroup\$ The indentation on the first line really did stick out. A function definition and its contents should not be on the same level of indentation. \$\endgroup\$ – Jamal Jan 3 '15 at 23:30
  • \$\begingroup\$ It's indented correctly in my code, but I only added the 4 spaces to the first line. Is there any way to have it insert 4 spaces into every line? \$\endgroup\$ – MattDs17 Jan 4 '15 at 0:27
  • \$\begingroup\$ @MattDs17 Yes; Ctrl+k. There's a button, too. \$\endgroup\$ – Veedrac Jan 4 '15 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.