![]() | Episode 2: Prototypes |
|
29 Jan 2010, 01:36
Sean P. DeNigris (17 posts) |
My interest was piqued by prototypes, but I still don’t get the point! I now know how to use them, but don’t understand the reasons/benefits/costs involved relative to class-based code. Sean |
|
29 Jan 2010, 04:16
Dave Thomas (233 posts) |
If nothing else, understanding how prototypes work is a key step in the path to metaprogramming mastery. But every now and then, you’ll find that they’re a great technique to use in your code. The more I code, the less I use traditional class-based design techniques. |
|
29 Jan 2010, 16:22
Sean P. DeNigris (17 posts) |
Now I’m really interested! Would you give an example of a situation you came across where prototypes provided clear value?! Thanks! |
|
29 Jan 2010, 18:47
Dave Thomas (233 posts) |
Every time you define a singleton method on an object, you’re effectively doing prototype-based subclassing. Take it a step further—rather then use statically defined classes with constructors, how often could you instead write a factory method that returns a specialized instance of an object? That’s the pattern I use a lot. |
|
29 Jan 2010, 20:33
Sean P. DeNigris (17 posts) |
Would you prefer the factory method pattern in general, or are there specific cases where it’s beneficial? |
|
29 Jan 2010, 23:33
Dave Thomas (233 posts) |
Factories are appropriate when you want specific (and perhaps varied) construction semantics. I find I use them a lot. If an object has a natural constructor, and I’m using a class, I’ll just use initialize(). But I hate code with overloaded constructors so I use factories. Sometimes those factories don’t call the underlying constructor, and instead clone objects. My recommendation: look for opportunities to experiment. These kinds of things are best experienced, rather than discussed. |
|
30 Jan 2010, 14:39
Sean P. DeNigris (17 posts) |
Great, thanks for the ideas! |
| You must be logged in to comment |

