提交 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 { ...@@ -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 ```java
/** public enum CarType {
* Factory of cars.
*/
public class CarSimpleFactory {
/** /**
* Enumeration for different types of cars. * Enumeration for different types of cars.
*/ */
static enum CarType { FORD(Ford::new),
FORD, FERRARI 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. * Factory method takes as parameter a car type and initiate the appropriate class.
*/ */
public static Car getCar(CarType type) { public static Car getCar(CarType type) {
switch (type) { return type.getConstructor().get();
case FORD: return new Ford();
case FERRARI: return new Ferrari();
default: throw new IllegalArgumentException("Model not supported.");
}
} }
} }
``` ```
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 ```java
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD); var car1 = CarsFactory.getCar(CarType.FORD);
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI); var car2 = CarsFactory.getCar(CarType.FERRARI);
LOGGER.info(car1.getDescription()); LOGGER.info(car1.getDescription());
LOGGER.info(car2.getDescription()); LOGGER.info(car2.getDescription());;
``` ```
Program output: 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"> <project xmlns="http://maven.apache.org/POM/4.0.0"
<modelVersion>4.0.0</modelVersion> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<parent> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.iluwatar</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>java-design-patterns</artifactId> <parent>
<version>1.24.0-SNAPSHOT</version> <groupId>com.iluwatar</groupId>
</parent> <artifactId>java-design-patterns</artifactId>
<artifactId>factory</artifactId> <version>1.24.0-SNAPSHOT</version>
<dependencies> </parent>
<dependency> <artifactId>factory</artifactId>
<groupId>org.junit.jupiter</groupId> <dependencies>
<artifactId>junit-jupiter-engine</artifactId> <dependency>
<scope>test</scope> <groupId>org.junit.jupiter</groupId>
</dependency> <artifactId>junit-jupiter-engine</artifactId>
<dependency> <scope>test</scope>
<groupId>junit</groupId> </dependency>
<artifactId>junit</artifactId> <dependency>
</dependency> <groupId>junit</groupId>
</dependencies> <artifactId>junit</artifactId>
<build> </dependency>
</dependencies>
<build>
<plugins> <plugins>
<!-- Maven assembly plugin is invoked with default setting which we have <!-- Maven assembly plugin is invoked with default setting which we have
in parent pom and specifying the class having main method --> in parent pom and specifying the class having main method -->
......
...@@ -43,8 +43,8 @@ public class App { ...@@ -43,8 +43,8 @@ public class App {
* Program main entry point. * Program main entry point.
*/ */
public static void main(String[] args) { public static void main(String[] args) {
var car1 = CarsFactory.getCar(CarsFactory.CarType.FORD); var car1 = CarsFactory.getCar(CarType.FORD);
var car2 = CarsFactory.getCar(CarsFactory.CarType.FERRARI); var car2 = CarsFactory.getCar(CarType.FERRARI);
LOGGER.info(car1.getDescription()); LOGGER.info(car1.getDescription());
LOGGER.info(car2.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; ...@@ -5,21 +5,10 @@ package com.iluwatar.factory;
*/ */
public class CarsFactory { 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. * Factory method takes as parameter a car type and initiate the appropriate class.
*/ */
public static Car getCar(CarType type) { public static Car getCar(CarType type) {
switch (type) { return type.getConstructor().get();
case FORD: return new Ford();
case FERRARI: return new Ferrari();
default: throw new IllegalArgumentException("Model not supported.");
}
} }
} }
...@@ -8,7 +8,7 @@ class CarsFactoryTest { ...@@ -8,7 +8,7 @@ class CarsFactoryTest {
@Test @Test
void shouldReturnFerrariInstance() { void shouldReturnFerrariInstance() {
final var ferrari = CarsFactory.getCar(CarsFactory.CarType.FERRARI); final var ferrari = CarsFactory.getCar(CarType.FERRARI);
assertTrue(ferrari instanceof Ferrari); assertTrue(ferrari instanceof Ferrari);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册