On 7/25/05, Ara.T.Howard <Ara.T.Howard / noaa.gov> wrote:
> On Tue, 26 Jul 2005, Lyndon Samson wrote:
> 
> > Factory is a very common pattern in the java world, in some places
> > it's almost considered 'Evil (TM)' to have any sort of knowledge of
> > object construction.
> >
> > In the Ruby world, where Classes and Objects seem to be de-emphasised,
> > constructing an object with .new doesn't seem to be held as such a bad
> > thing.
> >
> > I know there are a few IOC containers in the RubyWorld, but they dont
> > seem to be overly popular.
> >
> > Why might this be so?
> 
> probably because 'new' is a method and really doesn't give any clue as to how
> an object is contructed - this is how i generally implement factory
> 
>    class Factory
>      class TypeA; end
>      class TypeB; end
>      class TypeC; end
> 
>      def Factory::new(arg, *a, &b)
>        klass =
>          case arg
>            when /a/
>              TypeA
>            when /b/
>              TypeB
>            when /b/
>              TypeC
>          end
>        klass::new(*a, &b)
>      end
>    end
> 
> although the returned type might depend on file extension or something else.
> since 'new' is just a method on a class object it's always free to return
> anything it likes.  the nice thing about this is that, in ruby, __every__ call
> to new is, by definition, the factory pattern - it just so happens that there
> is a default implementation inherited by class 'Class'.  simplicity.


Would you mind giving an example of how you'd use that class?  How
does it benefit the programmer?