Solved

Factory Design pattern

Posted on 2010-09-22
3
568 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Cobalt Digital Marketing began using the Scrum Framework development process in summer 2009.  We hired a consultant to train the teams, observe meetings, and answer questions. He recommended that we begin using one-week sprints for several reason…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now