• Advertisement

Archived

This topic is now archived and is closed to further replies.

Factory Pattern: Extensibility Question

This topic is 5498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I read about the benefits of the Factory Pattern (simplified Factory without subclassing (via static method), Template Method) and they seem quite obvious to me. Factory Patterns, for example, are said to improve extensibility because object creation is centralized and therefore you don't have to worry about other parts of the code when you add new types. However, I can't think of any good example where extensiblity can be an issue when introducing new types without using the factory pattern. The only case that came into my mind is that when using a newer derived component and prefering it over the "obsolte" component it can be convenient having the construction code in one place because you'd only have to change the constructor call of the new type once - in the factory. I assume that this is an extreme case - maybe you can give me better examples. I'd be very grateful for any illustrations. [edited by - Neutron on January 21, 2003 12:25:47 PM] [edited by - Neutron on January 21, 2003 12:27:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Well, when you are parsing a file (or input, etc) and creating objects, factories are great. So instead of having to do

if (type = "apple"
object = new Apple();
if (type = "orange"
object = new Orange();

you can just do

object = Factory(type);

Share this post


Link to post
Share on other sites
Thanks for giving me a simple answer to my question
Your piece of pseudocode reminds me of an example of polymorphism I read long time ago.. so that''s how "polymorphic construction process" comes into play

Thanks again,
Neutron

Share this post


Link to post
Share on other sites
Just a thought: there''s an example in the book "Design Patterns" by Gamma.

Share this post


Link to post
Share on other sites
Another game related reason is a scripting engine. I''m developing a scripting engine now, and I use the factory pattern because I dynamically load up classes, and when scripters create a new instance of a class, I need a factory to create the proper instance.

Share this post


Link to post
Share on other sites
hm..

Maybe it is not about your statement.

But.. it must useful for you...

I had used OODBMS than factory for what object creation.

typically object creation through scripting information, is an persistence creation.

As you see, OODBMS provides this abilities.

as refer for you.

good luck

Share this post


Link to post
Share on other sites
I suggest you get Modern C++ Design by Andrei Alexendrascu. The example by AP is a bad example on how the factory is to be used.

Share this post


Link to post
Share on other sites
No, it''s about Factory pattern all right.

Using if/else for the factory defys the LSP (I forgot what it stands for) OO design principle.

Cons

1. Your factory class needs to know about every possible concrete class - compilation and dependency bottleneck.
2. Your order of creation must be correct when it comes to inheritance.

MC++D suggestion is a factory which uses registration (much like the pluggable factory article here, I realized)

Share this post


Link to post
Share on other sites
quote:
Original post by Void
LSP (I forgot what it stands for

Liskov''s Substitution Principle.

Don''t really get what that has to do with the internal implementation of the factory, tho...



"I know very well who Satan is: He is freedom. He is the uncontrolled, the incalculable, the antithesis of order and discipline, the antithesis of the legalism of outer space.... We know where a planet will be in twelve years, four months and nine days. But we don''t know where a butterfly will have flown one minute hence. Therefore the butterfly is of Satan."
-- Jens Bjørneboe

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the other ap used the "if/else"-code as an example for bad code and how it would be refactored using the factory pattern. he never went into the implementation of the factory itself.

Share this post


Link to post
Share on other sites
My bad. In my haste, I though AP was referring to the factory implementation.

Share this post


Link to post
Share on other sites

  • Advertisement