473,378 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Design Pattern - Abstract Factory

max
Hello,

I analyze this design pattern for a long time but I do not understand how
this pattern work and what the purpose is? (I looked a this site
http://www.dofactory.com/Patterns/PatternAbstract.aspx).

Could anybody try to explain me in his own words how this pattern work and
what the purpose is?

thanks in advance

max

PS: Generally I do not understand why they use (also in Adapter) for example
AbstractFactory factory1 = new ConcreteFactory1(); - Means they define a
object from an abstract class and create in this object a object from a
concrete class. What is the sense of that? :(
Oct 18 '05 #1
4 2490
> PS: Generally I do not understand why they use (also in Adapter) for
example
AbstractFactory factory1 = new ConcreteFactory1(); - Means they define a
object from an abstract class and create in this object a object from a
concrete class. What is the sense of that? :(


See it as a form of typecasting.

Suppose you create spin-off classes from the AbstractFactory through
inheritance, for example ConcreteFactory1, ConcreteFactory2,
ConcreteFactory3. This way you could hook any inherited of them to this one
variable.

Now imagine that the abstract class have a public Print() method and the you
override that print in the inhertied spin-offs.

Thsi way you could call factory1.Print() and depending if you hooked
ConcreteFactory1, ConcreteFactory2 or ConcreteFactory3 to it, you will call
the custom Print ConcreteFactory1.Print(), ConcreteFactory2.Print() or
ConcreteFactory3 .Print().

For example:
AbstractFactory factory1 = new ConcreteFactory1();
factory1.Print() actually calls ConcreteFactory1.Print()

but if you would have done this;
AbstractFactory factory1 = new ConcreteFactory2();
then factory1.Print() actually calls ConcreteFactory2 .Print()

In this case we could have created ConcreteFactory1 factory1 = new
ConcreteFactory1() and it would work too if ony ConcreteFactory1 is supposed
to be used, but imagin that if you create a list of items that could be
ConcreteFactory1, ConcreteFactory2 or ConcreteFactory3 then making it
AbstractFactory factory1 = new ConcreteFactory1(); is more usefull
especially in this case:

(suppose list is an array of AbstractFactory)

for (int i=0; ix<iCount; i++) {
List[i].Print();
}

Basically it is a technique to make your code simpler, readable and reusable
and is regularly used in OOP programming

I hope this helps?

Oct 18 '05 #2
max
Hello,

thank you very much for this information, now I understand the intentions
behind this statement.
Could you further explain me how this Abstract Factory work and what the
intention is behind the AbstractFactory? Unfurtunately I do not look through
it related to the UML Diagram and the explanation on my previos posted link.
:((

regards

max

"Olaf Baeyens" wrote:
PS: Generally I do not understand why they use (also in Adapter) for

example
AbstractFactory factory1 = new ConcreteFactory1(); - Means they define a
object from an abstract class and create in this object a object from a
concrete class. What is the sense of that? :(


See it as a form of typecasting.

Suppose you create spin-off classes from the AbstractFactory through
inheritance, for example ConcreteFactory1, ConcreteFactory2,
ConcreteFactory3. This way you could hook any inherited of them to this one
variable.

Now imagine that the abstract class have a public Print() method and the you
override that print in the inhertied spin-offs.

Thsi way you could call factory1.Print() and depending if you hooked
ConcreteFactory1, ConcreteFactory2 or ConcreteFactory3 to it, you will call
the custom Print ConcreteFactory1.Print(), ConcreteFactory2.Print() or
ConcreteFactory3 .Print().

For example:
AbstractFactory factory1 = new ConcreteFactory1();
factory1.Print() actually calls ConcreteFactory1.Print()

but if you would have done this;
AbstractFactory factory1 = new ConcreteFactory2();
then factory1.Print() actually calls ConcreteFactory2 .Print()

In this case we could have created ConcreteFactory1 factory1 = new
ConcreteFactory1() and it would work too if ony ConcreteFactory1 is supposed
to be used, but imagin that if you create a list of items that could be
ConcreteFactory1, ConcreteFactory2 or ConcreteFactory3 then making it
AbstractFactory factory1 = new ConcreteFactory1(); is more usefull
especially in this case:

(suppose list is an array of AbstractFactory)

for (int i=0; ix<iCount; i++) {
List[i].Print();
}

Basically it is a technique to make your code simpler, readable and reusable
and is regularly used in OOP programming

I hope this helps?

Oct 18 '05 #3
"max" <ma*@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
Hello,

I analyze this design pattern for a long time but I do not understand how
this pattern work and what the purpose is? (I looked a this site
http://www.dofactory.com/Patterns/PatternAbstract.aspx).

Could anybody try to explain me in his own words how this pattern work and
what the purpose is?

thanks in advance

max

PS: Generally I do not understand why they use (also in Adapter) for
example
AbstractFactory factory1 = new ConcreteFactory1(); - Means they define a
object from an abstract class and create in this object a object from a
concrete class. What is the sense of that? :(


First of all, the creational patterns attempt to seperate creation of an
object from its use. This ties to the notion that the 'new' keyword, when
used in a procedure that both creates and object and uses it, is harmful,
because it cannot be subclassed. 'new' always creates a concrete object and
always an object of the type specifically mentioned in the code. This is
not always appropriate. Sometimes, you want the system to have
configuration files that declare which type of a 'animal' class to create
and the calling code doesn't care if the actual object created is a 'dog' or
a 'horse'.

That is the basic foundation behind the factory patterns. The two most
common are factory method and abstract factory. Factory method just allows
an object of Type T to be created by a bit of code, and that code can have
all the logic needed to build Type T (or any if its descendents) without the
calling class being aware of which concrete object is created.

Sometimes the factory method is done by creating a single method on an
object that is otherwise part of the same heirarchy as the object it
creates. More frequently, you will have special factory object that
contains only one method, just to create the child object. Note: this is
massive overkill for situations where a descendent will never be created and
should be avoided unless you can justify it. Otherwise, your code can get
difficult to maintain.

Abstract Factory is a special case of Factory method. It is used primarily
in frameworks when you have RELATED objects. For example, let's say that
you have a system whereby your entire business object layer is nicely
isolated from the user interface. You want to run automated tests on your
user interface, but you don't want those automated tests to fail if there is
a defect in the business object layer. In other words, automated UNIT
testing. You could have a single concrete object that creates each of the
objects in the business layer. Let's say it is of Type ActualFactory. If
ActualFactory inherits from AbstractFactory interface, then you could also
have an object called MockFactory that, instead of creating the actual
business objects, creates "mock" objects... simple objects with very few
side effects that respond with minimal logic in an expected way for the sake
of testing.

So, in the real system, you would create an ActualFactory object and then
use it to create each of your business objects. In the Testing scenario,
you would create a MockFactory object instead. If the calling code used the
AbstractFactory interface that they both inherit from, then the calling code
is completely unaware if it is performing actual logic or if it is simply
being tested. This is the best way to test... where the code cannot tell if
it is being tested.

I hope this helps.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Oct 18 '05 #4
> thank you very much for this information, now I understand the intentions
behind this statement.
Could you further explain me how this Abstract Factory work and what the
intention is behind the AbstractFactory? Unfurtunately I do not look through it related to the UML Diagram and the explanation on my previos posted link. :((

The most simplest way to describe what it is doing is that this factory
class is some kind of shortcut that creates another type of object(s).
So you could have a list of different types of shortcuts (factories
instantiated as object) that creates other objects.


Oct 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
4
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
2
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and...
2
by: Chris | last post by:
Hi, I have been stuck trying to come up with a design for days. I am working on a small project regarding barcode and I want to implement a factory design. I am now confused. I decided factory...
1
by: sunny | last post by:
Hai, can anyone provide me with documents related to factory desing pattern and Abstract factory pattern.where i can gain good understanding on the thesee design patterns. Regards, Sunny
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
1
by: =?Utf-8?B?RXJpYw==?= | last post by:
I am using the factory method to solve a problem where a factory can produce product. I have a base factory class and a base product class. The problem that I am having is that for every product...
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.