Suppose we have an Abstract factory that creates for us some products. We know that the abstract factory can provide us some specific subclasses of the product but we don't want to check the type (this is the main reason for this pattern). Now we need to create a specific view for each type of object, how can we do this without know the specific type? Should the same factory create the different views?
Update: I created a github repo to try out all the different approaches.
For this problem we can look how abstract factory pattern implemented in ADO.NET.
We have an abstract factory called
DbProviderFactory
. There are several implementations of this factory likeSqlClientFactory
,MySqlClientFactory
,OracleClientFactory
etc.For this problem, the products are database related objects like connection, command, data adapter etc.
At first our abstract factory gives us a connection (product). It could be
MySqlConnection
orOracleConnection
. The only thing we know is, it is aDbConnection
.Now we need to create a command object (view) that can be used along with this connection.
As you see, command (view) is created by the connection (product) not by the abstract factory.
But this is not quite true...
What actually happens is hidden in implementation details. When abstract factory creates the connection it passes itself to the connection. And when we ask for a command, the connection creates the command via the abstract factory provided.
So, if we return to your problem, implementation can be something like this.