Abstract Factory Pattern

Abstract Factory

posted Feb 17, 2011, 10:02 PM by Ky Vo   [ updated Feb 17, 2011, 10:03 PM ]

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.
  • Needs to isolate concrete classes from their super classes.
  • A system needs independent of how its products are created, composed, and represented.
  • Try to enforce a constraint.
  • An alternative to Facade to hide platform-specific classes
  • Easily extensible to a system or a family
  • 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/");
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.
}
}

 C:\   Command Prompt

 
C:\> java Test
Connect to a remote site
Load from a remote database

 C:\   Command Prompt

 
C:\> java Test local
Load from a local database

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.

Creational Patterns - Abstract Factory Pattern

posted Feb 17, 2011, 10:02 PM by Ky Vo   [ updated Feb 17, 2011, 10:02 PM ]

This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.

Let’s understand this pattern with the help of an example.

Suppose we need to get the specification of various parts of a computer based on which work the computer will be used for.

The different parts of computer are, say Monitor, RAM and Processor. The different types of computers are PC, Workstation and Server.

So, here we have an abstract base class Computer.

package creational.abstractfactory;

public abstract class Computer {

  /**
* Abstract method, returns the Parts ideal for
* Server
* @return Parts
*/
public abstract Parts getRAM();

/**
* Abstract method, returns the Parts ideal for
* Workstation
* @return Parts
*/
public abstract Parts getProcessor();

/**
* Abstract method, returns the Parts ideal for
* PC
* @return Parts
*/
public abstract Parts getMonitor();

}// End of class

This class, as you can see, has three methods all returning different parts of computer. They all return a method called Parts. The specification of Parts will be different for different types of computers. Let’s have a look at the class Parts.

package creational.abstractfactory;

public class Parts {

  /**
* specification of Part of Computer, String
*/
public String specification;

/**
* Constructor sets the name of OS
* @param specification of Part of Computer
*/
public Parts(String specification) {
this.specification = specification;
}

/**
* Returns the name of the part of Computer
*
* @return specification of Part of Computer, String
*/
public String getSpecification() {
return specification;
}

}// End of class

And now lets go to the sub-classes of Computer. They are PC, Workstation and Server.

package creational.abstractfactory;

public class PC extends Computer {

  /**
* Method over-ridden from Computer, returns the Parts ideal for
* Server
* @return Parts
*/
public Parts getRAM() {
return new Parts("512 MB");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* Workstation
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Celeron");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* PC
* @return Parts
*/
public Parts getMonitor() {
return new Parts("15 inches");
}

}// End of class

package creational.abstractfactory;

public class Workstation extends Computer {
  /**
* Method over-ridden from Computer, returns the Parts ideal for
* Server
* @return Parts
*/
public Parts getRAM() {
return new Parts("1 GB");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* Workstation
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Intel P 3");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* PC
* @return Parts
*/
public Parts getMonitor() {
return new Parts("19 inches");
}

}// End of class

package creational.abstractfactory;

public class Server extends Computer{

  /**
* Method over-ridden from Computer, returns the Parts ideal for
* Server
* @return Parts
*/
public Parts getRAM() {
return new Parts("4 GB");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* Workstation
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Intel P 4");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* PC
* @return Parts
*/
public Parts getMonitor() {
return new Parts("17 inches");
}

}// End of class

Now let’s have a look at the Abstract factory which returns a factory “Computer”. We call the class ComputerType.

package creational.abstractfactory;

/**
* This is the computer abstract factory which returns one
* of the three types of computers.
*
*/
public class ComputerType {

  private Computer comp;

public static void main(String[] args) {

    ComputerType type = new ComputerType();

Computer computer = type.getComputer("Server");
System.out.println("Monitor: "+computer.getMonitor().getSpecification());
System.out.println("RAM: "+computer.getRAM().getSpecification());
System.out.println("Processor: "+computer.getProcessor().getSpecification());

  }   
   

/**
* Returns a computer for a type
*
* @param computerType String, PC / Workstation / Server
* @return Computer
*/

  public Computer getComputer(String computerType) {
    if (computerType.equals("PC"))
comp = new PC();
else if(computerType.equals("Workstation"))
comp = new Workstation();
else if(computerType.equals("Server"))
comp = new Server();

return comp;

  }  
}// End of class

Running this class gives the output as this:

Monitor: 17 inches
RAM: 4 GB
Processor: Intel P 4.

When to use Abstract Factory Pattern?
One of the main advantages of Abstract Factory Pattern is that it isolates the concrete classes that are generated. The names of actual implementing classes are not needed to be known at the client side. Because of the isolation, you can change the implementation from one factory to another.

1-2 of 2