Magazine
 
Design Pattern
 

public Parts getRAM() {
return new Parts(“256 MB”);
}
public Parts getProcessor() {
return new Parts(“Pentium3”);
}
public Parts getHarddisk() {
return new Parts(“40GB”);
}}
package creational.abstractfactory;
public class Workstation extends Computer {
public Parts getRAM() {
return new Parts(“1 GB”);
}
public Parts getProcessor() {
return new Parts(“Pentium4”);
}
public Parts getHarddisk() {
return new Parts(“80GB”);
}}
package creational.abstractfactory;
public class Server extends Computer{
public Parts getRAM() {
return new Parts(“2 GB”);
}
public Parts getProcessor() {
return new Parts(“DualCore”);
}
public Parts getHarddisk() {
return new Parts(“160GB”);
}}
package creational.abstractfactory;
public class CatagoryType {
private Computer comp;
public static void main(String[] args) {
CatagoryType type = new CatagoryrType();
Computer computer = type.getComputer(“Server”);
System.out.println(“Harddisk:
“+computer.getHarddisk().getConfiguration());
System.out.println(“RAM:
“+computer.getRAM().getConfiguration());
System.out.println(“Processor:
“+computer.getProcessor().getConfiguration());
}

 

public Computer getComputer(String catagoryType) {
if (catagoryType.equals(“PC”))
comp = new PC();
else if(catogoryType.equals(“Workstation”))
comp = new Workstation();
else if(catagoryType.equals(“Server”))
comp = new Server();
return comp;
}}

The above class gives the output like this:
Harddisk: 160GB
RAM: 2 GB
Processor: DualCore.

 

August 2007 | Java Jazz Up | 27
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40           Download PDF