Solved

Factory Design pattern

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's the most efficient way to load up a couple of large table in C#? 5 45
how to read json value 2 48
Error in script 11 55
Reference issues in Visual Studio 3 29
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

805 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