From: Logan Capaldo Date: 2005-12-06T10:07:36+09:00 Subject: Re: Subclassing Class. On Dec 5, 2005, at 7:57 PM, John Carter wrote: > Ok. This is a wild idea. > > I don't like the factory pattern. > > We don't need no stinkin Factories. We have the Class object. > > All a Factory is really Class. > > However, a traditional Factory object can have... > * State. > * Richer behaviour than the Class class. > > Ok, this gets a little hairy so lets get concrete. > > class Foo > end > > fo = Foo.new > > What is the class of fo? Foo > > What is the class of Foo? Class > > Now a FooFactory object is just an object with a bit more intelligence > and state that the traditional Foo class object for making Foo > instances in a better smarter way. > > > So the obvious thing to do is instead of making a new class FooFactory > < Object, it should inherit from Class. > > Well, since Foo is already a Class, we really just want a smarter > Class. > > ie.. > > Instead of > > class FooFactory > def initialize( foo_state) > @foo_state = foo_state > end > > def foo_fu( bah) > # Knowledge of Foo making > end > > def create( needs_fu, stuff) > Foo.new( foo_fu( needs_fu), @foo_state, stuff) > end > end > > factory = FooFactory.new( 'foo juice') > foo = factory.create( 'footile') > > We really want > > class Foo < Class > def initialize( foo_state) > @foo_state = foo_state > end > > def foo_fu( bah) > # Knowledge of Foo making > end > > def new( needs_fu, stuff) > super( foo_fu( needs_fu), @foo_state, stuff) > end > end > > foo = Foo.new( 'fooless', 'stuffed') > > > Except ruby doesn't like that > class Foo < Class > bit. > > It gets quite grumpy in fact. > > Ruby says "can't make subclass of Class (TypeError)" > > Rats. > > We can however say... > > FooClass = Class.new(Class) > > Unfortunately we can't then say.... > > FooClass.new( Object) > > Ruby says... > :in `new': wrong instance allocation (TypeError) > > Ahh! I'm just going around in circles fighting Ruby. > > We need a way of saying... > > class FooClass < Class > # Add state and behaviour to Class > end > > And then instead of saying... > class Foo < FooParent > # Add state and behaviour to FooParent > end > we need to be able to say... > > FooClass Foo < FooParent > # Add state and behaviour to FooParent > end > > Aargh! I'm chasing my tail. > > Is there anyway, (short of the traditional Factory Pattern) for me > to catch my tail? > > > John Carter Phone : (64)(3) 358 6639 > Tait Electronics Fax : (64)(3) 359 4632 > PO Box 1645 Christchurch Email : john.carter@tait.co.nz > New Zealand > > Carter's Clarification of Murphy's Law. > > "Things only ever go right so that they may go more spectacularly > wrong later." > > From this principle, all of life and physics may be deduced. > Why are you trying so hard? class Foo # state and behavior for Foo the Class # using class instance variables and singleton methods # eg: @foo_state = foo_state # this works believe it or not # etc. @old_new = method(:new) def self.new(needs_fi, stuff) @old_new.call(foo_fu(needs_fu, @foo_state, stuff) end end What does subclassing class get you over this approach? Your class can be your factory (in ruby anyway).