0

If this place is not the proper place, please, point me where could I discuss/solve my doubts before closing the question.

It's a little thing I've had in my mind for a bit. In Java, and maybe other OOP languages, what would be better, to have...

class Entity {
  [final] int xPos;
  [final] int yPos;

  // ...
}

or rather...

class Entity {
  Position p;

  // ...
}

class Position {
  [final] int x;
  [final] int y;
}

What are the pros and the cons of this? To me, it seems more practical to have the second approach, especially when you need to enter both properties as method parameter or return value, but I'm also thinking if this would produce too many unneeded procedures (creating new objects for minor things)...

Alxe
  • 381
  • 3
  • 12

3 Answers3

2

Or you could just not write a custom class and use Point. Really it's up to you. If you need to perform calculations involving the co-ordinates, or need to pass the object's co-ordinates around a lot, then it's best to wrap them up in an object to keep your code simpler. If they are constant, and will never change (which you can never really know) then you can keep them as int values.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • I was thinking of drawing on a canvas, so an `Entity` would be represented by two `int` or `float` numbers, the coordinates `x` and `y`. Given this, I suppose their `Position` would change, but the value of the `Position` itself (that is, `p` would change, `x` and `y` not). Also, in case both properties of `Position` were final, would the JVM be intelligent enought to assign two different `new Position(x, y)` the same reference? – Alxe May 11 '13 at 15:20
2

You'd only really need a separate class definition if it represents something that has an independent purpose - so in the future might be required elsewhere or include independent functionality (division of responsibility). As Chris mentioned your example relates directly to the Point class so the latter is more appropriate.

S.W.
  • 60
  • 6
  • you'd need two references in order to maintain accessibility to both sets of coordinates, a reference is a singularity so cant hold two memory locations, it would need to be wrapped one way or another. 2 point references in the Entity class one for initialLocation and one for latestLocation, leave the second empty until it changes and any requirement for their use put into a function that checks on the presence of an actual reference in the second and return the first if null – S.W. May 11 '13 at 15:32
  • dont forget you can always extend the Point class if you need to add custom functionality – S.W. May 11 '13 at 15:39
1

Neither. The best design would be the following:

class Position {
    final int x;
    final int y;
}

interface Positionable {
    Position getPosition();
}

class Entity implements Positionable {
    private Position p;
    public Position getPosition() {
        return position;
    }
}

Reasoning:

  • Allows Position to evolve - maybe you want to add a z coordinate
  • Allows Position to be immutable
  • Allows Position to be passed around without the baggage of being contained within a heavier object
  • Allows you to freely add methods to Position, eg float distanceFrom (Position p)
  • Allows Position to outlive the object it describes
  • Allows other classes to easily be given a Position
  • Decouples as much as possible the various concerns

Note that Point is mutable, so using it or extending it is inferior from a good design perspective, because immutability seems a wise choice for such a class (it's considered a mistake that Point is mutable - for other "mistakes in java", see this answer)

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • That seems a bit complicated for just storing a pair of integers. I would only go this route if Position was independent and had a good chance of evolving on its own and it was to be reused for different purposes. But if all you need to do is get the X position of Entity or the Y position of entity, do you really need an interface and 2 extra classes? – Brandon May 11 '13 at 22:27
  • @Brandon Well I did virtually exactly this recently for a geopoint (with two doubles for lat/long) and it worked beautifully. I would do it again too. – Bohemian May 11 '13 at 23:51