Skip to content

Commit 56e3083

Browse files
dhananjay-ngDhananjay Nagargoje
authored andcommitted
#DP : Factory Pattern
1 parent a4fc3e1 commit 56e3083

File tree

8 files changed

+626
-391
lines changed

8 files changed

+626
-391
lines changed

.idea/workspace.xml

Lines changed: 511 additions & 391 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package designpatterns.creational.factory;
2+
3+
public abstract class Car {
4+
private CarType model;
5+
6+
public Car(CarType carType) {
7+
this.model = carType;
8+
arrangeParts();
9+
10+
}
11+
12+
private void arrangeParts() {
13+
// common logic here
14+
}
15+
16+
//each car subclass will construct car in different way
17+
protected abstract void construct();
18+
19+
public CarType getModel() {
20+
return model;
21+
}
22+
23+
public void setModel(CarType model) {
24+
this.model = model;
25+
}
26+
27+
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package designpatterns.creational.factory;
2+
3+
public class CarFactory {
4+
public static Car buildCar(CarType carType) {
5+
Car car;
6+
7+
switch (carType) {
8+
9+
case SMALL:
10+
car = new SmallCar();
11+
break;
12+
13+
case SEDAN:
14+
car = new SedanCar();
15+
break;
16+
17+
case LUXURY:
18+
car = new LuxuryCar();
19+
break;
20+
21+
default:
22+
throw new RuntimeException();
23+
}
24+
return car;
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package designpatterns.creational.factory;
2+
3+
public enum CarType {
4+
SMALL,
5+
SEDAN,
6+
LUXURY
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package designpatterns.creational.factory;
2+
3+
public class LuxuryCar extends Car {
4+
5+
public LuxuryCar() {
6+
super(CarType.LUXURY);
7+
construct();
8+
}
9+
10+
@Override
11+
protected void construct() {
12+
System.out.println("Building Luxury Car ..");
13+
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package designpatterns.creational.factory;
2+
3+
public class SedanCar extends Car {
4+
5+
SedanCar() {
6+
super(CarType.SEDAN);
7+
construct();
8+
}
9+
10+
@Override
11+
protected void construct() {
12+
System.out.println("Building sedan car..");
13+
// add accessories
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package designpatterns.creational.factory;
2+
3+
public class SmallCar extends Car {
4+
5+
SmallCar() {
6+
super(CarType.SMALL);
7+
construct();
8+
}
9+
10+
@Override
11+
protected void construct() {
12+
System.out.println("Building small car..");
13+
// add accessories
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package designpatterns.creational.factory;
2+
3+
public class TestCarFactory {
4+
public static void main(String[] args) {
5+
System.out.println(CarFactory.buildCar(CarType.SMALL));
6+
System.out.println(CarFactory.buildCar(CarType.SEDAN));
7+
System.out.println(CarFactory.buildCar(CarType.LUXURY));
8+
}
9+
}

0 commit comments

Comments
 (0)