Monday, 18 August 2014

Abstract Factory Design Pattern Implementation

Abstract Factory Design Pattern


As we know AbstractFactory is responsible for creating factories of classes for the client as per request.
Any details regarding Abstract Factory design pattern please refer below:

http://www.javatechtipssharedbygaurav.com/2014/03/java-design-patterns-part-1.html


1. In this example, the abstract factory will be able to produce different database connection's.

Connection interface.

package com.gaurav.designpattern.abstractfactory;
/**
 * @author gaurav
 *
 */
public interface Connection {
public void dbConnection();

}

2. Concrete implementation  for Connection interface.

 a) package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class OracleConnection implements Connection{

@Override
public void dbConnection() {
System.out.println("Connecting to Oracle DB...");
}

}

b) package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class MySQLConnection implements Connection{

@Override
public void dbConnection() {
System.out.println("Connecting to MySQL DB...");
}

}

c) package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class MSAccessConnection implements Connection{

@Override
public void dbConnection() {
System.out.println("Connection to MS Access DB...");
}


3. Now we will define Abstract factories for creating different DB connection's
    package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public interface ConnectionFactory {
public Connection getConnection();
}

4. Concrete factory(OracleConnectionFactory) implementing Abstract Connection factory
    
package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class OracleConnectionFactory implements ConnectionFactory{

@Override
public Connection getConnection() {
return new OracleConnection();
}

}

5. Concrete factory(MySQLConnectionFactory) implementing Abstract Connection factory

package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class MySQLConnectionFactory implements ConnectionFactory{

@Override
public Connection getConnection() {
return new MySQLConnection();
}

}

6. Concrete factory(MSAccessConnectionFactory) implementing Abstract Connection factory

package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class MSAccessConnectionFactory implements ConnectionFactory{

@Override
public Connection getConnection() {
return new MSAccessConnection();
}

}

7. Now defining a demo application where we will use an injected factory implementation.
    
   package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class ConnectionDemoApplication {
private final ConnectionFactory conFactory;

 public ConnectionDemoApplication(ConnectionFactory connectionFactory) {
   this.conFactory = connectionFactory;
 }

 public void startConnection() {
   Connection con = conFactory.getConnection();
   con.dbConnection();
   // rest of the process which we want to execute after getting the connection...
 }
}

8. In the above code, we can find that using this design pattern application is completely decoupled. Both the connection factory and the database concrete implementation class are independent.

Testing the application
 package com.gaurav.designpattern.abstractfactory;

/**
 * @author gaurav
 *
 */
public class TestDBConnection {
/*An Enumeration type */
private enum DBType {
 ORACLE, MSACCESS, MYSQL, SQLSERVER 
 }
public static void main(String[] args) {
   DBType dbType = DBType.ORACLE;
   ConnectionFactory connectionFactory = 
            getConnectionFactory(dbType);

   ConnectionDemoApplication demoApplication = new ConnectionDemoApplication(connectionFactory);
   demoApplication.startConnection();
   
   dbType = DBType.MYSQL;
   connectionFactory = 
            getConnectionFactory(dbType);

   demoApplication = new ConnectionDemoApplication(connectionFactory);
   demoApplication.startConnection();
   
   dbType = DBType.MSACCESS;
   connectionFactory = 
            getConnectionFactory(dbType);

   demoApplication = new ConnectionDemoApplication(connectionFactory);
   demoApplication.startConnection();
   
   dbType = DBType.SQLSERVER;
   connectionFactory = 
            getConnectionFactory(dbType);

   demoApplication = new ConnectionDemoApplication(connectionFactory);
   demoApplication.startConnection();
 }

 private static ConnectionFactory getConnectionFactory(
     DBType dbType) {
   switch (dbType) {
   case ORACLE:
    return new OracleConnectionFactory();
   case MYSQL:
     return new MySQLConnectionFactory();
   default:
     return new MSAccessConnectionFactory();
   }
 }
}

Result :- 

Connecting to Oracle DB...
Connecting to MySQL DB...
Connection to MS Access DB...
Connection to MS Access DB...

Note:- Modularization is the key for the programming and when we will proceed towards the modularized approach, then we will be implementing this abstract Factory Pattern. 



No comments:

Post a Comment