Skip to content

Commit 6caf78e

Browse files
committed
updates :
- Using lambda expression to create cars - Using spaces instead of tabs in pom.xml
1 parent 8b26452 commit 6caf78e

6 files changed

Lines changed: 75 additions & 51 deletions

File tree

‎factory/README.md‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,41 +68,52 @@ public class Ferrari implements Car {
6868
}
6969
```
7070

71-
Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory".
71+
Enumeration above represents types of cars that we support(Ford and Ferrari).
7272

7373
```java
74-
/**
75-
* Factory of cars.
76-
*/
77-
public class CarSimpleFactory {
74+
public enum CarType {
7875

7976
/**
8077
* Enumeration for different types of cars.
8178
*/
82-
static enum CarType {
83-
FORD, FERRARI
79+
FORD(Ford::new),
80+
FERRARI(Ferrari::new);
81+
82+
private final Supplier<Car> constructor;
83+
84+
CarType(Supplier<Car> constructor) {
85+
this.constructor = constructor;
86+
}
87+
88+
public Supplier<Car> getConstructor() {
89+
return this.constructor;
8490
}
91+
}
92+
```
93+
Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory".
94+
95+
```java
96+
/**
97+
* Factory of cars.
98+
*/
99+
public class CarsFactory {
85100

86101
/**
87102
* Factory method takes as parameter a car type and initiate the appropriate class.
88103
*/
89104
public static Car getCar(CarType type) {
90-
switch (type) {
91-
case FORD: return new Ford();
92-
case FERRARI: return new Ferrari();
93-
default: throw new IllegalArgumentException("Model not supported.");
94-
}
105+
return type.getConstructor().get();
95106
}
96107
}
97108
```
98109

99-
Now on the client code we can create differentes types of cars(Ford or Ferrari) using the factory class.
110+
Now on the client code we can create different types of cars using the factory class.
100111

101112
```java
102-
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD);
103-
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI);
113+
var car1 = CarsFactory.getCar(CarType.FORD);
114+
var car2 = CarsFactory.getCar(CarType.FERRARI);
104115
LOGGER.info(car1.getDescription());
105-
LOGGER.info(car2.getDescription());
116+
LOGGER.info(car2.getDescription());;
106117
```
107118

108119
Program output:

‎factory/pom.xml‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2-
<modelVersion>4.0.0</modelVersion>
3-
<parent>
4-
<groupId>com.iluwatar</groupId>
5-
<artifactId>java-design-patterns</artifactId>
6-
<version>1.24.0-SNAPSHOT</version>
7-
</parent>
8-
<artifactId>factory</artifactId>
9-
<dependencies>
10-
<dependency>
11-
<groupId>org.junit.jupiter</groupId>
12-
<artifactId>junit-jupiter-engine</artifactId>
13-
<scope>test</scope>
14-
</dependency>
15-
<dependency>
16-
<groupId>junit</groupId>
17-
<artifactId>junit</artifactId>
18-
</dependency>
19-
</dependencies>
20-
<build>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.24.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>factory</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.junit.jupiter</groupId>
14+
<artifactId>junit-jupiter-engine</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
</dependency>
21+
</dependencies>
22+
<build>
2123
<plugins>
2224
<!-- Maven assembly plugin is invoked with default setting which we have
2325
in parent pom and specifying the class having main method -->

‎factory/src/main/java/com/iluwatar/factory/App.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public class App {
4343
* Program main entry point.
4444
*/
4545
public static void main(String[] args) {
46-
var car1 = CarsFactory.getCar(CarsFactory.CarType.FORD);
47-
var car2 = CarsFactory.getCar(CarsFactory.CarType.FERRARI);
46+
var car1 = CarsFactory.getCar(CarType.FORD);
47+
var car2 = CarsFactory.getCar(CarType.FERRARI);
4848
LOGGER.info(car1.getDescription());
4949
LOGGER.info(car2.getDescription());
5050
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.factory;
2+
3+
import java.util.function.Supplier;
4+
5+
public enum CarType {
6+
7+
/**
8+
* Enumeration for different types of cars.
9+
*/
10+
FORD(Ford::new),
11+
FERRARI(Ferrari::new);
12+
13+
private final Supplier<Car> constructor;
14+
15+
CarType(Supplier<Car> constructor) {
16+
this.constructor = constructor;
17+
}
18+
19+
public Supplier<Car> getConstructor() {
20+
return this.constructor;
21+
}
22+
}

‎factory/src/main/java/com/iluwatar/factory/CarsFactory.java‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,10 @@
55
*/
66
public class CarsFactory {
77

8-
/**
9-
* Enumeration for different types of cars.
10-
*/
11-
static enum CarType {
12-
FORD, FERRARI
13-
}
14-
158
/**
169
* Factory method takes as parameter a car type and initiate the appropriate class.
1710
*/
1811
public static Car getCar(CarType type) {
19-
switch (type) {
20-
case FORD: return new Ford();
21-
case FERRARI: return new Ferrari();
22-
default: throw new IllegalArgumentException("Model not supported.");
23-
}
12+
return type.getConstructor().get();
2413
}
2514
}

‎factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CarsFactoryTest {
88

99
@Test
1010
void shouldReturnFerrariInstance() {
11-
final var ferrari = CarsFactory.getCar(CarsFactory.CarType.FERRARI);
11+
final var ferrari = CarsFactory.getCar(CarType.FERRARI);
1212
assertTrue(ferrari instanceof Ferrari);
1313
}
1414

0 commit comments

Comments
 (0)