?
Solved

query regarding abstract factory design pattern

Posted on 2010-08-14
8
Medium Priority
?
718 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Accepted Solution

by:
ambience earned 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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…
Introduction to Processes

765 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