Merge pull request #757 from abhijay94/Development

Added Builder Design pattern implementation in Java and its test
This commit is contained in:
Libin Yang 2019-05-17 18:14:50 +08:00 committed by GitHub
commit 6458e31c45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,82 @@
package src.main.java.com.designpatterns.builderpattern;
/**
* The Builder is a design pattern designed to provide a flexible solution to various object creation problems in
* object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex
* object from its representation.
*
* @see <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder Pattern</a>
*/
public class Desktop {
private String CPU;
private String RAM;
private boolean isGraphicCardEnabled;
private String operatingSystem;
private int diskSizeGB;
private String graphicCard;
private Desktop(DesktopBuilder builder) {
this.CPU = builder.CPU;
this.RAM = builder.RAM;
this.isGraphicCardEnabled = builder.isGraphicCardEnabled;
this.operatingSystem = builder.operatingSystem;
this.diskSizeGB = builder.diskSizeGB;
this.graphicCard = builder.graphicCard;
}
/**
* Builder class for the above Desktop class. Constructs the Desktop by invoking the Desktop class constructor and
* allows access to set optional fields in the Desktop class.
*/
public static class DesktopBuilder {
private String CPU;
private String RAM;
private boolean isGraphicCardEnabled;
private String operatingSystem;
private int diskSizeGB;
private String graphicCard;
public DesktopBuilder(String CPU, String RAM) {
this.CPU = CPU;
this.RAM = RAM;
}
public DesktopBuilder setGraphicCardEnabled(boolean graphicCardEnabled) {
this.isGraphicCardEnabled = graphicCardEnabled;
return this;
}
public DesktopBuilder setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
return this;
}
public DesktopBuilder setDiskSizeGB(int diskSize) {
this.diskSizeGB = diskSize;
return this;
}
public DesktopBuilder setGraphicCard(String graphicCard) {
this.graphicCard = graphicCard;
return this;
}
public Desktop build() {
return new Desktop(this);
}
}
@Override
public String toString() {
return "Desktop{" +
"CPU='" + CPU + '\'' +
", RAM='" + RAM + '\'' +
", isGraphicCardEnabled=" + isGraphicCardEnabled +
", operatingSystem='" + operatingSystem + '\'' +
", diskSizeGB=" + diskSizeGB +
", graphicCard='" + graphicCard + '\'' +
'}';
}
}

View File

@ -0,0 +1,32 @@
package src.test.java.com.designpatterns.builderpattern;
import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.designpatterns.builderpattern.Desktop;
public class DesktopBuilderTest {
private final String configOne = "Desktop{CPU='Intel i7', RAM='Corsair Vengeance 3000', isGraphicCardEnabled=true" +
", operatingSystem='Windows 10', diskSizeGB=16, graphicCard='NVIDIA GTX 1080'}";
private final String configTwo = "Desktop{CPU='Intel i5', RAM='HyperX Fury v5', isGraphicCardEnabled=true, " +
"operatingSystem='Red Hat Enterprise', diskSizeGB=16, graphicCard='NVIDIA RTX 2080'}";
@Test
public void testDesktopBuilder() {
Desktop d1 = new Desktop.DesktopBuilder("Intel i7", "Corsair Vengeance 3000")
.setDiskSizeGB(16)
.setGraphicCard("NVIDIA GTX 1080")
.setGraphicCardEnabled(true)
.setOperatingSystem("Windows 10")
.build();
Assert.assertEquals(d1.toString(), configOne);
Desktop d2 = new Desktop.DesktopBuilder("Intel i5", "HyperX Fury v5")
.setDiskSizeGB(16)
.setGraphicCard("NVIDIA RTX 2080")
.setGraphicCardEnabled(true)
.setOperatingSystem("Red Hat Enterprise")
.build();
Assert.assertEquals(d2.toString(), configTwo);
}
}