1

Here are the docs for the Vector2 class which is supposed to encapsulate a 2D vector. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Vector2.html

Based on the demonstration here and the api, is there a way of setting the fields of Vector2 without accessing them directly, after all "Encapsulation refers to the state of objects - objects encapsulate their state and hide it from the outside". - Encapsulation vs Abstraction? Is there a reason why this class doesn't have any getters and setters for x and y?

Here's how the author of the demo set the x and y fields. Demo - http://www.kilobolt.com/day-5-the-flight-of-the-dead---adding-the-bird.html

 if (velocity.y > 200) {
        velocity.y = 200;
}
 and 
   public void onClick() {
    velocity.y = -140;
}

which I think violates encapsulation in OO design. Is there a way around this?

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • What about set(float x, float y)? – nikoliazekter Dec 13 '14 at 10:48
  • But you have some way of retrieving the x and y without breaking encapsulation – committedandroider Dec 13 '14 at 15:58
  • You mean getters? No, there isn't any getter in Vector2 class. – nikoliazekter Dec 13 '14 at 19:30
  • Yeah I think they should have them. It can't hurt can it? – committedandroider Dec 13 '14 at 22:40
  • http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Vector2.html Here you go. As you can see there is no getters. But libgdx is open source so you are free to add them. – nikoliazekter Dec 14 '14 at 11:51
  • do you add via commit to github repository? – committedandroider Dec 15 '14 at 05:28
  • If you want to change source code of libgdx, search here https://github.com/libgdx/libgdx – nikoliazekter Dec 15 '14 at 15:52
  • Yeah I don't think that I'm that good enough with github(not at all) and libgdx yet. I come back to this in the future though. – committedandroider Dec 16 '14 at 08:12
  • Nothing bad in velocity.x instead of velocity.getX(). – nikoliazekter Dec 16 '14 at 15:04
  • A Vector2 is a simple object with only two variables. There is no way to break the object by directly accessing and modifying its elements because there are no necessary side effects of changing either value. In C++, it would be a struct. – Tenfour04 Dec 17 '14 at 14:02
  • In this case the benefits of encapsulation are not really justified, because the elements are so frequently accessed and they're being used in a game engine, where performance is critical. The only benefit I can see is convenience if you are used to typing `get`. If you submit a pull request, I don't think they will accept a change that reduces visibility of `x` and `y`. Plus, it would break just about every class in every game made by anyone. :) In my opinion, there should not be getters and setters sitting along side a public variable, because it might be a source of confusion. – Tenfour04 Dec 17 '14 at 14:06
  • I mean how would making the fields private having setters/getters affect performance? – committedandroider Dec 17 '14 at 17:45
  • They don't change performance, see here http://stackoverflow.com/questions/23931546/java-getter-and-setter-faster-than-direct-access – nikoliazekter Dec 22 '14 at 17:59

0 Answers0