package com.builder;
public class Cpu {
private String name;
public Cpu(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void work() {
System.out.println(name + "is working");
}
}
package com.builder;
public class Disk {
private String name;
public Disk(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void work() {
System.out.println(name + "is working");
}
}
package com.builder;
public class Memory {
private String name;
public Memory(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void work() {
System.out.println(name + "is working");
}
}
package com.builder;
public class Computer {
private String name;
private Cpu cpu;
private Disk disk;
private Memory memory;
public Computer() {
super();
}
public Computer(String name, Cpu cpu, Disk disk, Memory memory) {
super();
this.name = name;
this.cpu = cpu;
this.disk = disk;
this.memory = memory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cpu getCpu() {
return cpu;
}
public void setCpu(Cpu cpu) {
this.cpu = cpu;
}
public Disk getDisk() {
return disk;
}
public void setDisk(Disk disk) {
this.disk = disk;
}
public Memory getMemory() {
return memory;
}
public void setMemory(Memory memory) {
this.memory = memory;
}
public void work() {
cpu.work();
disk.work();
memory.work();
}
}
package com.builder;
public interface ComputerBuilder {
void createCpu();
void createDisk();
void createMemory();
Computer createComputer();
}
package com.builder;
public class MiBuilder implements ComputerBuilder {
private Computer computer = new Computer();
@Override
public void createCpu() {
computer.setCpu(new Cpu("intel i9"));
}
@Override
public void createDisk() {
computer.setDisk(new Disk("2T soild"));
}
@Override
public void createMemory() {
computer.setMemory(new Memory("16G Memory"));
}
@Override
public Computer createComputer() {
computer.setName("Mi air13");
return computer;
}
}
package com.builder;
public class Worker {
public static Computer createComputer(ComputerBuilder cb) {
cb.createCpu();
cb.createDisk();
cb.createMemory();
return cb.createComputer();
}
}
package com.builder;
public class Test {
public static void main(String[] args) {
Computer computer = Worker.createComputer(new MiBuilder());
computer.work();
}
}