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.