From: ara.t.howard@... Date: 2006-09-29T00:24:23+09:00 Subject: Re: Multi-Module Alternative to the Factory Pattern? On Thu, 28 Sep 2006, Trans wrote: > Hi Pit-- > > Pit Capitain wrote: >> Tom, couldn't you pass the packager itself to the external modules and >> let them query the attributes they need? > > Yes. That would work too. And that might be the best way to do it. > Though it still means writing code to do the attribute transfer, but > that can be automated fairly easily. > > I'm still curious about this "multi-module" idea in general though. > Seems like it could be an alternative to code injection too. > > T. why not: harp:~ > cat a.rb module DEB A, B, C = %w( D E B ) def self.pack_step_one() p A end def self.pack_step_two() p B end def self.pack_step_three() p C end def self.pack_step_four(arg) p arg end end module RPM A, B, C = %w( R P M ) def self.pack_step_one() p A end def self.pack_step_two() p B end def self.pack_step_three() p C end def self.pack_step_four(arg) p arg end end class Packager def initialize type @type = type end def pack arg @type.instance_eval{ pack_step_one pack_step_two pack_step_three pack_step_four arg } end end packager = Packager.new DEB packager.pack 42 harp:~ > ruby a.rb "D" "E" "B" 42 ?? btw. for these kinds of setups: modules with state (singletons) i've been using prototypes lately: harp:~ > cat a.rb require 'prototype' DEB = prototype{ @a, @b, @c = %w( D E B ) def pack_step_one() p @a end def pack_step_two() p @b end def pack_step_three() p @c end def pack_step_four(arg) p arg end } RPM = prototype{ @a, @b, @c = %w( D E B ) def pack_step_one() p @a end def pack_step_two() p @b end def pack_step_three() p @c end def pack_step_four(arg) p arg end } class Packager attr 'type' def initialize type @type = type end def pack arg @type.instance_eval{ pack_step_one pack_step_two pack_step_three pack_step_four arg } end end packager = Packager.new DEB packager.pack 42 harp:~ > ruby -r rubygems a.rb "D" "E" "B" 42 i guess i'm failing to see what multi-module inclusion givs you that delegation plus argument passing (into the delegate module not the other way around) doesn't? regards. -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei