On Nov 17, 2007 4:38 AM, Raul Parolari <raulparolari / gmail.com> wrote: > Thufir wrote: > > > Put another way: there must be a non-contrived case of composition in > > Ruby? Yes? > > I do not really see this as a contrived example of composition; the only > thing that I did not like in the solution we examined is the name > 'Engine' for a module (for a principle given by D.Black of 'adjectives > for Modules, and nouns for Classes'..). But I think that the advantage > of a Module in this case trounces the naming principle (so far; perhaps > if design continued..). I think there are two issues here. First , how to model the relationship between car and engine, and second what the interface to the resulting car should be. Including an Engine in a car using a module seems wrong to me. Modules add behavior to an object. That, I think, is the heart of David's analogy of Modules as adjectives, the Enumerable module adds enumerability to an object, the Comparable module adds comparability... Cars don't behave like engines, they use the engine internally to provide their power source. Modeling the engine as a separate class allows different models of cars with different types of engines, likely created using some form of Factory pattern. vw_bug.engine.vroom violates the 'law' of Demeter, which says that it's bad style to reach through one object to get at another. The classic example is modeling a paperboy collecting money from a customer. Bad: the paper boy shouldn't be grabbing money from the customer's wallet. class PaperBoy def collect_from(customer) receive_payment(customer, customer.wallet.remove(2.dollars)) end end instead of class Paperboy def collect_from(customer) receive_payment(customer, customer.pay(2.dollars)) end end So I think that to start the car it should be something like vw_bug.start And the start method would cause the engine to start, producing Vrooom as a side effect, depending on the particular engine which was installed by the factory. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/