Edit Page

Builder

Building an Ice Cream Example

IceCream.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class IceCream {

    // Required IceCream fields
    private String type; // cup or cone
    private String size; // regular, medium, large
    private String flavor; // chocolate, strawberry, etc.

    // optional fields
    private String[] toppings;
    private String sauce;

    private IceCream(Builder builder) {
        this.type = builder.type;
        this.size = builder.size;
        this.flavor = builder.flavor;
        this.toppings = builder.toppings;
        this.sauce = builder.sauce;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Ice cream:\n " +
                this.type + " \t" + this.size + "\t" + this.flavor);
        if (this.sauce != "") {
            sb.append("\nSauce:\t" + sauce);
        }
        if (this.toppings.length > 0) {
            sb.append("\nToppings:");
            for (String t : toppings) {
                sb.append("\t" + t);
            }
        }
        return sb.toString();
    }

    public static class Builder {
        // Required builder fields
        private String type; // cup or cone
        private String flavor;
        private String[] toppings;

        // Optional builder fields
        private String sauce;
        private String size; // regular, medium, large

        public Builder(String type, String size, String flavor) {
            this.type = type;
            this.size = size;
            this.flavor = flavor;
        }

        public Builder withToppings(String[] toppings) {
            this.toppings = toppings;
            return this;
        }

        public Builder withSauce(String sauce) {
            this.sauce = sauce;
            return this;
        }

        public IceCream build() {
            return new IceCream(this);
        }

    }

}

Demo.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Demo{
    public static void main(String[] args) {
        IceCream chocolateIcecream = 
        new IceCream.Builder("cone", "small", "chocolate")
        .withSauce("Caramel")
        .withToppings(new String[]{"Chocolate chip cookie", "Oreo crumbs", "M&M's"})
        .build();
        System.out.println(chocolateIcecream);
    }
}


Building Virtual Machine Example

Builder.java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.Arrays;

public class VirtualMachine {

    // 10 required fields: CloudProvider, Region, AvailabilityZone, ImageId, InstanceClass, vpc, Subnet, SecurityGroup, and PublicKey, and publicIP.
    private String cloudProvider;
    private String region;
    private String availabilityZone;
    private String imageId;
    private String instanceClass;
    private String vpc;
    private String subnet;
    private String securityGroup;
    private String publicKey;
    private String publicIP;

    // 7 optional fields: Ports, PrivateIP, PlacementGroup, Architecture, Hypervisor, Storage, and Tags.
    private String[] ports;
    private String privateIP;
    private String placementGroup;
    private String architecture;
    private String hypervisor;
    private String storage;
    private String[] tags;

    // This creates a VirtualMachine instance with all required fields
    private VirtualMachine(VMBuilder builder) {
        this.cloudProvider = builder.cloudProvider;
        this.region = builder.region;
        this.availabilityZone = builder.availabilityZone;
        this.imageId = builder.imageId;
        this.instanceClass = builder.instanceClass;
        this.vpc = builder.vpc;
        this.subnet = builder.subnet;
        this.securityGroup = builder.securityGroup;
        this.publicKey = builder.publicKey;
        this.publicIP = builder.publicIP;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("VirtualMachine{" +
                "cloudProvider='" + cloudProvider + '\'' +
                ", region='" + region + '\'' +
                ", availabilityZone='" + availabilityZone + '\'' +
                ", imageId='" + imageId + '\'' +
                ", instanceClass='" + instanceClass + '\'' +
                ", vpc='" + vpc + '\'' +
                ", subnet='" + subnet + '\'' +
                ", securityGroup='" + securityGroup + '\'' +
                ", publicKey='" + publicKey + '\'' +
                ", publicIP='" + publicIP + '\'');
        if (ports != null) {
            sb.append(", ports=" + Arrays.toString(ports));
        }
        if (privateIP != null) {
            sb.append(", privateIP='" + privateIP + '\'');
        }
        if (placementGroup != null) {
            sb.append(", placementGroup='" + placementGroup + '\'');
        }
        if (architecture != null) {
            sb.append(", architecture='" + architecture + '\'');
        }
        if (hypervisor != null) {
            sb.append(", hypervisor='" + hypervisor + '\'');
        }
        if (storage != null) {
            sb.append(", storage='" + storage + '\'');
        }
        if (tags != null) {
            sb.append(", tags=" + Arrays.toString(tags));
        }
        sb.append("}");
        return sb.toString();
    }

    public static class VMBuilder {
        // 10 required fields: CloudProvider, Region, AvailabilityZone, ImageId, InstanceClass, vpc, Subnet, SecurityGroup, and PublicKey, and publicIP.
        private String cloudProvider;
        private String region;
        private String availabilityZone;
        private String imageId;
        private String instanceClass;
        private String vpc;
        private String subnet;
        private String securityGroup;
        private String publicKey;
        private String publicIP;

        // 7 optional fields: Ports, PrivateIP, PlacementGroup, Architecture, Hypervisor, Storage, and Tags.
        private String[] ports;
        private String privateIP;
        private String placementGroup;
        private String architecture;
        private String hypervisor;
        private String storage;
        private String[] tags;

        public VMBuilder(String cloudProvider, String region, String availabilityZone, String imageId,
                         String instanceClass, String vpc, String subnet, String securityGroup,
                         String publicKey, String publicIP) {
            this.cloudProvider = cloudProvider;
            this.region = region;
            this.availabilityZone = availabilityZone;
            this.imageId = imageId;
            this.instanceClass = instanceClass;
            this.vpc = vpc;
            this.subnet = subnet;
            this.securityGroup = securityGroup;
            this.publicKey = publicKey;
            this.publicIP = publicIP;
        }

        public VMBuilder withPorts(String[] ports) {
            this.ports = ports;
            return this;
        }

        public VMBuilder withPrivateIP(String privateIP) {
            this.privateIP = privateIP;
            return this;
        }

        public VMBuilder withPlacementGroup(String placementGroup) {
            this.placementGroup = placementGroup;
            return this;
        }

        public VMBuilder withArchitecture(String architecture) {
            this.architecture = architecture;
            return this;
        }

        public VMBuilder withHypervisor(String hypervisor) {
            this.hypervisor = hypervisor;
            return this;
        }

        public VMBuilder withStorage(String storage) {
            this.storage = storage;
            return this;
        }

        public VMBuilder withTags(String[] tags) {
            this.tags = tags;
            return this;
        }

        public VirtualMachine build(){
            return new VirtualMachine(this);
        }
    }
}

App.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class App 
{
    public static void main( String[] args )
    {
        // Create a VM on AWS, us-east, az-1, ubuntu20.04-098im, t2.micro, myvpc, mysubnet,
        // sg1, my-vm-pub-key.
        VirtualMachine awsVM = new VirtualMachine.VMBuilder("AWS", "us-east", "az-1",
                "ubuntu20.04-098im", "t2.micro", "myvpc",
                "mysubnet", "sg1", "my-vm-key.pub", "53.24.12.15")
                .build();
        System.out.println("launching a VM on AWS \n" + awsVM);

        // Create a VM on Azure, us-west, az-1, windows-server.2022-5483im, a.series, myvpc, mysubnet,
        // sg1, my-vm-pub-key, ports [3389, 80, 443], and tags ["cpit252", "lab"]
        VirtualMachine azureVM = new VirtualMachine.VMBuilder("Azure", "us-west", "az-1",
                "ubuntu20.04-098im", "a.series", "myvpc",
                "mysubnet", "sg1", "my-vm-pub-key", "53.24.12.15")
                .withPorts(new String[]{"3389", "80", "443"})
                .withTags(new String[]{"cpit252", "lab"})
                .build();

        System.out.println("launching a VM on Azure \n" + azureVM);
    }
}