Skip to content

Commit f654f02

Browse files
committed
Builder Design Pattern - Ambulance Builder example
1 parent d893238 commit f654f02

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.premaseem.ambulanceBuilder;
2+
3+
public class AmbulanceBuilder {
4+
5+
Ambulance buildAmbulance(int numberOfWheel, String label){
6+
Ambulance ambulance=new Ambulance();
7+
VanFactory vanFactory = new VanFactory();
8+
ambulance.setVan(vanFactory.getVan(numberOfWheel));
9+
ambulance.setLabel(label);
10+
return ambulance;
11+
}
12+
}
13+
14+
class Ambulance{
15+
Van van;
16+
String label;
17+
18+
Van getVan() {
19+
return van;
20+
}
21+
void setVan(Van van) {
22+
this.van = van;
23+
}
24+
String getLabel() {
25+
return label;
26+
}
27+
void setLabel(String label) {
28+
this.label = label;
29+
}
30+
31+
32+
@Override
33+
public String toString() {
34+
return "Ambulance for " + label + " with number of wheels " + van.noOfWheels;
35+
}
36+
37+
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.premaseem.ambulanceBuilder;
2+
3+
import java.util.Scanner;
4+
5+
6+
public class ClientForAmbulanceBuilder {
7+
8+
public static void main(String[] args) {
9+
10+
System.out.println("Welcome to Abmulance Builder project ");
11+
Scanner scan = new Scanner(System.in);
12+
13+
int repeatRunFlag = 1;
14+
AmbulanceBuilder ambulanceBuilder = new AmbulanceBuilder();
15+
Ambulance ambulance = null;
16+
while (repeatRunFlag == 1) {
17+
System.out.println("What kind of Ambulance you want ? ");
18+
System.out.println("press 1 for 4 Wheeler");
19+
System.out.println("press 2 for 6 Wheeler ");
20+
System.out.println("press 3 for 8 Wheeler ");
21+
22+
int numberOfWheel = scan.nextInt();
23+
24+
System.out.println("What kind lable you want for Ambulance ? ");
25+
String label = scan.next();
26+
27+
ambulance = ambulanceBuilder.buildAmbulance(numberOfWheel, label);
28+
29+
30+
System.out.println("Ambulanc is build " + ambulance);
31+
System.out.println("=============================");
32+
33+
System.out.println("Press 1 to Repeat .... ");
34+
try {
35+
repeatRunFlag = scan.nextInt();
36+
} catch (Exception e) {
37+
repeatRunFlag = 0;
38+
}
39+
40+
}
41+
42+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
43+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.com $$$$$$$$$$$$$$$$$$$$$$ \n ");
44+
45+
}
46+
47+
48+
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.premaseem.ambulanceBuilder;
2+
3+
public class VanFactory {
4+
5+
// factory method
6+
Van createVan(int noOfWheel) {
7+
Van van = new Van();
8+
van.noOfWheels = noOfWheel;
9+
return van;
10+
}
11+
12+
// simple factory
13+
Van getVan(int noOfWheel) {
14+
15+
switch (noOfWheel) {
16+
case 1:
17+
return new FourWheelerVan();
18+
case 2:
19+
return new FourWheelerVan();
20+
default:
21+
return new EightWheelerVan();
22+
}
23+
24+
}
25+
26+
}
27+
28+
29+
30+
31+
32+
class Van {
33+
int noOfWheels;
34+
}
35+
36+
class SixWheelerVan extends Van {
37+
public SixWheelerVan() {
38+
noOfWheels = 6;
39+
}
40+
}
41+
42+
class FourWheelerVan extends Van {
43+
public FourWheelerVan() {
44+
noOfWheels = 4;
45+
}
46+
}
47+
48+
class EightWheelerVan extends Van {
49+
public EightWheelerVan() {
50+
noOfWheels = 8;
51+
}
52+
}

0 commit comments

Comments
 (0)