Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Factory Design pattern

Posted on 2010-09-22
3
Medium Priority
?
588 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 252 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 248 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Communication between departments might not happen in two different languages, but they do exist in two different worlds. With different targets and performance goals the same phrase often means something completely different to each party. Learn ho…
Transparency shows that a company is the kind of business that it wants people to think it is.
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…
Simple Linear Regression

688 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