Solved

Factory Design pattern

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A simple overview of the possibilities of using technology for project management.
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…
Introduction to Processes

617 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