Question about Object Oriented Design with Ruby

I am thinking of writing a CLI Monopoly in Ruby. This will be the first major project I have done in Ruby. Most of my programming experience has been with functional programming languages ​​such as Clojure and Haskell. I understand Object Orientation very well, but I have no experience designing object-oriented programs.

Now, here's the deal.

In Monopoly there are many spaces located around the board. Most spaces are properties, while others are others.

Would it be wise to have a class for each of the spaces? I thought that the class of the Space class, to which all other spaces are inherited, also has the Property class, which inherits from Space, and then has a class for each property that inherits the property. That would mean many classes, which makes me think that this is a terrible way to do what I'm trying to do.

What I also intended to do was use the "inherited" method to keep track of all the properties so that I could search and remove them from the list if necessary.

Such a problem arises in many programs, so my question is: is there a better way to do this, and is there something missing that is very important for object-oriented design?

Sorry if this is a stupid question, I just don't know when it comes to OOPD.

Thanks.

+5
source share
2 answers

You are on the right track, but you have gone too far, which is a common mistake for beginners with OOP. Each property should not be a separate class; they must all be instances of the Property class. I would make classes with attributes in the following lines:

Space

  • Name
  • What parts are on it
  • Which space is next (and possibly previous?)
  • Any special actions that occur when landing on it

Property (extends space)

  • Who does it belong to
  • How many houses / hotels on it
  • Property value
  • Monopoly Property Group
  • Rental rates
  • Will it be laid.

, , Boardwalk Property, , .

+9

, , ; , .

, "if" "switch"? , , . OO if/switch . , , if/switch.

, . . , , , , .. - . , , ( - ) , .

, , CityBlocks. CityBlock , "Boardwalk" "Electric Company", "Community Chest". CityBlock? CityBlocks , ZoningOrdinance?

. | Cityblock | ---- > | ZoningOrdinance |

ZoningOrdinance. "". , , "where" "", .

, , , FreeParking, Passing Go .. CityBlock. CityBlock ZoningOrdinance.

, ...

+4

All Articles