Meaningless Drivel is fun!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Understanding Abstract Factory design 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 "Understanding Abstract Factory design pattern?" Watch "Understanding Abstract Factory design pattern?" New topic
Author

Understanding Abstract Factory design pattern?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
The pattern says-
Intent

Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes.



If you look in above image, it is creating objects AbstractProductA and AbstractProductB, there are not necessarily or can be family of related or dependent objects.

So what does the term family of related objects really meant to be?

SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

The objects can be related in any way. Maybe they need to work together, or perhaps they have similar responsibilities.

Take a look at the BorderFactory class in the standard API. It serves as a factory for all sorts of different Borders. As you can see, the objects are related in that they fulfill the same purpose.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Stephan van Hulst wrote:The objects can be related in any way. Maybe they need to work together, or perhaps they have similar responsibilities.


Thanks this makes sense, the example shown by you have one scenario where they have similar responsibilities. I'm sure there would be real world scenarios where it won't be really family of related objects.

But if think like this, since ProductA1 & ProducA2 have common super class AbstractProductA so they are family of related products. This may not hold true for AbstractProductA & AbstractProductB.

It would have been best if the stated INTENT had broader meaning. Example-

Intent

Abstract Factory offers the interface for creating multiple independent objects, without explicitly specifying their classes.
Thorin Potjes
Greenhorn

Joined: Aug 27, 2011
Posts: 14

Hi Amandeep,

The family is actually Product A1 and Product B1; the other family is Product A2 and B2.

Like Stephan wrote, within a family, the classes can be related any way. They are logically related, rathern than through inheritance.

A good example is a set of classes that implements a look & feel: the Motif look and feel family will contain classes for buttons, text fields, etc.; the Windows look and feel will also contain classes for buttons, text fields etc.. The Windows button class is no family of the Motif button class, although they do share the same abstract parent. When switching look and feel, the Abstract Factory can provide you with a set of classes that supports this look and feel.

Hope this makes sense.


'Design Patterns for Java' trainer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding Abstract Factory design pattern?