Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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

Abstract Factory Pattern

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one please explain me about below diagram(attachment).

Capture.PNG
[Thumbnail for Capture.PNG]
Abstract Factory Pattern
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should explain it in your own words, and let us know which parts you don't understand.
 
Greenhorn
Posts: 25
Oracle AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link have a good explanation about that pattern http://www.oodesign.com/abstract-factory-pattern.html
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So abstract pattern does:

create families of related objects

and

enforce dependencies between concrete classes ?
specify the types of objects to create by using a sample instance ?
separate the construction of a complex object from its representation ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Bertotti wrote:enforce dependencies between concrete classes ?


No, the abstract factory pattern is used to remove dependencies between concrete classes. The abstract factory pattern makes concrete classes depend on abstract types, and allows for the creation of instances of these abstract types without knowing ahead of time what the actual type will be.

specify the types of objects to create by using a sample instance ?


There's no such thing as a "sample instance".

separate the construction of a complex object from its representation ?


I don't know what you mean by this. What is the representation of an object?

The best way I can explain the abstract factory pattern is to use plugins as an example. When you write an application which you want to make extensible by allowing other people to write plugins for it, you need the abstract factory pattern:

You don't know the concrete type of classes that the plugin writer provides, but your application needs to use them, so you need an interface to describe what those classes can do. In the diagram you posted, your application is represented by Client, the concrete class that's provided by the plugin is represented by ProductA1 and the interface that it implements so that your application can use it is AbstractProductA.

Because you don't know how to create instances of those classes, you need the plugin writer to provide a class that can do it for you. This class does nothing else but create instances of the aforementioned ProductA. In your diagram, this class is represented by ConcreteFactory1. Because you need a common interface to be able to use ConcreteFactory1, your application needs AbstractFactory.

The flow is like this:

Your application needs AbstractProductA and AbstractProductB, but it doesn't know how to implement them. It can load plugins though, and each plugin provides a concrete implementation of AbstractFactory. Plugin 1 has a ConcreteFactory1, and whenever your application needs an instance of AbstractProductA or AbstractProductB, it asks ConcreteFactory1 to create an instance of those classes, in the form of ProductA1 and ProductB1.
 
Marco Bertotti
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum...if it removes dependecy(A) then it's either B o C of the 3 answer i posted. C seems to be builder pattern. B looks like factory...
Cant figure out whats the right answer between the 3
 
Marco Bertotti
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after reading again Cade/Sheil and on internet i come accross it should be : enforce dependencies between concrete classes. C is for sure Builder...
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the GOF book the intent of the abstract factory pattern is "provide an interface for creating families of related or dependent objects without specifying their concrete classes"


Marco Bertotti wrote:
create families of related objects


yes

Marco Bertotti wrote:
enforce dependencies between concrete classes ?


No. The abstract factory pattern doesn't "enforce dependencies" but remove.

Marco Bertotti wrote:
specify the types of objects to create by using a sample instance ?


No I think the word sample should have been single making it the singleton pattern

Marco Bertotti wrote:
separate the construction of a complex object from its representation ?


No This is indeed the builder pattern which intent is "separate the construction of a complex object from its representation so that the same construction process can create different representations"

 
Marco Bertotti
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank. Gonna take the GoF book again to have a look.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic