Skip to content

Commit b532d70

Browse files
committed
Added Builder Design pattern implementation in Java and its test
1 parent 616d5cf commit b532d70

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package src.test.java.com.designpatterns.builderpattern;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.designpatterns.builderpattern.Desktop;
6+
7+
public class DesktopBuilderTest {
8+
private final String configOne = "Desktop{CPU='Intel i7', RAM='Corsair Vengeance 3000', isGraphicCardEnabled=true" +
9+
", operatingSystem='Windows 10', diskSizeGB=16, graphicCard='NVIDIA GTX 1080'}";
10+
private final String configTwo = "Desktop{CPU='Intel i5', RAM='HyperX Fury v5', isGraphicCardEnabled=true, " +
11+
"operatingSystem='Red Hat Enterprise', diskSizeGB=16, graphicCard='NVIDIA RTX 2080'}";
12+
13+
@Test
14+
public void testDesktopBuilder() {
15+
Desktop d1 = new Desktop.DesktopBuilder("Intel i7", "Corsair Vengeance 3000")
16+
.setDiskSizeGB(16)
17+
.setGraphicCard("NVIDIA GTX 1080")
18+
.setGraphicCardEnabled(true)
19+
.setOperatingSystem("Windows 10")
20+
.build();
21+
Assert.assertEquals(d1.toString(), configOne);
22+
23+
Desktop d2 = new Desktop.DesktopBuilder("Intel i5", "HyperX Fury v5")
24+
.setDiskSizeGB(16)
25+
.setGraphicCard("NVIDIA RTX 2080")
26+
.setGraphicCardEnabled(true)
27+
.setOperatingSystem("Red Hat Enterprise")
28+
.build();
29+
Assert.assertEquals(d2.toString(), configTwo);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)