[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: The Factory Method

James Gray

5/10/2007 12:33:00 PM

On May 10, 2007, at 1:56 AM, Enrique Comba Riepenhausen wrote:

> I was thinking about the Factory Method Pattern and following came
> to my mind.

In addition to Pat's great answer, I think it's important to note
that classes are objects in Ruby. This often aids in Factory pattern
scenarios. For example:

>> class Prose
>> def initialize(title, author)
>> @title = title
>> @author = author
>> end
>> def inspect
>> "#{self.class}: #{@title} by #{@author}"
>> end
>> end
=> nil
>> class Novel < Prose; end
=> nil
>> class ShortStory < Prose; end
=> nil
>> class Author
>> def initialize(name)
>> @name = name
>> end
>> def to_s
>> @name
>> end
>> def write(type, title)
>> type.new(title, self)
>> end
>> end
=> nil
>> james = Author.new("James")
=> #<Author:0x14a11d0 @name="James">
>> james.write(Novel, "Ruby: The Girl I Loved")
=> Novel: Ruby: The Girl I Loved by James
>> james.write(ShortStory, "Little Ruby Riding Hood")
=> ShortStory: Little Ruby Riding Hood by James

Just some more food for thought.

James Edward Gray II


7 Answers

Enrique Comba Riepenhausen

5/10/2007 1:10:00 PM

0


On 10 May 2007, at 14:33, James Edward Gray II wrote:

> On May 10, 2007, at 1:56 AM, Enrique Comba Riepenhausen wrote:
>
>> I was thinking about the Factory Method Pattern and following came
>> to my mind.
>
> In addition to Pat's great answer, I think it's important to note
> that classes are objects in Ruby. This often aids in Factory
> pattern scenarios. For example:
>
> >> class Prose
> >> def initialize(title, author)
> >> @title = title
> >> @author = author
> >> end
> >> def inspect
> >> "#{self.class}: #{@title} by #{@author}"
> >> end
> >> end
> => nil
> >> class Novel < Prose; end
> => nil
> >> class ShortStory < Prose; end
> => nil
> >> class Author
> >> def initialize(name)
> >> @name = name
> >> end
> >> def to_s
> >> @name
> >> end
> >> def write(type, title)
> >> type.new(title, self)
> >> end
> >> end
> => nil
> >> james = Author.new("James")
> => #<Author:0x14a11d0 @name="James">
> >> james.write(Novel, "Ruby: The Girl I Loved")
> => Novel: Ruby: The Girl I Loved by James
> >> james.write(ShortStory, "Little Ruby Riding Hood")
> => ShortStory: Little Ruby Riding Hood by James
>
> Just some more food for thought.
>
> James Edward Gray II

Thanks a lot James, I think this clarifies (at least from the newbie
point of view) a lot of the power under the hood in Ruby!

I'm now thinking where to include this in the Wiki.

Any idea?

Cheers, and thanks again!

>
>


James Gray

5/10/2007 1:14:00 PM

0

On May 10, 2007, at 8:10 AM, Enrique Comba Riepenhausen wrote:

> Thanks a lot James, I think this clarifies (at least from the
> newbie point of view) a lot of the power under the hood in Ruby!

Sure.

> I'm now thinking where to include this in the Wiki.
>
> Any idea?

I feel it could be part of a discussion of the factory pattern. It
fits well there, I think.

James Edward Gray II

Enrique Comba Riepenhausen

5/10/2007 1:25:00 PM

0


On 10 May 2007, at 15:14, James Edward Gray II wrote:

> On May 10, 2007, at 8:10 AM, Enrique Comba Riepenhausen wrote:
>
>> Thanks a lot James, I think this clarifies (at least from the
>> newbie point of view) a lot of the power under the hood in Ruby!
>
> Sure.
>
>> I'm now thinking where to include this in the Wiki.
>>
>> Any idea?
>
> I feel it could be part of a discussion of the factory pattern. It
> fits well there, I think.
>
> James Edward Gray II
>

Yep! Included! http://www.rubypatterns.org...
gang_of_four_patterns:factory_method

By the way, feel free to edit the wiki in your liking. I actually set
it up as a collaborative project where we all can share our knowledge
in the Design Pattern implementation and discoveries in Ruby ;)

Cheers!

Enrique Comba Riepenhausen

Robert Klemme

5/10/2007 2:57:00 PM

0

On 10.05.2007 15:25, Enrique Comba Riepenhausen wrote:
>
> On 10 May 2007, at 15:14, James Edward Gray II wrote:
>
>> On May 10, 2007, at 8:10 AM, Enrique Comba Riepenhausen wrote:
>>
>>> Thanks a lot James, I think this clarifies (at least from the newbie
>>> point of view) a lot of the power under the hood in Ruby!
>>
>> Sure.
>>
>>> I'm now thinking where to include this in the Wiki.
>>>
>>> Any idea?
>>
>> I feel it could be part of a discussion of the factory pattern. It
>> fits well there, I think.
>>
>> James Edward Gray II
>>
>
> Yep! Included!
> http://www.rubypatterns.org/doku.php/gang_of_four_patterns:fact...
>
> By the way, feel free to edit the wiki in your liking. I actually set it
> up as a collaborative project where we all can share our knowledge in
> the Design Pattern implementation and discoveries in Ruby ;)

I'd go even further and point out that *every* class[1] in Ruby is a
concrete factory - maybe even at the start of the article. In the most
general way the interface is new() i.e. what you would call a "default
constructor" in other languages. Few know that classes usually also
provide method #allocate to create an instance of the class but without
invoking #initialize. So that would be the second factory method that
all classes support. :-)

Also, you can create any number of additional factory methods on a
class, if you wish so.

And finally, since there are no interfaces in Ruby the pattern might be
less obvious, even if implemented "properly", i.e. with concrete
creation methods. (I think you do mention that on the Wiki already.)

Kind regards

robert


[1] excluding classes that do not support #new.

Enrique Comba Riepenhausen

5/10/2007 4:01:00 PM

0

On 10 May 2007, at 17:00, Robert Klemme wrote:

> I'd go even further and point out that *every* class[1] in Ruby is
> a concrete factory - maybe even at the start of the article. In
> the most general way the interface is new() i.e. what you would
> call a "default constructor" in other languages. Few know that
> classes usually also provide method #allocate to create an instance
> of the class but without invoking #initialize. So that would be
> the second factory method that all classes support. :-)
>
> Also, you can create any number of additional factory methods on a
> class, if you wish so.
>
> And finally, since there are no interfaces in Ruby the pattern
> might be less obvious, even if implemented "properly", i.e. with
> concrete creation methods. (I think you do mention that on the Wiki
> already.)
>
> Kind regards
>
> robert
>
>
> [1] excluding classes that do not support #new.

Thanks for the clarification Robert! I included it actually in the
Wiki as I think it'll help to further understand this concepts in
Ruby...

Chris Gernon

5/10/2007 4:10:00 PM

0

Thank you so much for this discussion, and for starting the wiki.

I picked up "Head First Design Patterns" a while back to try to learn
design patterns. The book is completely Java-oriented, but I thought I
could use my knowledge of Ruby to "translate" the lessons in the book (I
don't have any Java experience). By an unfortunate coincidence, though,
the first pattern they covered was the Factory pattern, and based on my
Ruby experience, not only couldn't I figure out how to do it in Ruby, I
couldn't even understand what problem they were trying to solve! I put
it down for a while and moved on to other computer books. Now that this
wiki is up and running, though, I do believe I'll go back and tackle it
again!

--
Posted via http://www.ruby-....

Enrique Comba Riepenhausen

5/10/2007 4:15:00 PM

0

On 10 May 2007, at 18:10, Chris Gernon wrote:

> Thank you so much for this discussion, and for starting the wiki.
>
> I picked up "Head First Design Patterns" a while back to try to learn
> design patterns. The book is completely Java-oriented, but I thought I
> could use my knowledge of Ruby to "translate" the lessons in the
> book (I
> don't have any Java experience). By an unfortunate coincidence,
> though,
> the first pattern they covered was the Factory pattern, and based
> on my
> Ruby experience, not only couldn't I figure out how to do it in
> Ruby, I
> couldn't even understand what problem they were trying to solve! I put
> it down for a while and moved on to other computer books. Now that
> this
> wiki is up and running, though, I do believe I'll go back and
> tackle it
> again!
>
> --
> Posted via http://www.ruby-....
>

Hey Chris!

Thanks a lot! You are welcome to post your findings in the Wiki and
help to improve the body of knowledge even more there. I think it is
a great thing to do and I am happy that it might help some of us...

Cheers,

Enrique Comba Riepenhausen