Cricinfo Live Scores

Saturday, February 2, 2008

Design Pattern (GOF): Creational Pattern: Abstract Factory


Definition

Provides one level of interface higher than the factory pattern. It is used to return one of several factories.

Where to use & benefits

  • Creates families of related or dependent objects like Kit.
  • Provides a class library of products, exposing interface not implementation.
  • Related patterns include
    • Factory Method, which is often implemented with an abstract factory.
    • Singleton, which is often implemented with an abstract factory.
    • Prototype, which is often implemented with an abstract factory.
    • Facade, which is often used with an abstract factory by providing an interface for creating implementing class.

Example

Suppose you need to write a program to show data in two different places. Let's say from a local or a remote database. You need to make a connection to a database before working on the data. In this case, you have two choices, local or remote. You may use abstract factory design pattern to design the interface in the following way:

class DataInfo {}
interface Local {
  DataInfo[] loadDB(String filename);
}
 
interface Remote extends Local{
  void connect2WWW(String url);
}
 
class LocalMode implements Local {
  public DataInfo[] loadDB(String name) {
    System.out.print("Load from a local database ");
    return null;
  }
}
 
class RemoteMode implements Remote {
  public void connect2WWW(String url) {
    System.out.println("Connect to a remote site ");
  }
  public DataInfo[] loadDB(String name) {
     System.out.println("Load from a remote database ");
     return null;
  }
}
 
// The Abstract Factory
interface ConnectionFactory {
  Local getLocalConnection();
  Remote getRemoteConnection();
}
 
class DataManager implements ConnectionFactory {
    boolean local = false;
    DataInfo[] data;
    //...
    public Local getLocalConnection() {
        return new LocalMode();
    }
    public Remote getRemoteConnection() {
        return new RemoteMode();
    }
    public  void loadData() {
         if(local){
            Local conn = getLocalConnection();
            data = conn.loadDB("db.db");
         }else {
            Remote conn = getRemoteConnection();
            conn.connect2WWW("www.some.where.com");
            data = conn.loadDB("db.db");
         }
         
     }
         // work on data 
   
    public void setConnection(boolean b) {
       local = b;
    }
}
 
//Use the following Test class to test the above classes
class Test {
    public static void main(String[] args) {
        DataManager dm = new DataManager();
        DataInfo[] di = null;
        String dbFileName = "db.db";
        if (args.length == 1) {
            //assume local is set to true
            dm.setConnection(true);
            LocalMode lm = (LocalMode)dm.getLocalConnection(); 
            di = lm.loadDB(dbFileName); 
        } else {  
           //Note: dm.local = false is default setting
           RemoteMode rm = (RemoteMode)dm.getRemoteConnection();
           rm.connect2WWW("www.javacamp.org/db/"); 
// change it to localhost
 
           di = rm.loadDB(dbFileName); 
        }
        //use one set of methods to deal with loaded data.
        //You don't need to worry about connection from this point.
        //Like di.find(), di.search() etc.
    }
}

Such design is often used in SCJD project assignment. If you have a multiple places to load data, you just add more methods in the connection interface without altering other structure, or add a location variable in.

Author:
-----------------------
Kazi Masudul Alam
Software Engineer