Ron Jeffries <ronjeffries / REMOVEacm.org> writes:

> I could build a separate class, SetFactory, and do things over there,
> but in Ruby I see no reason to do that. I'm comfortable with a bunch of
> factory methods on Set, such as Set.create, except that for those
> methods to compile, all the concrete subclasses that Set uses have to be
> at least declared before they are used. So the top of the code looks
> like this:

How about having the concrete classes extend set to add their own
factories in

  # untested
  class Set
    class <<self
       protected :new
    end
  end


  class ArraySet < Set
    def Set.fromArray(ary)
      s = Set.new
      ary.each {|i| s.add(i) }
    end
  end

  class WibbleSet < Set
    def Set.fromWibble(wib)
      # ...
    end
  end


  as = Set.fromArray [ 1, 2, 3, 4 ]
  wb = Set.fromWibble( wibble, wobble, whee! )

This schema means you can add new concrete representations without
ever changing the base class.



Cheers


Dave