Solved

query regarding abstract factory design pattern

Posted on 2010-08-14
8
712 Views
Last Modified: 2013-11-13
Hi

I was reading an article about abstract factory design pattern on the website
http://www.codeproject.com/KB/cs/Abstract_Factory_Pattern.aspx . The requirement of the system is

"we group a factory for creating Indian Bread and a factory for creating American bread together and then we decide what "kind of" bread to create based on the information which we will get at run time."

Now as per the definition of Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

So in the above example which of the them are related or dependent object.

Kindly advice.

Regards
Karan Gupta
0
Comment
Question by:KaranGupta
  • 4
  • 4
8 Comments
 

Author Comment

by:KaranGupta
ID: 33443181
Hi

Any updates?


Regards
Karan Gupta

0
 
LVL 22

Expert Comment

by:ambience
ID: 33449432
Check this http://en.wikipedia.org/wiki/Abstract_factory_pattern
AS a shortcut, you can consider all the objects that the IndianBreadFactory creates as related and the objects that AmericanBreadFactory creates are related.
That however is not a very good definition of the pattern, because the objects may not be related or dependent at all, except for the relation that they all are supposed to be of a certain type or implement an interface (The Is A relation). The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme.
By common theme, you can understand that all of them return Bread objects for example. You can not have Abstract Factory where some of your factories creates Breads and others create Pepsi - unless ofcourse if Pepsi is a Bread (and probably this is where the related and dependence part becomes a bit relevant).
0
 

Author Comment

by:KaranGupta
ID: 33464162
Hi

As per the tutorial I have created a document that will explain a basic example of design pattern.

Please let me know is what I have done is right or wrong.

Regards
Karan
Abstract-Factory.doc
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 33464639
Actually its not correct. It would qualify as a Brdige. see this http://en.wikipedia.org/wiki/Bridge_pattern
One big reason why? because there is no factory in the code. A factory is a Creational pattern and therefore it must create and return objects. To make it into an Abstract factory you would need something like
class IncomeTaxSystemFactory { // The Abstract Factory
IncomeTaxSystem createIncomeTaxSystem();
}
class IndianIncomeTaxSystemFactory : public IncomeTaxSystemFactory { // The Concrete Factory
IncomeTaxSystem createIncomeTaxSystem() {
return new IndianIncomTaxSystem();
}
}

The idea is that you dont know what type of income tax system would get used, for example a Builder pattern can be used to build and return the factories. Note that there is little difference between Factory and Abstract Factory. For exampe the following is factory but not Abstract Factory. The example that you picked in the document can perhaps be implemented using a simple Factory pattern, because the TaxSystems are not Factories themselves. That leads to the another IMPORTANT observation
An Abstract Factory can be thought of a Factory of Factories! If TaxSystems were Factories (Creational Pattern) then even the following would become Abstract Factory


class IncomeTaxSystemFactory {
IncomeTaxSystem createIncomeTaxSystem(String country) {
if(country == "india") return new IndianTaxSystem();
}  
}
 
0
 

Author Comment

by:KaranGupta
ID: 33518316
Hi

Sorry for the late reply. I have a question that document which I have uploaded qualify factory pattern.

Regards
Karan
0
 
LVL 22

Expert Comment

by:ambience
ID: 33519330
I cant seem to understand your question, but if you meant to ask whether it qualifies as factory or not then as I already mentioned above - it does not,
See a factory is a Creational pattern and you can see for yourself that in your document there is no ** creational ** method.
0
 

Author Comment

by:KaranGupta
ID: 33520108
Hi ambience

You have mentioned in your thread "The example that you picked in the document can perhaps be implemented using a simple Factory pattern,"

I know you have explained so wonderfully that the document which I have uploaded doesn't qualify abstract factory pattern but is this a simple factory pattern as you have mentioned in your post.

Kindly suggest.

Regards
Karan Gupta
0
 
LVL 22

Expert Comment

by:ambience
ID: 33529071
Im afraid it's not, for the same reason that a factory or abstract factory both are creational patterns, and by that we mean that they must create and return an object.
Your examples would qualify as a Brdige. see this http://en.wikipedia.org/wiki/Bridge_pattern In the case of a bridge there are different implementations of a common abstraction, as your IncomTaxSystem is an abstract and the other two classes are concrete implementations of it.
So if you were to implement a simple factory in your code then it would have been like
class IncomeTaxSystemFactory {
IncomeTaxSystem createIncomeTaxSystem(String country) {
if(country == "india") return new IndianTaxSystem();
}  
}
Look at the signature of that method and its implementation, it ** creates ** and returns an object and therefore it is a factory method.
I hope this makes it clearer.
 
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Dependencies in Software Design In software development, the idea of dependencies (http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29) is an issue of some importance. This article seeks to explain what dependencies are and where they …
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now