Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is passing object itself (this) to a field's method a bad approach?

Tags:

java

oop

I'm programming a small game and I have two classes: Board and Piece. Board have (so far) one Piece which can be moved in borders of Board.

public class Board {
  private Piece piece_;
  private int width, height;
  ...

  movePieceDown() {
    piece_.moveTo(this, 1, 2);
  }
}

public class Piece {
  public boolean moveTo(Board board, int x, int y) {
    // move piece to new location (x,y)
    // return true if successful
  }
}

Is it bad approach if I in piece_.moveTo(this, 1, 2); pass reference to Board so Piece could move to new location if there is no obstacles on Board? Without passing reference to Board, Piece doesn't know if there is obstacles on new location or is it out of Board borders.

It is in my opinion that every object should worry only about self: Piece should move to new location and Board should worry if it is legal move or not, BUT this sample code I have posted makes more sense and simplifies things.

TL;DR: Am I breaking some OOP guideline with passing object itself to a field's method or is it some normal OOP idiom? Thank you in advance for clarifying this to me.

like image 897
h00s Avatar asked Jun 26 '13 11:06

h00s


People also ask

Can you pass an object to a method?

When we pass a primitive type to a method, it is passed by value. But when we pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference.

What happens when an object is passed to a method as a parameter?

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

Can we pass object as argument?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

When two objects communicate with each other some parameters might be passed?

When two or more objects communicate with each other that means that those objects are sending and receiving messages. This is often called method calling. Sending object (Object A) knows which method of receiving object (Object B) is being called, so it knows more details than needed.


1 Answers

It's not a bad approach. Passing this to various methods is not uncommon thing in programming. It just depends on your design and as far as I'm concerned, nothing in Piece class.

One thing in the Board class might be confusing - movePieceDown method. You have unusable parameter Board board. If you are planning to use this as an argument, remove that attribute because the board should not receive other boards as potential arguments, and as a matter of fact, should not be aware that other boards can exist.

And finally, you could make sure that Piece sees an interface of the Board as a method parameter, and make sure that that contract will never change. For example, it could have these methods:

public interface IBoard { 
    public boolean isFieldFree(int x, int y)
    public Piece getPiece(int x, int y) 
}

No matter how you change implementation of the Board later, this is something that should always work so you don't have to rework your Piece code when you change the Board. This way you could enforce a rule that a Piece cannot change the state of your board (in the interface, don't expose any methods that change the state of the board).

like image 177
darijan Avatar answered Sep 25 '22 08:09

darijan