Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating new object vs. implementing a reset() method

I'm currently creating a console implementation of a game of 5 card draw poker in Java.

I have a class called HandOfCards, which will handle the proceedings of an individual hand - dealing players their cards, betting and determining the winner. I also have a class called GameOfPoker which facilitates multiple hands of poker, representing the full sitting of a game of poker.

I will construct the HandOfPoker instance for GameOfPoker as such:

HandOfPoker handOfPoker = new HandOfPoker(List<PokerPlayer> players, DeckOfCards deck);

My question is, in GameOfPoker should I instantiate a new object or should I define a reset method in HandOfPoker:

public class HandOfPoker{    
  public void reset(List<PokerPlayer> players) {
    this.players = players;
  }
}

public class GameOfPoker{
  public play() {
    // carry out game
    // then after a hand, I could either instantiate:
    //handOfPoker = new HandOfPoker(players, deck);
    // or I could reset:
    handOfPoker.reset();
    // now I'm ready to play another hand.
  }
}

Intuitively, it feels like the reset() approach seems better - as to instantiate a new object seems more costly as a new instance has to be created, and the old one has to be trashed.

Is there a best practice approach here, or is the difference between the two approaches small enough that it doesn't really matter?

like image 356
screeb Avatar asked Apr 09 '17 15:04

screeb


People also ask

What does Reset () do in Java?

reset() is an inbuilt method in java that resets variables maintaining the sum to zero. When the object of the class is created its initial value is zero.

How do you reset an object in Java?

Have a set of default values or states for your class stored inside of it. Then write a reset() method that will restore all of these defaults within the class.

How do you reset an instance in Python?

With this decorator, the instance can be reset by calling Foo. reset() , and the cls parameter will be passed automatically to the method.


1 Answers

Generally, creating a new object and letting garbage collector destroy it is not expensive, unless this is done lots of times in a very tight loop. If you do that once per hand in a game, you wouldn't be able to measure the difference.

Therefore, it's best to concentrate on the most logical way to express your design to human readers of your code while deciding whether you should implement a reset() method or not:

  • If HandOfPoker is never shared among multiple objects, not using reset() has a cleaner appearance to readers of your code, because they don't need to look inside reset() to see what's going on.
  • If HandOfPoker is shared among multiple objects, e.g. for displaying, for persistence, etc., then it is better to have a reset() rather than setting the new object in multiple places.
like image 128
Sergey Kalinichenko Avatar answered Sep 19 '22 16:09

Sergey Kalinichenko