|
| 1 | +package src.main.java.com.designpatterns.builderpattern; |
| 2 | + |
| 3 | +/** |
| 4 | + * The Builder is a design pattern designed to provide a flexible solution to various object creation problems in |
| 5 | + * object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex |
| 6 | + * object from its representation. |
| 7 | + * |
| 8 | + * @see <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder Pattern</a> |
| 9 | + */ |
| 10 | + |
| 11 | +public class Desktop { |
| 12 | + private String CPU; |
| 13 | + private String RAM; |
| 14 | + |
| 15 | + private boolean isGraphicCardEnabled; |
| 16 | + private String operatingSystem; |
| 17 | + private int diskSizeGB; |
| 18 | + private String graphicCard; |
| 19 | + |
| 20 | + private Desktop(DesktopBuilder builder) { |
| 21 | + this.CPU = builder.CPU; |
| 22 | + this.RAM = builder.RAM; |
| 23 | + this.isGraphicCardEnabled = builder.isGraphicCardEnabled; |
| 24 | + this.operatingSystem = builder.operatingSystem; |
| 25 | + this.diskSizeGB = builder.diskSizeGB; |
| 26 | + this.graphicCard = builder.graphicCard; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Builder class for the above Desktop class. Constructs the Desktop by invoking the Desktop class constructor and |
| 31 | + * allows access to set optional fields in the Desktop class. |
| 32 | + */ |
| 33 | + public static class DesktopBuilder { |
| 34 | + private String CPU; |
| 35 | + private String RAM; |
| 36 | + private boolean isGraphicCardEnabled; |
| 37 | + private String operatingSystem; |
| 38 | + private int diskSizeGB; |
| 39 | + private String graphicCard; |
| 40 | + |
| 41 | + public DesktopBuilder(String CPU, String RAM) { |
| 42 | + this.CPU = CPU; |
| 43 | + this.RAM = RAM; |
| 44 | + } |
| 45 | + |
| 46 | + public DesktopBuilder setGraphicCardEnabled(boolean graphicCardEnabled) { |
| 47 | + this.isGraphicCardEnabled = graphicCardEnabled; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public DesktopBuilder setOperatingSystem(String operatingSystem) { |
| 52 | + this.operatingSystem = operatingSystem; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public DesktopBuilder setDiskSizeGB(int diskSize) { |
| 57 | + this.diskSizeGB = diskSize; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + public DesktopBuilder setGraphicCard(String graphicCard) { |
| 62 | + this.graphicCard = graphicCard; |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + public Desktop build() { |
| 67 | + return new Desktop(this); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String toString() { |
| 73 | + return "Desktop{" + |
| 74 | + "CPU='" + CPU + '\'' + |
| 75 | + ", RAM='" + RAM + '\'' + |
| 76 | + ", isGraphicCardEnabled=" + isGraphicCardEnabled + |
| 77 | + ", operatingSystem='" + operatingSystem + '\'' + |
| 78 | + ", diskSizeGB=" + diskSizeGB + |
| 79 | + ", graphicCard='" + graphicCard + '\'' + |
| 80 | + '}'; |
| 81 | + } |
| 82 | +} |
0 commit comments