Tutorial :Java accessors/mutators - programming form



Question:

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


Solution:1

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.


Solution:2

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.


Solution:3

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.


Solution:4

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »