From: ahoward Date: 2003-06-19T00:35:12+09:00 Subject: Re: switching multiple interfaces of an object On Wed, 18 Jun 2003, Mauricio [iso-8859-1] Fern�ndez wrote: > Is this useful at all? very. > I thought of this as I was writing on the Adaptor pattern. I consider > the above to be a "generalized" (dynamic) adaptor. looks very cool. i've done something similar (i think) using delegation: ~/eg/ruby > cat interface.rb #!/usr/bin/env ruby require 'delegate' class Klass < SimpleDelegator def initialize @data = Data.new @a, @b, @c = [InterfaceA, InterfaceB, InterfaceC].map{|i| i.new @data} super @a end def interface= interface __setobj__( case interface.to_s when %r/^InterfaceA$/i @a when %r/^InterfaceB$/i @b when %r/^InterfaceC$/i @c else raise end ) end private class Data attr :data def initialize; @data = 42; end end class InterfaceA def initialize data; @data = data; end def meth; "InterfaceA: #{@data.data}"; end end class InterfaceB def initialize data; @data = data; end def meth; "InterfaceB: #{@data.data}"; end end class InterfaceC def initialize data; @data = data; end def meth; "InterfaceC: #{@data.data}"; end end end k = Klass.new p k.meth %w[InterfaceC InterfaceB InterfaceA].map{|i| k.interface = i and p k.meth} ~/eg/ruby > ruby interface.rb "InterfaceA: 42" "InterfaceC: 42" "InterfaceB: 42" "InterfaceA: 42" care to comment on the relative merits/drawbacks of these approaches? i really do think something standard like this would be extremely useful. -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 | ~ > ruby -e 'p(%.\x2d\x29..intern)' ====================================