Class design Help!

51 views
Skip to first unread message

oliver bee

unread,
May 25, 2013, 2:40:25 AM5/25/13
to rubyonra...@googlegroups.com
I have 3 classes ("Truck", "Semi", "Motorcycle") that all inherit from
the "Vehicle" class.
Each subclass has an attribute for "Wheels" that tells how many wheels
the vehicle has (I do not store these values in the database because
they will never change)

I'm wondering what is the preferred method for handling this? Right now
I have individual methods in each class for "Wheels":

For example:

class Truck
def wheels
4
end
end

class Motorcycle
def wheels
2
end
end

class Semi
def wheels
18
end
end

Is there a better way??? Thanks

--
Posted via http://www.ruby-forum.com/.

Norm Scherer

unread,
May 25, 2013, 2:41:52 PM5/25/13
to rubyonra...@googlegroups.com
Maybe you have a particular limited set of vehicles but trucks I know
have from 4 to 10 (or more) wheels and motorcycles have 2 or 3 wheels
and Semi-trailer trucks have 10 to 20 (or more) wheels depending on
application. I would store the number of wheels in the database.

Norm

oliver bee

unread,
May 26, 2013, 2:21:36 AM5/26/13
to rubyonra...@googlegroups.com
Thanks for the Response Norm, That isn't necessary in this instance
though (all trucks will have 4 wheels). Should I just keep with what I
have?

Hassan Schroeder

unread,
May 26, 2013, 11:27:06 AM5/26/13
to rubyonra...@googlegroups.com
On Sat, May 25, 2013 at 11:21 PM, oliver bee <li...@ruby-forum.com> wrote:
> Thanks for the Response Norm, That isn't necessary in this instance
> though (all trucks will have 4 wheels). Should I just keep with what I
> have?

I agree with Norm that this feels inauthentic - objects are typically
used to 'model' real world things and relationships, and I can see
lots of exceptions on your horizon :-)

In any case, I would probably do something like:

class Vehicle
attr_accessor :wheel_count
end

class Truck < Vehicle
def initialize(wheel_count = 4)
@wheel_count = wheel_count
end
end

... and so on.

I'd also recommend 'Practical Object-Oriented Design in Ruby'
( http://www.poodr.info/ ) for some good thoughts on this.

FWIW,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

oliver bee

unread,
May 26, 2013, 2:24:29 PM5/26/13
to rubyonra...@googlegroups.com
I originally tried to set it up that way and it works until I retrieve a
record from the database. I dont think it triggers the initialize
function. because I cant call the "wheels" method on an object after I
retrieve it.

Love U Ruby

unread,
May 26, 2013, 2:33:49 PM5/26/13
to rubyonra...@googlegroups.com
Hassan Schroeder wrote in post #1110193:
> On Sat, May 25, 2013 at 11:21 PM, oliver bee <li...@ruby-forum.com>

Nice one!

> class Vehicle
> attr_accessor :wheel_count
> end
>
> class Truck < Vehicle
> def initialize(wheel_count = 4)
> @wheel_count = wheel_count
> end
> end
>
> ... and so on.
>
> I'd also recommend 'Practical Object-Oriented Design in Ruby'
> ( http://www.poodr.info/ ) for some good thoughts on this.

Norm Scherer

unread,
May 26, 2013, 10:06:26 PM5/26/13
to rubyonra...@googlegroups.com
On 05/25/2013 11:21 PM, oliver bee wrote:
Thanks for the Response Norm, That isn't necessary in this instance 
though (all trucks will have 4 wheels).  Should I just keep with what I 
have?

What you suggested would work.  My approach would be to have a vehicle class/table with the characteristics of the vehicle types just because I think STI is more bother than reward unless there are other things that you do not mention that would drive you in that direction.  I prefer the simplest approach that will do what is needed.  It makes it easier to pass it on to someone else and I am not as likely to forget why things are like they are.

Norm

Hassan Schroeder

unread,
May 27, 2013, 11:14:44 AM5/27/13
to rubyonra...@googlegroups.com
On Sun, May 26, 2013 at 11:24 AM, oliver bee <li...@ruby-forum.com> wrote:
> I originally tried to set it up that way and it works until I retrieve a
> record from the database.

Sorry, my bad for not explicitly saying that the approach above does
imply you're going to persist that attribute (which makes sense if the
value is variable).
Reply all
Reply to author
Forward
0 new messages