• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding Abstract Factory design pattern?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic