• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract Factory Pattern

 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one please explain me about below diagram(attachment).

Capture.PNG
[Thumbnail for Capture.PNG]
Abstract Factory Pattern
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should explain it in your own words, and let us know which parts you don't understand.
 
caio bessa
Greenhorn
Posts: 22
AngularJS Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link have a good explanation about that pattern http://www.oodesign.com/abstract-factory-pattern.html
 
Marco Bertotti
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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...
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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"

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!