Program Design

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 5472 Views - Last Post: 04 July 2015 - 07:19 AM

#16 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 30 June 2015 - 10:36 AM

Thanks for all the good examples here. Here's even more questions lol,

If a class or object has no state should I consider NOT making its own class at all?

Can I not reference an object created in say Game class in Battle class without passing it in a method or making new separate object in Battle class? For instance if I wanted to use a single dice rolling object in all of my classes.. Is this wrong or bad style or not even possible?

This post has been edited by scatman_crothers: 30 June 2015 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Program Design

Posted 30 June 2015 - 11:10 AM

Quote

If a class or object has no state should I consider NOT making its own class at all?


Not exactly sure what you mean here, but if you mean a bunch of methods which don't maintain state, you might look at java.lang.math for an example. Everything there is implemented as static methods which take arguments and return values.


Quote

Can I not reference an object created in say Game class in Battle class without passing it in a method or making new separate object in Battle class? For instance if I wanted to use a single dice rolling object in all of my classes.. Is this wrong or bad style or not even possible?


Yes, you can do this, and yes, it's wrong and bad style. You'd do it by making it a public field of Game. Then you can refer to it from any place that can see Game. The reason this is bad is that it creates a tight coupling between Battle and Game: Battle depends on Game having this object created, and Battle's behavior now depends on the state of Game in weird and unpredictable ways. Essentially, Game is now a part of Battle, and you can't debug Battle without looking at Game. This is a bad condition to be in.
Was This Post Helpful? 1
  • +
  • -

#18 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 30 June 2015 - 11:34 AM

Quote

Not exactly sure what you mean here, but if you mean a bunch of methods which don't maintain state, you might look at java.lang.math for an example. Everything there is implemented as static methods which take arguments and return values.


Yes that is what I meant, I understand that now.

Quote

Yes, you can do this, and yes, it's wrong and bad style.


So then my Dice object will have to be created separately in every class it is used in? For instance, I have a method of Dice class to determine the outcome of a battle in my Battle class. Then I have a method of Dice class to determine an action the computer will take in the Computer class. Is this setup completely wrong?
Was This Post Helpful? 0
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Program Design

Posted 30 June 2015 - 11:37 AM

View Postscatman_crothers, on 30 June 2015 - 01:34 PM, said:

So then my Dice object will have to be created separately in every class it is used in? For instance, I have a method of Dice class to determine the outcome of a battle in my Battle class. Then I have a method of Dice class to determine an action the computer will take in the Computer class. Is this setup completely wrong?


Dice seems a lot like Math to me...
Was This Post Helpful? 0
  • +
  • -

#20 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 03 July 2015 - 08:17 PM

How so? Im using randomgenerator class from acm library for the die rolls fyi
Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Program Design

Posted 03 July 2015 - 09:00 PM

Quote

if you mean a bunch of methods which don't maintain state, you might look at java.lang.math for an example. Everything there is implemented as static methods which take arguments and return values.

Was This Post Helpful? 0
  • +
  • -

#22 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 03 July 2015 - 09:57 PM

Oh ok I gotcha. I thought you meant dice literally sounded like math class and I should use that instead lol. But you meant just that I should make Dice's methods static so i dont have to create an object to use them... However, I do have a field for dice and that is the player's base roll which will allow me to effect the outcome of the dice roll based on factors from the player's choices.. Should that field be in another class? All very confusing stuff
Was This Post Helpful? 0
  • +
  • -

#23 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Program Design

Posted 03 July 2015 - 11:18 PM

That sounds like it's part of the player's state, and not part of the dice. Ask the Player object for their modifier and then apply it.
Was This Post Helpful? 0
  • +
  • -

#24 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 04 July 2015 - 07:19 AM

Thank you sir :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2