提交 6caf78e4 编写于 作者: S Samil Ayoub

updates :

- Using lambda expression to create cars
- Using spaces instead of tabs in pom.xml
上级 8b26452c
......@@ -68,41 +68,52 @@ public class Ferrari implements Car {
}
```
Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory".
Enumeration above represents types of cars that we support(Ford and Ferrari).
```java
/**
* Factory of cars.
*/
public class CarSimpleFactory {
public enum CarType {
/**
* Enumeration for different types of cars.
*/
static enum CarType {
FORD, FERRARI
FORD(Ford::new),
FERRARI(Ferrari::new);
private final Supplier<Car> constructor;
CarType(Supplier<Car> constructor) {
this.constructor = constructor;
}
public Supplier<Car> getConstructor() {
return this.constructor;
}
}
```
Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory".
```java
/**
* Factory of cars.
*/
public class CarsFactory {
/**
* Factory method takes as parameter a car type and initiate the appropriate class.
*/
public static Car getCar(CarType type) {
switch (type) {
case FORD: return new Ford();
case FERRARI: return new Ferrari();
default: throw new IllegalArgumentException("Model not supported.");
}
return type.getConstructor().get();
}
}
```
Now on the client code we can create differentes types of cars(Ford or Ferrari) using the factory class.
Now on the client code we can create different types of cars using the factory class.
```java
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD);
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI);
var car1 = CarsFactory.getCar(CarType.FORD);
var car2 = CarsFactory.getCar(CarType.FERRARI);
LOGGER.info(car1.getDescription());
LOGGER.info(car2.getDescription());
LOGGER.info(car2.getDescription());;
```
Program output:
......
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>factory</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>factory</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven assembly plugin is invoked with default setting which we have
in parent pom and specifying the class having main method -->
......
......@@ -43,8 +43,8 @@ public class App {
* Program main entry point.
*/
public static void main(String[] args) {
var car1 = CarsFactory.getCar(CarsFactory.CarType.FORD);
var car2 = CarsFactory.getCar(CarsFactory.CarType.FERRARI);
var car1 = CarsFactory.getCar(CarType.FORD);
var car2 = CarsFactory.getCar(CarType.FERRARI);
LOGGER.info(car1.getDescription());
LOGGER.info(car2.getDescription());
}
......
package com.iluwatar.factory;
import java.util.function.Supplier;
public enum CarType {
/**
* Enumeration for different types of cars.
*/
FORD(Ford::new),
FERRARI(Ferrari::new);
private final Supplier<Car> constructor;
CarType(Supplier<Car> constructor) {
this.constructor = constructor;
}
public Supplier<Car> getConstructor() {
return this.constructor;
}
}
......@@ -5,21 +5,10 @@ package com.iluwatar.factory;
*/
public class CarsFactory {
/**
* Enumeration for different types of cars.
*/
static enum CarType {
FORD, FERRARI
}
/**
* Factory method takes as parameter a car type and initiate the appropriate class.
*/
public static Car getCar(CarType type) {
switch (type) {
case FORD: return new Ford();
case FERRARI: return new Ferrari();
default: throw new IllegalArgumentException("Model not supported.");
}
return type.getConstructor().get();
}
}
......@@ -8,7 +8,7 @@ class CarsFactoryTest {
@Test
void shouldReturnFerrariInstance() {
final var ferrari = CarsFactory.getCar(CarsFactory.CarType.FERRARI);
final var ferrari = CarsFactory.getCar(CarType.FERRARI);
assertTrue(ferrari instanceof Ferrari);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册