On Nov 13, 2005, at 22:12, CBlair1986 wrote:
Hello all. I’ve got a somewhat unique problem, I think. I have a list
of classes planned out, with the attributes needed for them and
everything else, but I want to figure out how the classes should
relate
to one another, so that I don’t need to needlessly repeat myself.
This isn’t unique at all, but is in fact one of the defining aspects
of all object-oriented design. You can learn about it in the Pickaxe
here: http://www.whytheluckystiff.net/ruby/pickaxe/html/tut_classes.html
For instance, say I have the classes planned as such:
class Apple
attr_accessor :taste, :type, :flavor, :flesh, …
end
class Orange
attr_accessor :taste, :type, :flavor, :peel, …
end
and so on.
class Fruit
attr :taste, :flavor, …
end
class Apple < Fruit
attr :something_unique_about_apples
end
class Citrus < Fruit
def prevents_scurvy?
true
end
end
class Orange < Citrus
has everything from Fruit and Citrus and whatever’s unique about
Oranges
end
class Lime < Citrus
has everything from Fruit and Citrus and whatever’s unique about
Limes
end
For simple, every-day objects, this is a fairly straight-forward
process: Take an apple, and take an orange. Make a list of the
properties of the apple you want to represent, and a similar list for
the orange. When you want to represent the same thing for both of
the objects, make a superclass for them for those attributes/methods
(Fruit, Citrus).