┌────────────────────────────┐ ┌────────────────────────────┐ │ <<interface>> │ │ <<interface>> │ │ Table │ │ Column │ ← Abstract Products ├────────────────────────────┤ ├────────────────────────────┤ │ + toSQL(): String │ │ + toSQL(): String │ └────────────────────────────┘ └────────────────────────────┘ △ △ │ implements │ implements │ │ ┌─────────────────┬─────────────────┐ ┌──────────────────┬──────────────────┐ │ OracleTable │ DerbyTable │ │ OracleColumn │ DerbyColumn │ ← Concrete Products ├─────────────────┼─────────────────┤ ├──────────────────┼──────────────────┤ │+ toSQL(): String│+ toSQL(): String│ │+ toSQL(): String │+ toSQL(): String │ └─────────────────┴─────────────────┘ └──────────────────┴──────────────────┘ ┌────────────────────────────────────────────┐ │ <<interface>> │ │ SchemaFactory │ ← Abstract Factory ├────────────────────────────────────────────┤ │ + createTable(): Table │ │ + createColumn(): Column │ └────────────────────────────────────────────┘ △ │ implements │ ┌─────────────────────────┬─────────────────────────┐ │ OracleSchemaFactory │ DerbySchemaFactory │ ← Concrete Factories ├─────────────────────────┼─────────────────────────┤ │+ createTable() │+ createTable() │ │+ createColumn() │+ createColumn() │ └─────────────────────────┴─────────────────────────┘
Abstract Products
:public interface Table { public String toSQL(); }
public interface Column { public String toSQL(); }
Concrete Products
:public class OracleTable implements Table { public String toSQL() { return "OracleTable - toSQL"; } }
public class DerbyTable implements Table { public String toSQL() { return "DerbyTable - toSQL"; } }
public class OracleColumn implements Column { public String toSQL() { return "OracleColumn - toSQL"; } }
public class DerbyColumn implements Column { public String toSQL() { return "DerbyColumn - toSQL"; } }
Abstract Factory
:public interface SchemaFactory { public Table createTable(); public Column createColumn(); }
Concrete Factories
:public class OracleSchemaFactory implements SchemaFactory { public Table createTable() { return new OracleTable(); } public Column createColumn() { return new OracleColumn(); } }
public class DerbySchemaFactory implements SchemaFactory { public Table createTable() { return new DerbyTable(); } public Column createColumn() { return new DerbyColumn(); } }
public class FactoryMethodSchema { public static SchemaFactory getSchema(final String type) { if ("oracle".equalsIgnoreCase(type)) { return new OracleSchemaFactory(); } else if ("derby".equalsIgnoreCase(type)) { return new DerbySchemaFactory(); } return null; } }
public class AbstractFactoryPatternTest { public static void main(String[] args) { // Example 1: Oracle Database // Get the Oracle factory using the factory method helper final SchemaFactory oracleSchema = FactoryMethodSchema.getSchema("oracle"); // Create Oracle-specific products using the factory final Table oracleTable = oracleSchema.createTable(); final Column oracleColumn = oracleSchema.createColumn(); // Use the Oracle products (both belong to the same family) final String s_oracleTable = oracleTable.toSQL(); final String s_oracleColumn = oracleColumn.toSQL(); System.out.println("Oracle Database: " + s_oracleTable + " / " + s_oracleColumn); // Example 2: Derby Database // Get the Derby factory using the factory method helper final SchemaFactory derbySchema = FactoryMethodSchema.getSchema("derby"); // Create Derby-specific products using the factory final Table derbyTable = derbySchema.createTable(); final Column derbyColumn = derbySchema.createColumn(); // Use the Derby products (both belong to the same family) final String s_derbyTable = derbyTable.toSQL(); final String s_derbyColumn = derbyColumn.toSQL(); System.out.println("Derby Database: " + s_derbyTable + " / " + s_derbyColumn); } }