How to create Factory Pattern in Java

  • 03 November 2016
  • ADM

 

How to create Factory Pattern in Java - images/logos/oopjava.jpg

 

The Factory Design Pattern is a creational pattern and provides a better way of creating an object without specifying the exact class that need to be created. The object is created without exposing the creation logic to the client and will refer to the new object using a interface.

Example

A good example where the Factory Design Pattern can be used if is needed to implement an generic solution for creating a database table regardless the database engine.

The factory design pattern relies on inheritance, as the object creation is delegated to the sub-classes that implement the factory method to create objects.

For this, will need an interface to define the behaviour for all classes. Will take a simple example with only one method.

package com.admfactory.factory;

public interface Table {
    public boolean create();
}

And some concrete classes to implement the interface. These classes will implement the create method with a simple console output, just for testing.

For example will choose a few of the main database engines available:

  • MSSQL
  • MySQL
  • Oracle
  • PostgreeSQL
package com.admfactory.factory;

public class MSSQLTable implements Table {

    @Override
    public boolean create() {
	System.out.println("MSSQLTable::create() was called.");
	return true;
    }
}
package com.admfactory.factory;

public class MySQLTable implements Table {

    @Override
    public boolean create() {
	System.out.println("MySQLTable::create() was called.");
	return true;
    }
}
package com.admfactory.factory;

public class OracleTable implements Table {

    @Override
    public boolean create() {
	System.out.println("OracleTable::create() was called.");
	return true;
    }
}
package com.admfactory.factory;

public class PostgreSQLTable implements Table {

    @Override
    public boolean create() {
	System.out.println("PostgreSQLTable::create() was called.");
	return true;
    }
}

For database type can be used an enumeration, but in the same way can be used any other type.

package com.admfactory.factory;

public enum DatabaseType {
    MySQL, MSSQL, Oracle, PostgreSQL
}
The central part of the Factory Design Pattern is the factory class that will create the new object based on the DatabaseType parameter received, by calling the specific constructor corresponding for each database type. e.g. for DatabaseType.MySQL will create the MYSQLTable object.
package com.admfactory.factory;

public class TableFactory {
    public Table getTable(DatabaseType type) {
	if (type == DatabaseType.MySQL) {
	    return new MySQLTable();
	} else if (type == DatabaseType.MSSQL) {
	    return new MSSQLTable();
	} else if (type == DatabaseType.PostgreSQL) {
	    return new PostgreSQLTable();
	} else if (type == DatabaseType.Oracle) {
	    return new OracleTable();
	} else {
	    return null;
	}
    }
}

Usage

Here is a simple example how the factory class is being used.

package com.admfactory.factory;

public class FactoryExample {
    public static void main(String[] args) {
	System.out.println("Factory Design Pattern Example");
	System.out.println();
	TableFactory factory = new TableFactory();

	Table table1 = factory.getTable(DatabaseType.MSSQL);
	table1.create();

	Table table2 = factory.getTable(DatabaseType.MySQL);
	table2.create();

	Table table3 = factory.getTable(DatabaseType.PostgreSQL);

	table3.create();

	Table table4 = factory.getTable(DatabaseType.Oracle);

	table4.create();
    }
}

Output

In the console output can be observed that even if all 4 objects type is Table the factory class will return the specific implementation type based on the parameter.

Factory Design Pattern Example

MSSQLTable::create() was called.
MySQLTable::create() was called.
PostgreSQLTable::create() was called.
OracleTable::create() was called.

 

References