Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes OO, Patterns, UML and Refactoring and the fly likes Importance of abstract factory pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Importance of abstract factory pattern" Watch "Importance of abstract factory pattern" New topic

Importance of abstract factory pattern

Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 245

I was reading up on abstract factory pattern here, and I am not able to completely grasp its importance.

From what I can understand, it allows programmer to not specify instantiating the specific class. So, if there is a DocumentCreator factory and I need to create my resume then I wouldn't need to do this:

DocumentCreator creator = new ResumeCreator();

Instead I can do this with:

DocumentCreator creator = DocumentCreator.getInstance(resume); (I recall)

Does my understanding on this pattern correct and if it is not could you elaborate on its pros? Why is not specifying the concrete class such a big deal?

Also is the GOF book an easy read for someone newly acquainted with design pattern?
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

The goal is to separate *what* is being done from *how* it is being done. In other words, the code that uses the Abstract Factory only cares about getting a reference to something that is capable of doing something; how that thing is created is irrelevant. This has to do with the concept of coupling. You want your program components to be loosely coupled as opposed to being tightly coupled. Consider the following code:

The latter declaration of myList is tightly coupled to a specific implementation (ArrayList) whereas the former is not. The former is only concerned with the general behavior as defined by the List interface. This same principle is behind the Abstract Factory pattern. The benefit is that it makes your design/code less brittle/more flexible.

Junilu - [How to Ask Questions] [How to Answer Questions]
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 245

Really appreciate your explaining in plain and simple language. Thanks a bunch! I suppose the static factory is a close cousin of this pattern and understanding this will speed up comprehending the factory method pattern.
Stijn De Mulder

Joined: Apr 09, 2014
Posts: 3


I saw that there was no reply to your question "Is the GOF book easy to read?".

Well, of course it depends on what you call "easy to read". The Gof book is strong, but perhaps not the most easy resource for learning about design patterns. The book covers every aspect of the patterns it describes. So it's a very good book if you have already a fair bit of design experience and it can be used as a reference.

I personally prefer Sourcemaking if you want to get a clear overview of the patterns.

Follow me

Learn Design Patterns
I agree. Here's the link:
subject: Importance of abstract factory pattern
It's not a secret anymore!