From: Kevin Smith Date: 2001-01-10T12:15:31+09:00 Subject: [ruby-talk:8975] Re: Modules and mixins Holden Glova wrote: >> Let's say you wanted to add a debug logging >> method to several unrelated classes. The method >> is called log, and it figures out once where to >> send the log data, and after that it just sends >> it. Rather than inserting the same "def log..." >> code into each class, you can mix it in by >> creating a module that has the log method >> defined, and just including it into each class >> that you want to be able to log. > >Ok, this is an example where I would probably start out with aggregation. >Perhaps a pattern to look for would be when many classes are aggregating a >common class(s) to provide a(several) common elements of functionality - >would you/others agree with this? The light my have just clicked on within my >sometimes dark tunnel of vision *wink* Bingo. Typical aggregation would allow you to insert, say, a Logger object member into each of your classes. But what if you want to actually insert a handful of methods? Or a couple data members and some methods? Mixins allow you to do that and those inserted items are part of your class itself, rather than you having to remember that they're part of an aggregated member. Kevin