From 6caf78e4e57fe98fde7813f9129437f530022f63 Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Fri, 4 Sep 2020 21:21:51 +0100 Subject: [PATCH] updates : - Using lambda expression to create cars - Using spaces instead of tabs in pom.xml --- factory/README.md | 43 ++++++++++++------- factory/pom.xml | 42 +++++++++--------- .../main/java/com/iluwatar/factory/App.java | 4 +- .../java/com/iluwatar/factory/CarType.java | 22 ++++++++++ .../com/iluwatar/factory/CarsFactory.java | 13 +----- .../com/iluwatar/factory/CarsFactoryTest.java | 2 +- 6 files changed, 75 insertions(+), 51 deletions(-) create mode 100644 factory/src/main/java/com/iluwatar/factory/CarType.java diff --git a/factory/README.md b/factory/README.md index 42c8838a3..62478e4a9 100644 --- a/factory/README.md +++ b/factory/README.md @@ -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 constructor; + + CarType(Supplier constructor) { + this.constructor = constructor; + } + + public Supplier 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: diff --git a/factory/pom.xml b/factory/pom.xml index fbca994a7..2a422db90 100644 --- a/factory/pom.xml +++ b/factory/pom.xml @@ -1,23 +1,25 @@ - - 4.0.0 - - com.iluwatar - java-design-patterns - 1.24.0-SNAPSHOT - - factory - - - org.junit.jupiter - junit-jupiter-engine - test - - - junit - junit - - - + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.24.0-SNAPSHOT + + factory + + + org.junit.jupiter + junit-jupiter-engine + test + + + junit + junit + + + diff --git a/factory/src/main/java/com/iluwatar/factory/App.java b/factory/src/main/java/com/iluwatar/factory/App.java index 13f217646..22c50d86d 100644 --- a/factory/src/main/java/com/iluwatar/factory/App.java +++ b/factory/src/main/java/com/iluwatar/factory/App.java @@ -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()); } diff --git a/factory/src/main/java/com/iluwatar/factory/CarType.java b/factory/src/main/java/com/iluwatar/factory/CarType.java new file mode 100644 index 000000000..5878025bd --- /dev/null +++ b/factory/src/main/java/com/iluwatar/factory/CarType.java @@ -0,0 +1,22 @@ +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 constructor; + + CarType(Supplier constructor) { + this.constructor = constructor; + } + + public Supplier getConstructor() { + return this.constructor; + } +} diff --git a/factory/src/main/java/com/iluwatar/factory/CarsFactory.java b/factory/src/main/java/com/iluwatar/factory/CarsFactory.java index 01ad157ca..76b83d0be 100644 --- a/factory/src/main/java/com/iluwatar/factory/CarsFactory.java +++ b/factory/src/main/java/com/iluwatar/factory/CarsFactory.java @@ -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(); } } diff --git a/factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.java b/factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.java index eae75919a..fc823c6d4 100644 --- a/factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.java +++ b/factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.java @@ -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); } -- GitLab