• Post Reply Bookmark Topic Watch Topic
  • New Topic

Modifying an attribute of an Object  RSS feed

 
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently developing a Java text adventure game for fun in my spare time.

I would like to know what is the better approach to my small problem.

I have an interface named IGameObject:



and an abstract weapon class:



Now, lets assume I have the following sub class called WeaponGun:




Is it okay to modify an internal attribute of an object? What I mean is, everytime the user types fire at the prompt the this.numberofshots taken will be deducted by 1. Is it better to do it that way or with an internal private variable(this.shotsTaken) and deduct that instead? Is there anything wrong with deducting from this.numberofshots given that it's an attribute of WeaponGun? Or should I have a setter/getter and modify it in the main and use the setter to reassign the this.numberofshots.

The whole reason I'm asking is, I was wondering if it made sense to make certain objects immutable. Now, for certain game objects it would be good to make it immutable, and at that point, all attributes in the constructor are final and cannot be changed. For others it's not quite so easy. Given the WeaponGun, it can have several states, for example, I can have a silencer, or if the player reaches a certain level, the number of shots available could increase, or decrease with shots taken. That whole thought got me wondering if it's okay to modify an attribute of an object given that it's mutable(with good reason). I wasn't sure so I decided to ask.
 
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects should be complete units and take care of all of their own needs. So a "gun" should know how many shots it has, it should decrement it's ammo counter, it should take care of any chance of misfire, and etc. Objects should be autonomous as much as they can possibly be made to be.

you should expose those things you want the PC to be able to query with getters, and setters. So like how check how many shots left "public int shortLeft()" and reloading something like "public int loadGun(final int rounds)" where there return value would be the number of rounds actually loaded according to the capacity of the gun--cannot put 12 rounds in a 6 round weapon that already has 2 rounds in it.

In your game scenario you should possibly expand that to include the sounds it makes, a way to check how many shots are left, identification of the gun, knows (and does) move itself, knows when it needs to explode, and removes itself from the game. In a graphics based game it should be expanded to the point where each object paints itself to the screen.
Svetlana Rosemond wrote:
Is it okay to modify an internal attribute of an object? What I mean is, everytime the user types fire at the prompt the this.numberofshots taken will be deducted by 1. Is it better to do it that way or with an internal private variable(this.shotsTaken) and deduct that instead? Is there anything wrong with deducting from this.numberofshots given that it's an attribute of WeaponGun? Or should I have a setter/getter and modify it in the main and use the setter to reassign the this.numberofshots.
quote]
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Svetlana Rosemond,

I am quite surprised of method's name and the actual purpose of it.

Such method, which reads isSomethingValid() I would expect it to return boolean value and nothing else. That means, at worst I'd expect to see a method fire() and might inside of it a call of method isActionValid() if that action is actually allowed in current state.
isActionValid() I'd expect it to check if gun is not locked, if there are some bullets left, and anything else what is related and needed to be satisfied in order to accomplish fire action.
 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew that before I posted it. The method name is unconventional, given what it does. I pulled the code from an old copy of my game to illustrate my problem. I'm aware the method gives the reader the impression it returns a boolean, and it did at one point return a boolean in the early stages. After some changes the name and return type changed. Unfortunately, I didn't have the up-to-date code when I posted my question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!