Wednesday, April 13, 2011

Design Pattern : Abstract Factory Pattern

Creational Patterns - Abstract Factory Pattern ( Sets of methods to make various objects )

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.

When to use ?
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.


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 RAM and Processor. The different types of computers are PC and Server.

So, here we have an abstract base class Computer.

/**
*
* @author R.Amirtharaj
*/
public abstract class Computer {
public abstract Parts getRAM();
public abstract Parts getProcessor();
}


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.

/**
*
* @author R.Amirtharaj
*/
public class Parts {

private String specification;

public Parts(String specification) {
this.specification = specification;
}

public String getSpecification() {
return specification;
}
}

And now lets go to the sub-classes of Computer.

/**
*
* @author R.Amirtharaj
*/
public class PC extends Computer {

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

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



/**
*
* @author R.Amirtharaj
*/
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
* Server
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Intel P 4");
}
}

Now let’s have a look at the Abstract factory which returns a factory “Computer”

/**
*
* @author R.Amirtharaj
*/
public class Demo {

public static void main(String arg[])
{
Demo abstarcfactorydemo = new Demo();
Computer pc = abstarcfactorydemo.getComputer("PC");
System.out.println("PC Specification ");
System.out.println("RAM = "+pc.getRAM().getSpecification());
System.out.println("Processor ="+pc.getProcessor().getSpecification());

Computer server = abstarcfactorydemo.getComputer("Server");
System.out.println("Server Specification ");
System.out.println("RAM = "+server.getRAM().getSpecification());
System.out.println("Processor ="+server.getProcessor().getSpecification());
}

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

No comments:

Post a Comment