Finally we are now ready to see the Solution of our problem. The Abstract factory can return the exact type of Computer by calling the required factory of the type required. We need to call the implementations of the Server, WorkStation, Notebook and Tablet as factories since the Client always wants a Computer and the Abstract Factory is unaware of the Components which make up the Computer.
First
Next
Finally we are ready to run the tester through a command script.
The Command Script
At last the output file
First
ComputerAbstractFactory.
package com.company.self.study.core.java;
/**
*
* Abstract Factory Pattern Example
*
* @author administrator
*
*/
public class ComputerAbstractFactory {
private static ComputerAbstractFactory instance = new ComputerAbstractFactory();
/**
*
*
* @return
*/
public static ComputerAbstractFactory getInstance() {
return instance;
}
/**
*
*
*
* @param type
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public Computer getComputer(String type) {
Computer computer = null;
Component processorCore = null;
Component hardDisk = null;
Component memory = null;
if ("Server".equals(type)) {
computer = new Server();// Servers are always default
}
else if ("WorkStation".equals(type)) {
computer = new WorkStation();// we need a default workstation.
}
else if ("NoteBook".equals(type)) {
computer = new NoteBook();
}
else if ("Tablet".equals(type)) {
computer = new Tablet();
}
else {
System.out.println(" this " + type + " is not current available ");
throw new UnsupportedOperationException(" this " + type + " is not current available ");
}
return computer;
}
}
Next
ComputerAbstractFactoryTester.
package com.company.self.study.core.java;
/**
*
* Abstract Factory Pattern Example
*
* @author administrator
*
*/
public class ComputerAbstractFactoryTester {
/**
*
* Client Class is unaware of the names of the Class Implementations or even of the component classes that are used to build.
* It is a coincidence that the type and the implementations are the same.
*
* @param args
*/ public static void main(String[] args) {
System.out.println(" ComputerAbstractFactoryTester start ");
Computer c1 = ComputerAbstractFactory.getInstance().getComputer("Server");
Computer c2 = ComputerAbstractFactory.getInstance().getComputer("WorkStation");
Computer c3 = ComputerAbstractFactory.getInstance().getComputer("NoteBook");
Computer c4 = ComputerAbstractFactory.getInstance().getComputer("Tablet");
System.out.println(" c1 " + c1);
System.out.println(" c2 " + c2);
System.out.println(" c3 " + c3);
System.out.println(" c4 " + c4);
System.out.println(" ComputerAbstractFactoryTester end ");
}
}
Finally we are ready to run the tester through a command script.
The Command Script
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\ClassTemplate.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\Component.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\Computer.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\ProcessorCore.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\HardDisk.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\Memory.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\Server.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\WorkStation.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\NoteBook.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\Tablet.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\ComputerAbstractFactory.java
"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\ComputerAbstractFactoryTester.java
java -classpath bin com.company.self.study.core.java.ComputerAbstractFactoryTester > Output_ComputerAbstractFactoryTester.txt
At last the output file
ComputerAbstractFactoryTester start
c1 server with DecaCore 40 GB RAM 100 TB physical disk
c2 workstation with QuadCore 8 GB RAM 1 TB physical disk
c3 notebook with QuadCore 4 GB RAM 100 GB physical disk
c4 tablet with DualCore 2 GB RAM 40 GB physical disk
ComputerAbstractFactoryTester end
No comments:
Post a Comment