Factory Design Pattern & Implementation Using Java

 


Introduction


Factory design pattern is very important and useful design patter and used in various industrious & technologies.

Factory design pattern is useful whenever we want to hide the object creation logic from the outside world. It is hiding object instantiation due to various reason like –

  • Object is complex to instantiate manually.
  • Object is having logic to instantiate object based on input parameter.
  • External API and don’t want to expose logic to outside.

Most of the time Factory design pattern return various object based on it’s input. In simple term, we can say that it is a collection of object.

Example: There is an Animal interface and it is implemented by many concrete classes like Mammal, Birds, Fishes, Reptiles etc. and we want to get the correct object by passing just input string. in this case we have to use Factory Design Pattern to get the correct object by passing string like –

Mammal: It will create Mammal object.

Birds: It will create Birds object.

Fishes: It will create Fishes object.


Example of Factory Design Pattern


Factory Design Pattern is having many live examples which we can use to demonstrate it but getting a Database object is more important and famous.

We have many Database like MySQL, SQL Server, Oracle and DB2 and want to get the correct connection object to connect with database based on input string.

We have to just pass String like MySQL to get MySQL database connection object and same for others.

Please find implementation using Java below –

KWDBConnection: This abstract class has common method implemented. For example we are having just constructor and description method. User can add any number of abstract or non abstract method in this class.

Please find code snippet below –

package com.dharma.designpatter.factory;
/**
 * This abstract class will have generic method implementation.
 * @author dknitk
 *
 */
public abstract class KWDBConnection {
    
    public KWDBConnection(){
        
    }
    public String description(){
        
        return "Generic";
    }

}

Now we will extends above class to implement more concrete classes like MySQL, Oracle, DB2, SQL Server etc.


KWDB2DBConnection: This concrete class will have DB2 related implementation.

package com.dharma.designpatter.factory;

/**
 * This concrete class will return DB2 Database Connection.
 *
 * @author dknitk
 *
 */
public class KWDB2DBConnection extends KWDBConnection {

    public KWDB2DBConnection() {

    }

    public String description() {

        return KWDBConnectionConstats.DB2;
    }

}


KWMySQLDBConnection: This concrete class will have MySQL Connection implementation.

package com.dharma.designpatter.factory;

/**
 * This concrete class will return MySQL Database Connection.
 *
 * @author dknitk
 *
 */
public class KWMySQLDBConnection extends KWDBConnection {

    public KWMySQLDBConnection() {

    }

    public String description() {

        return KWDBConnectionConstats.MYSQL;
    }

}

 


KWOracleDBConnection: This concrete class will have Oracle Connection implementation.

package com.dharma.designpatter.factory;

/**
 * This concrete class will return Oracle Database Connection.
 *
 * @author dknitk
 *
 */
public class KWOracleDBConnection extends KWDBConnection {

    public KWOracleDBConnection() {

    }

    public String description() {

        return KWDBConnectionConstats.ORACLE;
    }

}

KWSQLServerDBConnection: This concrete class will have implementation related to SQL Server.

package com.dharma.designpatter.factory;

/**
 * This concrete class will return SQL Server Database Connection.
 *
 * @author dknitk
 *
 */
public class KWSQLServerDBConnection extends KWDBConnection {

    public KWSQLServerDBConnection() {

    }

    public String description() {

        return KWDBConnectionConstats.SQLSERVER;
    }

}

 

Now, we will implement Factory class and method which helps to instantiate correct KWDBConnection object based on passed input string.


KWDBFactory: This class has factory implementation to instantiate KWDBFactory object.

package com.dharma.designpatter.factory;

/**
 * This class helps to invoke factory design pattern and will rturn correct
 * object based on input.
 *
 * @author dknitk
 *
 */
public class KWDBFactory {
    protected String type;

    public KWDBFactory(String type) {

        this.type = type;
    }

    /**
     * This method helps to create Connection and will return correct object
     * based on passed input type.
     *
     * @return
     */
    public KWDBConnection createConnection() {
        if (type.equals(KWDBConnectionConstats.ORACLE)) {
            return new KWOracleDBConnection();
        } else if (type.equals(KWDBConnectionConstats.SQLSERVER)) {
            return new KWSQLServerDBConnection();
        } else if (type.equals(KWDBConnectionConstats.DB2)) {
            return new KWDB2DBConnection();
        } else {
            return new KWMySQLDBConnection();
        }
    }

}

 


KWDBConnectionConstats: This interface will helps to keep all constants values like Oracle, MySql etc. We will use constants from this interface where ever applicable.

package com.dharma.designpatter.factory;

/**
 * This will have constants and will be used across.
 *
 * @author dknitk
 *
 */
public interface KWDBConnectionConstats {

    String ORACLE = "oracle";

    String DB2 = "DB2";

    String MYSQL = "MySQL";

    String SQLSERVER = "SQL Server";

}


We can test above written Factory Design Pattern using class which are having main method.

 

package com.dharma.designpatter.factory;

/**
 * This class helps to test KW connection object like Oracle, MySQL, DB2 etc.
 *
 * @author dknitk
 *
 */
public class KWConnectionImpl {

    /**
     * This method helps to start execution.
     *
     * @param args
     */
    public static void main(String[] args) {

        KWDBFactory kwdbFactory = new KWDBFactory(KWDBConnectionConstats.MYSQL);

        KWDBConnection kwDBConnection = kwdbFactory.createConnection();

        System.out.println("Object Created For " + kwDBConnection.description() + " Using Factory Design Pattern");
    }

}


Ouput: Please find output below –

Object Created For MySQL Using Factory Design Pattern


Please download project form below link and import in your eclipse to execute locally.

KWFactoryDesignPattern

Leave a Reply

Your email address will not be published. Required fields are marked *