Reputation: 13
I am learning design patterns in Java from an online video tutorial. I have learned the builder design pattern and I'm trying to implement what it does, but for me its showing some errors. Help me to resolve them.
public class Phone {
private String model;
private String os;
private int ram;
private double screensize;
private int battery;
public Phone(String model, String os, int ram, double screensize, int battery) {
super();
this.model = model;
this.os = os;
this.ram = ram;
this.screensize = screensize;
this.battery = battery;
}
@Override
public String toString() {
return "Phone [model=" + model + ", os=" + os + ", ram=" + ram + ", screensize=" + screensize + ", battery="
+ battery + "]";
}
}
public class Phonebuilder {
private String model;
private String os;
private int ram;
private double screensize;
private int battery;
public Phonebuilder setModel(String model) {
this.model = model;
return this;
}
public Phonebuilder setOs(String os) {
this.os = os;
return this;
}
public Phonebuilder setRam(int ram) {
this.ram = ram;
return this;
}
public Phonebuilder setScreensize(double screensize) {
this.screensize = screensize;
return this;
}
public Phonebuilder setBattery(int battery) {
this.battery = battery;
return this;
}
public Phone getphone () {
return new Phone(model, os, ram, screensize, battery);
}
}
public class Test {
public static void main(String[] args) {
//It showing error in the below line like type mismatch.
Phone p = new Phonebuilder().setBattery(9000).setModel("M31").setOs("Android");
System.out.println(p);
}
}
Upvotes: 0
Views: 59
Reputation: 2678
I don't think that it's good idea to provide public constructor in Phone class while using builder pattern. The reason to use builder pattern is to solve the problem associated with large number of optional parameter. Below is the alternative approach for your example (it's based on Item 2 'Consider a builder when faced with many constructor parameters' from book Effective Java ). This approach has one advantage is that phone class is immutable i.e. once created it cannot be changed.
public class Phone {
private final String model;
private final String os;
private final int ram;
private final double screensize;
private final int battery;
private Phone(Builder builder) {
model = builder.model;
os = builder.os;
ram = builder.ram;
screensize = builder.screensize;
battery = builder.battery;
}
public static class Builder {
private String model;
private String os;
private int ram;
private double screensize;
private int battery;
public Builder calories(String val) {
model = val;
return this;
}
public Builder os(String val) {
os = val;
return this;
}
public Builder ram(int val) {
ram = val;
return this;
}
public Builder screensize(double val) {
screensize = val;
return this;
}
public Builder battery(int val) {
battery = val;
return this;
}
public Phone build() {
return new Phone(this);
}
}
}
Create phone call builder as -
Phone phone =
new Phone.Builder()
.battery(1)
.calories("100")
.os("windows")
.screensize(15)
.battery(2)
.build();
Upvotes: 1
Reputation: 862
The last method invoked setOs returns Phonebuilder but you are assigning it to Phone. I guess the intent is to add getPhone after setting all parameters.
Upvotes: 2
Reputation: 17880
You are missing the method call to build the object (getphone()
)
Phone p = new Phonebuilder()
.setBattery(9000)
.setModel("M31")
.setOs("Android")
.getphone(); //This builds and returns a Phone instance
Sidenote: IMO buildPhone
might be a better name.
Upvotes: 2