Skip to content

Commit 0d4c90a

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

File tree

12 files changed

+427
-198
lines changed

12 files changed

+427
-198
lines changed

.idea/workspace.xml

Lines changed: 222 additions & 198 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
4+
public abstract class Car {
5+
private CarType model;
6+
private Location location;
7+
8+
public Car(CarType carType, Location location) {
9+
this.model = carType;
10+
this.location = location;
11+
12+
}
13+
14+
//each car subclass will construct car in different way
15+
protected abstract void construct();
16+
17+
public CarType getModel() {
18+
return model;
19+
}
20+
21+
public void setModel(CarType model) {
22+
this.model = model;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Model- "+model + " built in "+location;
28+
}
29+
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
public class CarFactory {
4+
private CarFactory() {
5+
6+
}
7+
8+
public static Car buildCar(CarType carType) {
9+
Car car;
10+
Location location = Location.INDIA; // access location from some configuration.
11+
12+
switch (location) {
13+
14+
case INDIA:
15+
car = IndianCarFactory.buildCar(carType);
16+
break;
17+
18+
case USA:
19+
car = USACarFactory.buildCar(carType);
20+
break;
21+
22+
default:
23+
car = DefaultCarFactory.buildCar(carType);
24+
break;
25+
}
26+
return car;
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
public enum CarType {
4+
SMALL,
5+
SEDAN,
6+
LUXURY
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
public class DefaultCarFactory {
4+
public static Car buildCar(CarType carType)
5+
{
6+
Car car = null;
7+
switch (carType)
8+
{
9+
case SMALL:
10+
car = new SmallCar(Location.DEFAULT);
11+
break;
12+
13+
case SEDAN:
14+
car = new SedanCar(Location.DEFAULT);
15+
break;
16+
17+
case LUXURY:
18+
car = new LuxuryCar(Location.DEFAULT);
19+
break;
20+
21+
default:
22+
//throw some exception
23+
break;
24+
}
25+
return car;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
public class IndianCarFactory {
4+
public static Car buildCar(CarType carType) {
5+
Car car = null;
6+
switch (carType) {
7+
case SMALL:
8+
car = new SmallCar(Location.INDIA);
9+
break;
10+
11+
case SEDAN:
12+
car = new SedanCar(Location.INDIA);
13+
break;
14+
15+
case LUXURY:
16+
car = new LuxuryCar(Location.INDIA);
17+
break;
18+
19+
default:
20+
//throw some exception
21+
break;
22+
}
23+
return car;
24+
}
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
public enum Location {
4+
INDIA,
5+
USA,
6+
DEFAULT
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package designpatterns.creational.abstractfactory;
2+
3+
public class LuxuryCar extends Car {
4+
5+
public LuxuryCar(Location location) {
6+
super(CarType.LUXURY, location);
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.abstractfactory;
2+
3+
public class SedanCar extends Car {
4+
5+
SedanCar(Location location) {
6+
super(CarType.SEDAN, location);
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.abstractfactory;
2+
3+
public class SmallCar extends Car {
4+
5+
SmallCar(Location location) {
6+
super(CarType.SMALL, location);
7+
construct();
8+
}
9+
10+
@Override
11+
protected void construct() {
12+
System.out.println("Building small car..");
13+
// add accessories
14+
}
15+
}

0 commit comments

Comments
 (0)