Understanding Abstract Factory Pattern

R

RSH

Hi,

I am a fairly seasoned developer and have recently started looking at
software design patterns. I am starting with the Abstract Factory Pattern
and was wondering if someone could help me understand where I might use this
Pattern. I get the general concept but I am having a hard time converting
that concept into where I might use that in a real world scenerio for a
business application or web development project.

I appreciate the fact that this is a conceptual question, and I appreciate
your time.

Thank you!
Ron
 
J

Jon Skeet [C# MVP]

I am a fairly seasoned developer and have recently started looking at
software design patterns. I am starting with the Abstract Factory Pattern
and was wondering if someone could help me understand where I might use this
Pattern. I get the general concept but I am having a hard time converting
that concept into where I might use that in a real world scenerio for a
business application or web development project.

Suppose you had encapsulated your data access into an interface, and
had implemented that interface for Oracle, SQL Server etc.

The abstract factory pattern would give you a central factory to call
which would in turn call the appropriate concrete factory depending on
configuration.

At least, that's the way I understand it.

Jon
 
M

Mark Rae

Suppose you had encapsulated your data access into an interface, and
had implemented that interface for Oracle, SQL Server etc.

The abstract factory pattern would give you a central factory to call
which would in turn call the appropriate concrete factory depending on
configuration.

At least, that's the way I understand it.

Yes indeed.

What happens when you add a provider that's not part of the "standard" .NET
Framework, e.g. MySQL or SqlCe...?

If your DAL factory includes SqlClient, Oracle *and* MySQL and you try to
run it on a machine which doesn't have the MySQL .NET native data provider,
won't your app fall over due to missing references etc...?
 
J

Jon Skeet [C# MVP]

Yes indeed.

What happens when you add a provider that's not part of the "standard" .NET
Framework, e.g. MySQL or SqlCe...?

If your DAL factory includes SqlClient, Oracle *and* MySQL and you try to
run it on a machine which doesn't have the MySQL .NET native data provider,
won't your app fall over due to missing references etc...?

I believe the references will only be fetched when they're first
actively used. That's what I'd hope, anyway - I haven't actually tried
it :)

I'm sure with suitable levels of indirection it should be okay.

Jon
 
R

RSH

I almost understand that.

Are there any tutorials you know of that illustrate this concept?

Thanks,
Ron
 
M

Mark Rae

I believe the references will only be fetched when they're first
actively used. That's what I'd hope, anyway - I haven't actually tried
it :)

Hmm - I'm sure that's correct, although I've never tried it either!
I'm sure with suitable levels of indirection it should be okay.

All my work tends to be targeted at one specific RDBMS - I've never been
asked to write anything which supported multiple providers...

Therefore, I have four quite separate DALs - SqlClient, OleDb, MySql and
most recently SqlCe...

I guess an abstract factory pattern might be more efficient as far as
development goes, but I'm not sure there's much point in a client receiving
a bespoke app with redundant code...
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Buy the Design Pattern book :
http://www.amazon.com/Design-Patter...1993608?ie=UTF8&s=books&qid=1179238351&sr=8-1

There you will find a good example of how to use each pattern based on a
Text Editor.

IIRC the example there is to how to handle different UI libraries without
modifying the code, let's say you want a program to run on MacOS, Win,
Linux, etc. You know that each of these system implement the same set of
controls (TextBox, CheckBox, ListBox,etc)
in such a way that you can declare an anbstract class (or inteface).

Your code does not need to know which one especifically is being used. It
just request an instance of ITextbox and the factory returns an instance of
it. Depending of what library is in used you get a instance of a Textbox of
the running system(MacOS, Win, Linux).

Take also a look at wikipedia, they have good articles explaining the
differents patterns.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

RSH said:
I almost understand that.

Are there any tutorials you know of that illustrate this concept?

The original book (see my other email) about Design pattern has a very
extense example using a Text Editor.

Also google for the desired pattern and you will get several examples
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Mark Rae said:
Yes indeed.

What happens when you add a provider that's not part of the "standard"
.NET Framework, e.g. MySQL or SqlCe...?

If your DAL factory includes SqlClient, Oracle *and* MySQL and you try to
run it on a machine which doesn't have the MySQL .NET native data
provider, won't your app fall over due to missing references etc...?

Yes, it will fail at runtime. It will fail when you first try to use it
 
R

RSH

So correct me if Im wrong....

The Abstract Factory is really like a runtime switch that instntiates the
appropriate class based on the conditions at run time. It encapsulates not
only the classes but the logic associated with selecting the appropriate
class to create the object.

The use of an interface further promotes loose coupling between the factory
and the underlying classes.

I can see the usefullness in this for sure.

Ron
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Jon Skeet said:
I believe the references will only be fetched when they're first
actively used. That's what I'd hope, anyway - I haven't actually tried
it :)

You are correct, I have one app that use VFP and when I install it on a
machine without the OLEDB provider I get the error when I first try to use
the provider.
 
M

Mark Rae

Yes, it will fail at runtime. It will fail when you first try to use it

Just to clarify, does that mean the app will fail the first time it tries to
use any part of the DAL...?

E.g. if your DAL supports both SqlClient and SqlCe, can you still use the
SqlClient "bits" even if the SqlCe runtime isn't installed...?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Mark Rae said:
Just to clarify, does that mean the app will fail the first time it tries
to use any part of the DAL...?

The application will fail when you first try to use any feature not
currently present in the machine.

In my example if I have no registered the OleDB provider for VFP
(vfpoledb.dll) the application throw an exception when I try to create it
:OleDbConnection conn = new OleDbConnection("......");
 
M

Mark Rae

The application will fail when you first try to use any feature not
currently present in the machine.

In my example if I have no registered the OleDB provider for VFP
(vfpoledb.dll) the application throw an exception when I try to create it
:OleDbConnection conn = new OleDbConnection("......");

OK - thanks very much...
 
J

Jon Skeet [C# MVP]

Mark Rae said:
All my work tends to be targeted at one specific RDBMS - I've never been
asked to write anything which supported multiple providers...

I've only done one project which really needed it, but then we used
Hibernate so we didn't have our own abstract factory anyway. We only
needed two types of servers for production - Oracle and SQL server -
but we developed and ran unit tests against HSQLDB as well, which was
blindingly fast and therefore great for unit tests.

It's certainly interesting writing multi-backend solutions.
 
S

sloan

There is a difference between the Simple Factory Pattern
and the Abstract Factory Pattern.

I'm very good with the Simple (of course, its simple) pattern.

The Abstract one illudes you for a while, but you can get it.

http://www.dofactory.com/Patterns/PatternAbstract.aspx
but the example is a not so crystal clear.

I have a java book at home ... that gave me an example that I could finally
figure it out.
Let me know if you're interested in the ISBN.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top