On Tue, Jul 12, 2011 at 9:46 AM, Ramon Leon <[email protected]>wrote:
> On 07/12/2011 08:42 AM, Conrad Taylor wrote:
>
>> The above class can be refactored as to the following:
>>
>> class SiteFactory
>> def self.create( site )
>> site.new
>> end
>> end
>>
>
> I'm just curious, what exactly is the point of this class?
>
>
> Now, we can rewrite our calling routine to the following:
>>
>> [ HerSite, HisSite ].each do | klass |
>> site = SiteFactory.create( klass )
>> Spider.crawl_site( site )
>> end
>>
>
> Seems needlessly verbose, why not just get rid of the factory that isn't
> doing anything and just do...
>
>
> [ HerSite, HisSite ].each do | klass |
> Spider.crawl_site(klass.new)
> end
>
> In fact, why not just...
>
> Site.subclasses.each { | klass | Spider.crawl_site(klass.new) }
>
>
Yes, the above is possible but I can see where just getting all the
subclasses of an
class might night be what you want.
> Forgive me, I'm a Smalltalker, but this whole explicit factory business and
> explicit arrays of classes just looks too Java'ish in an object system with
> meta classes and reflection. Is there some reason you wouldn't just reflect
> the subclasses? Is there some reason for a factory that does nothing? Even
> if you need a factory, why wouldn't you just use class methods on Site?
Next, the Ruby language 1.9.2/1.9.3dev doesn't support a built in method
called subclasses like Smalltalk. Thus, one could implement a subclasses
method in the Ruby language as follows:
class Class
def subclasses
ObjectSpace.each_object(Class).select { |klass| klass < self } # select
all the methods that are derived from self (i.e. Site).
end
end
This requires opening a class called Class and defining a method called
subclasses. Furthermore, one can use a built in Ruby hook method call
inherited to arrive at the same result. For example,
class Site
@subclasses = []
class << self
attr_reader :subclasses
end
def self.inherited( klass )
@subclasses << klass
end
def to_s
puts "using #{self.class}#to_s"
end
def crawl
puts "using #{self.class}#crawl version 0"
end
end
Ramon, you're correct in saying that SiteFactory class could be remove for a
much more concise solution.
-Conrad
>
> --
> Ramon Leon
> http://onsmalltalk.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
> rubyonrails-talk@googlegroups.**com<[email protected]>
> .
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscribe@**googlegroups.com<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at http://groups.google.com/**
> group/rubyonrails-talk?hl=en<http://groups.google.com/group/rubyonrails-talk?hl=en>
> .
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.