From b532d70716a53f502e960faf659d419f7e111242 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Thu, 16 May 2019 16:05:27 +0530 Subject: [PATCH] Added Builder Design pattern implementation in Java and its test --- .../builderpattern/Desktop.java | 82 +++++++++++++++++++ .../builderpattern/DesktopBuilderTest.java | 32 ++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/main/java/com/designpatterns/builderpattern/Desktop.java create mode 100644 src/test/java/com/designpatterns/builderpattern/DesktopBuilderTest.java diff --git a/src/main/java/com/designpatterns/builderpattern/Desktop.java b/src/main/java/com/designpatterns/builderpattern/Desktop.java new file mode 100644 index 00000000..19943de3 --- /dev/null +++ b/src/main/java/com/designpatterns/builderpattern/Desktop.java @@ -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 Builder Pattern + */ + +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 + '\'' + + '}'; + } +} diff --git a/src/test/java/com/designpatterns/builderpattern/DesktopBuilderTest.java b/src/test/java/com/designpatterns/builderpattern/DesktopBuilderTest.java new file mode 100644 index 00000000..c57b1f97 --- /dev/null +++ b/src/test/java/com/designpatterns/builderpattern/DesktopBuilderTest.java @@ -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); + } + +}