Solved

query regarding abstract factory design pattern

Posted on 2010-08-14
8
710 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Agile and Scrum have almost become synonymous. Have you wondered what's the difference? Scrum is just one way to be Agile. It is the most popular which leads to the common confusion. Agile actually refers to a philosophy shared by group of developme…
Transparency shows that a company is the kind of business that it wants people to think it is.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

10 Experts available now in Live!

Get 1:1 Help Now