Properties:
- Similar to simple factory. However there are multiple factory classes which shares a common abstract factory class and each of them is responsible for creating a different family of XYZ classes. XYZ here means a factory product. XYZ1 and XYZ2 are different products.
- Actual factory implementation is chosen like the way factory class itself choses its XYZ implementation. In this manner Abstract Factory Pattern can be regarded as Factory for Factory Classes.
- Identified by an abstract factory (interface as well) class defining root of factory hieracrchy and multiple factory implementations.
Implementation:
- An abstract factory class defines root for Factory hierarchy.
- Each Factory implementation has its own way creating a XYZ class instance.
- Suitable factory instance is selected and used to create XYZ instances.
Java Standard Library Implementations:
- Java.sql.Connection
Example Usage:
// Connection interface is abstract factory class.
// Obtain factory instance : For Mysql database
// com.mysql.jdbc.ConnectionImpl instance is returned.
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/3306"
+ "user=user&password=password");
// creator method createStatement(): returns concrete
// com.mysql.jdbc.StatementImpl instance
Statement statement = conn.createStatement();
// creator method prepareStatement(): returns
// concrete com.mysql.jdbc.PreparedStatement
PreparedStatement preparedStatement =
conn.prepareStatement("select * from person");
Comments
Post a Comment