Implement the Computer problem using the Abstract Factory pattern. public abstra
ID: 3592088 • Letter: I
Question
Implement the Computer problem using the Abstract Factory pattern.
public abstract class Computer {
public abstract String getRAM();
public abstract String getCPU();
@Override
public String toString(){
return "RAM= "+this.getRAM()+", CPU="+this.getCPU();
}
}
public class PC extends Computer
{
private String ram;
private String cpu;
public PC(String ram, String cpu)
{
}
//Override
public String getRAM() {
}
//Override
public String getCPU() {
}
}
public class Server extends Computer
{
.........
}
public interface ComputerAbstractFactory {
public Computer createComputer();
}
public class PCFactory implements ComputerAbstractFactory
{
private String ram;
private String cpu;
public PCFactory(String ram, String cpu)
{
}
//Override
public Computer createComputer()
{
...........
}
}
public class ServerFactory implements ComputerAbstractFactory
{
............
}
public class ComputerFactory {
public static Computer getComputer(ComputerAbstractFactory factory)
{
.....................
}
}
Create a class Test to test the class Computer Factory.
Explanation / Answer
Computer.java
public abstract class Computer {
public abstract String getRAM();
public abstract String getCPU();
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", CPU=" + this.getCPU();
}
}
_____________
PC.java
public class PC extends Computer {
private String ram;
private String cpu;
public PC(String ram, String cpu) {
super();
this.ram = ram;
this.cpu = cpu;
}
@Override
public String getRAM() {
return ram;
}
@Override
public String getCPU() {
return cpu;
}
}
_______________
Server.java
public class Server extends Computer {
private String ram;
private String cpu;
public Server(String ram, String cpu) {
this.ram = ram;
this.cpu = cpu;
}
@Override
public String getRAM() {
return ram;
}
@Override
public String getCPU() {
return cpu;
}
}
________________
ComputerAbstractFactory.java
public interface ComputerAbstractFactory {
public Computer createComputer();
}
_________________
PCFactory.java
public class PCFactory implements ComputerAbstractFactory {
private String ram;
private String cpu;
public PCFactory(String ram, String cpu) {
super();
this.ram = ram;
this.cpu = cpu;
}
@Override
public Computer createComputer() {
return new PC(ram, cpu);
}
}
_________________
ServerFactory.java
public class ServerFactory implements ComputerAbstractFactory {
private String ram;
private String cpu;
public ServerFactory(String ram, String cpu) {
super();
this.ram = ram;
this.cpu = cpu;
}
@Override
public Computer createComputer() {
return new Server(ram, cpu);
}
}
_________________
ComputerFactory.java
public class ComputerFactory {
public static Computer getComputer(ComputerAbstractFactory factory){
return factory.createComputer();
}
}
_________________
Test.java
public class Test {
public static void main(String[] args) {
Computer comp=ComputerFactory.getComputer(new PCFactory("4 GB"," 2.4 Ghz"));
Computer server=ComputerFactory.getComputer(new ServerFactory("32 GB","2.9 GHz"));
System.out.println("Computer Info :"+comp);
System.out.println("Server Info :"+server);
}
}
_________________
Output:
Computer Info :RAM= 4 GB, CPU= 2.4 Ghz
Server Info :RAM= 32 GB, CPU=2.9 GHz
_____________Could you rate me well.Plz .Thank You