Created
May 29, 2020 14:31
-
-
Save raviyasas/66ecbee2cd1bb0de0bd8b21d196e2479 to your computer and use it in GitHub Desktop.
Builder pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.designPatterns.builder1; | |
public class BuilderApp { | |
public static void main(String[] args) { | |
Phone iphone = new PhoneBuilder().setModel("Apple").setOs("IOS").buildPhone(); | |
System.out.println(iphone); | |
Phone androidPhone = new PhoneBuilder().setModel("Nokia").setOs("Android").setCamera("32MP").buildPhone(); | |
System.out.println(androidPhone); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.designPatterns.builder1; | |
public class Phone { | |
private String model; | |
private String os; | |
private Integer price; | |
private String camera; | |
public Phone(String model, String os, Integer price, String camera) { | |
super(); | |
this.model = model; | |
this.os = os; | |
this.price = price; | |
this.camera = camera; | |
} | |
@Override | |
public String toString() { | |
return "Phone{" + | |
"model='" + model + '\'' + | |
", os='" + os + '\'' + | |
", price=" + price + | |
", camera='" + camera + '\'' + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.designPatterns.builder1; | |
public class PhoneBuilder { | |
private String model; | |
private String os; | |
private Integer price; | |
private String camera; | |
public PhoneBuilder setModel(String model) { | |
this.model = model; | |
return this; | |
} | |
public PhoneBuilder setOs(String os) { | |
this.os = os; | |
return this; | |
} | |
public PhoneBuilder setPrice(Integer price) { | |
this.price = price; | |
return this; | |
} | |
public PhoneBuilder setCamera(String camera) { | |
this.camera = camera; | |
return this; | |
} | |
public Phone buildPhone() { | |
return new Phone(model, os, price, camera); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment