From: Liam Date: 2007-11-18T07:40:38+09:00 Subject: Re: Composition: Build objects from other objects On Sun, Nov 18, 2007 at 05:02:12AM +0900, Thufir wrote: > > Hehe, fair enough. Ok, enough Java, it was just an entry point (although > I would be interested to hear how this is addressed in other OOP oriented > languages). What stands out about the above is: > > 1.) I don't follow all of the syntax, some of it's over my head (which > is certainly ok). It makes use of fowardable[1] from the standard library. > > 2.) The engine is being instantiated *outside* of the class definition. > I was assuming that the engine would be created in the vehicle's > initialize method. The above technique is to specify the engine when the > car is "manufactured" by passing a new instance of Engine into the new > Vehicle instance. This is more of a design decision. Providing outside access allows for inversion of control. Perhaps you would prefer to use a factory for your car? class Factory def self.build_car Car.new(Engine.new) end end class Mechanic def soup_up(car) car.engine=MustangEngine.new end end falcon=Factory.build_car car.vroom joe=Mechanic.new joe.soup_up falcon car.vroom The interface is of course rather arbitrary. Take a look at logr4[2] for some interesting composition. [1] http://ruby-doc.org/stdlib/libdoc/forwardable/rdoc/files/forwardable_rb.html [2] http://log4r.sourceforge.net/