In: Computer Science
2 Builder Pattern
java
-set up builder pattern to run this
RobotBuilder builder = new RobotBuilder();
builder.setRam(1000);
builder.setStorage(10000);
builder.setWheels(2);
builder.setName("Robo Ron");
Robot robot = builder.build();
builder = new RobotBuilder();
builder.setName("Robo Rene");
builder.setRam(500);
builder.setStorage(15000);
Robot robot2 = builder.build();
class Robot{
private int ram;
private int storage;
private int wheels;
private String name;
/**
* @return the ram
*/
public int getRam() {
return ram;
}
/**
* @param aRam the ram to set
*/
public void setRam(int aRam) {
ram = aRam;
}
/**
* @return the storage
*/
public int getStorage() {
return storage;
}
/**
* @param aStorage the storage to set
*/
public void setStorage(int aStorage) {
storage = aStorage;
}
/**
* @return the weheel
*/
public int getWheels() {
return wheels;
}
/**
* @param aWeheel the weheel to set
*/
public void setWheels(int aWeheel) {
wheels = aWeheel;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param aName the name to set
*/
public void setName(String aName) {
name = aName;
}
/**
* @param aRam
* @param aStorage
* @param aWheels
* @param aName
*/
public Robot(int aRam, int aStorage, int aWheels,
String aName) {
super();
ram = aRam;
storage = aStorage;
wheels = aWheels;
name = aName;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Robot [ram=" + ram + ",
storage=" + storage + ", wheels=" + wheels + ", name=" + name +
"]";
}
}
class RobotBuilder{
private int ram;
private int storage;
private int wheels;
private String name;
/**
* @return the ram
*/
public int getRam() {
return ram;
}
/**
* @param aRam the ram to set
*/
public void setRam(int aRam) {
ram = aRam;
}
/**
* @return the storage
*/
public int getStorage() {
return storage;
}
/**
* @param aStorage the storage to set
*/
public void setStorage(int aStorage) {
storage = aStorage;
}
/**
* @return the weheel
*/
public int getWheels() {
return wheels;
}
/**
* @param aWeheel the weheel to set
*/
public void setWheels(int aWeheel) {
wheels = aWeheel;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param aName the name to set
*/
public void setName(String aName) {
name = aName;
}
public Robot build(){
return new
Robot(ram,storage,wheels,name);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RobotBuilder [ram=" + ram +
", storage=" + storage + ", wheels=" + wheels + ", name=" + name +
"]";
}
}
public class TestBuilder {
public static void main(String[] args) {
RobotBuilder builder = new
RobotBuilder();
builder.setRam(1000);
builder.setStorage(10000);
builder.setWheels(2);
builder.setName("Robo Ron");
Robot robot = builder.build();
builder = new
RobotBuilder();
builder.setName("Robo Rene");
builder.setRam(500);
builder.setStorage(15000);
Robot robot2 =
builder.build();
System.out.println(robot);
System.out.println(robot2);
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me