Difference between Factory Method Pattern and Abstract Factory Pattern

+1 vote
asked Jun 23, 2013 in Software Practices by anonymous

I have gone through some article about this Factory Method Pattern vs Abstract Factory Pattern? But really I do not understand what is the difference between these two design patterns. Where should I use one over the other? Shall some one explain the detail or give me some good link to understand these two.

Share

1 Answer

+1 vote
answered Jun 25, 2013 by Aadhira (1,213 points)
selected Jun 30, 2013 by administrator
 
Best answer

Abstract Factory Pattern has higher level abstraction than Factory method pattern.

Factory method pattern does know how to create object only about one concrete class. That means each concrete class will have its own factory.

For example, if there are classes like CustomerInfo, AccountInfo, AddressInfo, then we may have factory for each classes like CustomerInfoFactory, AccountInfoFactory, AddressInfoFacotry respectively, and know how to create instances for its own concrete classes. AccountInfoFactory may not know how to create instance for CustomerInfo or AddressInfo. The same is true for other 2 factories as well.

But the abstract factory pattern is capable of creating instances about the family of objects. So, it may create very specific object for the family based on the business need.

For example, if you are having data sources in different technologies like SQL Server, Oracle etc then, you may create a abstract factory for each like SQLServerDataSourceFactory, OracleDataSourceFactory and both of them would be derived from common interface like IDataSourceFactory. Then you can decide which data source you want to use and configure it in app.config or you may decide it during runtime and use that data source.

Both data source may have CustomerInfo, AccountInfo and AddressInfo. Based on the configuration, the data will be retrieved from the respective data source.

Of course both of your factories should return an object of type derived from a common interface. That means, all of your CustomerInfo, AccountInfo and AddressInfo must have derived from same interface.

To understand it clear, please take a look at the links below to know how each of these patterns are working

Factory Method Pattern:
http://www.pinfaq.com/63/what-is-factory-pattern-where-should-i-use

Abstract Factory Pattern:
http://www.pinfaq.com/70/what-is-abstract-factory-pattern-where-should-i-use

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...