0

I'm just starting a project that involves making some AI for hex.

I've been made to use java, a language which I have very little experience with.

I have a couple questions regarding accessors and mutators.

  1. What are the benefits of using accessors/mutators? I don't understand the point
  2. I have a board, that consists of tiles. I have made a board class and tile class. A board object consists of a multidimensional array of tiles. Should the mutator for tiles in the board class set the array, or each individual tile within the array by taking two extra arguments for coordinates? What is better programming form?

Thanks

oadams
  • 3,019
  • 6
  • 30
  • 53
  • For the first part of your question please see the answers to [What is the point of setters and getters in java?](http://stackoverflow.com/questions/1461598/what-is-the-point-of-setters-and-getters-in-java) – Bill the Lizard Jul 31 '10 at 04:48

4 Answers4

3

What are the benefits of using accessors/mutators?

There are many:

  • Hiding state behind getters and setters gives you more flexibility to change the internal representation of the class state without breaking other classes that uses the state.

  • Getters and setters allow the class to do things when the state is accessed or mutated. For instance, setters can validate prior to setting, trigger events and so on. Getters can return copies of array or collection attributes to avoid exposing the classes state to the possibility of accidental (or deliberate / malicious - see below) change.

  • Getters and setters can be synchronized.

  • Getters and setters (if implemented according to the "java bean" rules) enable all sorts of object "wiring" frameworks.

And the downsides of getters and setters are minimal:

  • Any half-decent Java IDE can generate a getter / setter pair for an attribute in a couple of keystrokes. And if you let your IDE generate your getters and setters, they won't be buggy either.

  • While getters and setters make your source code files bigger, you normally don't need to pay any attention to them when you are reading the code. An IDE helps here too.

  • At runtime, simple getters and setters will be inlined so that the performance is indistinguishable from directly reading or writing a public attribute.

[My parenthetical point about "deliberate / malicious" changes to exposed arrays and collections only applies when your code may be called by untrusted code in a security sandbox. As commenters pointed out, the state of private attributes is not securely hidden ... unless you can also prevent any untrusted code from using reflection. However, if you are in that situation, my point about only returning copies can be security critical.]

I have a board, that consists of tiles. I have made a Board class and Tile class. A board object consists of a multidimensional array of tiles. Should the mutator for tiles in the board class set the array, or each individual tile within the array by taking two extra arguments for coordinates? What is better programming form?

Without more detail, I'd say that the Board class should have methods:

public void setTileAt(Tile tile, int x, int y) {...}

public Tile getTileAt(int x, int y) {...}

and you probably should not have methods:

public void setTiles(Tile[][] tiles {...}

public Tile[][] getTiles() {...}

because they are (most likely) exposing the internals of the Board class to parts of your application that (ideally) shouldn't need to know about them.

Depending on the details of your application, it may be appropriate to use som kind of Point class instead of a pair (vector) of integer board coordinates.

Obviously though, if you were trying to implement a game playing algorithm that entailed examining huge numbers of game states to figure out the "best" move, you might want to compromise in the name of performance. But this kind of "micro-optimization" is usually a bad idea.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • +1, good answer, but I would change the part about "deliberate / malicious changes". With reflection, you might still be able to change private fields from outside the class. – Bruno Reis Jul 31 '10 at 05:43
  • Indeed, access control (`private` etc.) is not intended for security. You can access private fields via reflection, so this will not stop any deliberate / malicious changes. You'd have to make use of a `SecurityManager` to stop that. – Jesper Jul 31 '10 at 05:52
  • Adding `synchronized` to get/set methods is pointless. Generally if your interface is bad enough to have get/set methods, there isn't much room to change implementation at all. If wiring frameworks can use get and set methods, they could just as easily be written to use fields (Java Beans unfortunately exposes `java.lang.reflect.Method` in its interfaces). – Tom Hawtin - tackline Jul 31 '10 at 14:42
  • @Tom - Re synchronized and API changes - generally yes, but not always. Re wiring frameworks - true in theory, but in practice popular frameworks like Spring DI *do not work* unless you follow the JavaBeans conventions for getters and setters. – Stephen C Jul 31 '10 at 14:57
1

I've done similar and found it convenient to have accessors to get individual tiles via two coordinates. The java.awt.Point class is a nice convenient wrapper for the x, y of the two dimensional array.

In this situation the accessor for individual tiles can be nice as you can add some checks to see if the x and y are valid for the array and avoid the ArrayIndexOutOfBoundsException and maybe just have it return null.

David Blevins
  • 19,178
  • 3
  • 53
  • 68
0

Getters and setters are good for encapsulation. In general, you do not want to blindly provide getters and setters for all fields that do nothing more than return the value or set the value. You should think about what part of the state of the class you want to expose to the outside, and you control that by implementing the appropriate getters and setters.

It's often a good idea to make classes immutable - in that case, you wouldn't be providing setters at all, just getters.

Unfortunately, working with properties in Java is cumbersome because Java doesn't have any language support for them. In other programming languages (C#, Ruby, Scala, ...) you can access properties in a more elegant way, for example you'd write object.name and it would below the covers call object.getName(), or you'd write object.name = ... and it would call object.setName(...). See Uniform Access Principle.

I don't know exactly what your code for the board class looks like, but unless you'd want to set a whole row of tiles at once, I see no point in creating a setter method that sets the whole array. If you'd just need to set a tile at a certain row and column, I'd create a setter that takes two coordinate arguments, and let the board class handle the complexity of arrays-within-arrays.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Whilst getters and setters are marginally better than exposing fields, it does tie the interface and implementation closely together. They are therefore very bad for encapsulation. – Tom Hawtin - tackline Jul 31 '10 at 14:44
0

I don't know much about the game Hex.But I have made a tictactoe program in java.The coding is a bit dirty so don't expect much.You can just check it out so that you can get a general idea.If you have any doubts regarding the code or logic you can mail me:emilchacko at gmail.

Emil
  • 13,577
  • 18
  • 69
  • 108