Solved

query regarding abstract factory design pattern

Posted on 2010-08-14
8
716 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Learn how ViaSat reduced average response times for IT incidents from 10 minutes to 30 seconds.
A simple overview of the possibilities of using technology for project management.
Simple Linear Regression
Progress

717 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