Solved

Factory Design pattern

Posted on 2010-09-22
3
579 Views
Last Modified: 2013-11-13
To all,

I understand the benefits of a factory design if client is not supposed to know what concrete class to create, but something like this confuses me

Factory factory = new ConcreteFactory2();//Decides which object must create.
Base obj = factory.GetObject();

If this is in the main class, then why not just say
ConcreteFactory2 factory = new ConcreteFactory2()

I could understand Factory factory = new Factory(2)
where the 2 would represent the object to return.

My question:

Is there a real purpose for having  this Factory factory = new ConcreteFactory2();//Decides which object must create as the programmer needs to know which to instanciate?

Also looking at this from a factory prosepctive, I would like to hear from anyone who believes that is is better to use Abstract classes instead of interfaces.  I understand that interfaces cannot pass private variables through, but I don't really need to do that.

Thanks
0
Comment
Question by:rg20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 63 total points
ID: 33739329

Typlically in the main, it will be like:

Factory factory;

if(option = 1)
   factory = new ConcreteFactory1()
else if(option = 2)
   factory = new ConcreteFactory2()
else if(option = 3)
   factory = new ConcreteFactory3()

After this point, following code and classes will use only 'Factory factory' reference and doesn't know if it is ConcreteFactory1 or 2 or 3. That's the main purpose.

I would prefer to use interfaces first....then the Factory reference will be like:

IFactory factory;

and all the three ConcreteFactories will implement this IFactory interface...

When creating product objects, it will be like:

IProductA productA = factory.CreateProductA();








0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 62 total points
ID: 33858458
Yes as said by Kris_per by using factory pattern you can specify which object should be produced by factory based on some config settings or swtich cases.
I would like the concept of DbProviderFactory for this:
There you can specify the type of connection you want to create in config file and the same of connection object will be created at runtime by DbProvidedFactory
Please refer to http://www.simple-talk.com/dotnet/.net-framework/ado.net-2.0-factory-classes/
and http://msdn.microsoft.com/en-us/library/ms971499.aspx
yes even I like abstract class for this because it will enable you to add some base functionality. like you said private constants and all.
 
for DbConnectionFactory is also an abstract class and DbConnectionFactory
OdbcConnectionFactory, OleDbConnectionFactory , SqlConnectionFactory  are it's derived classes which is adding extra functionalites.

 
 
0
 
LVL 9

Author Closing Comment

by:rg20
ID: 33860244
I think the answer to the entire thing is where do you want to put the factory, in your main class or in another class closer to the datalayer.
Thanks for the input.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you’re making plans to join the modern business race, you should analyze various details that may affect your results. Nowadays, millions of businesses are trying to grow into established and appreciated professional enterprises.
Read about the ways of improving workplace communication.
Simple Linear Regression
Starting up a Project

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question