From 69dfcd329361c788a55007804c8bcbabb421377e Mon Sep 17 00:00:00 2001 From: alemoles Date: Mon, 14 Nov 2022 00:41:44 -0300 Subject: [PATCH] BAEL-5850 - Abstract Factory vs Factory Method Pattern in Java (#12) (#13017) --- .../abstract_factory/Corporation.java | 8 ++++ .../abstract_factory/ElectricVehicle.java | 5 +++ .../FutureVehicleCorporation.java | 13 +++++++ .../FutureVehicleElectricCar.java | 8 ++++ .../FutureVehicleMotorcycle.java | 8 ++++ .../abstract_factory/MotorVehicle.java | 5 +++ .../abstract_factory/NextGenCorporation.java | 13 +++++++ .../abstract_factory/NextGenElectricCar.java | 8 ++++ .../abstract_factory/NextGenMotorcycle.java | 8 ++++ .../baeldung/factory_pattern/method/Car.java | 8 ++++ .../factory_pattern/method/CarFactory.java | 8 ++++ .../factory_pattern/method/MotorVehicle.java | 5 +++ .../method/MotorVehicleFactory.java | 11 ++++++ .../factory_pattern/method/Motorcycle.java | 8 ++++ .../method/MotorcycleFactory.java | 8 ++++ .../AbstractFactoryUnitTest.java | 39 +++++++++++++++++++ .../method/FactoryMethodUnitTest.java | 23 +++++++++++ 17 files changed, 186 insertions(+) create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/Corporation.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/ElectricVehicle.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleCorporation.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleElectricCar.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleMotorcycle.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/MotorVehicle.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenCorporation.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenElectricCar.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenMotorcycle.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Car.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/CarFactory.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicle.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicleFactory.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Motorcycle.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorcycleFactory.java create mode 100644 patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/abstract_factory/AbstractFactoryUnitTest.java create mode 100644 patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/method/FactoryMethodUnitTest.java diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/Corporation.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/Corporation.java new file mode 100644 index 0000000000..a75e65e4fe --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/Corporation.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public abstract class Corporation { + + public abstract MotorVehicle createMotorVehicle(); + + public abstract ElectricVehicle createElectricVehicle(); +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/ElectricVehicle.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/ElectricVehicle.java new file mode 100644 index 0000000000..8de25d65ca --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/ElectricVehicle.java @@ -0,0 +1,5 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public interface ElectricVehicle { + void build(); +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleCorporation.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleCorporation.java new file mode 100644 index 0000000000..b99ab3d2d7 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleCorporation.java @@ -0,0 +1,13 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public class FutureVehicleCorporation extends Corporation { + @Override + public MotorVehicle createMotorVehicle() { + return new FutureVehicleMotorcycle(); + } + + @Override + public ElectricVehicle createElectricVehicle() { + return new FutureVehicleElectricCar(); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleElectricCar.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleElectricCar.java new file mode 100644 index 0000000000..436bbc9801 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleElectricCar.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public class FutureVehicleElectricCar implements ElectricVehicle { + @Override + public void build() { + System.out.println("Future Vehicle Electric Car"); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleMotorcycle.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleMotorcycle.java new file mode 100644 index 0000000000..c8896256d5 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/FutureVehicleMotorcycle.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public class FutureVehicleMotorcycle implements MotorVehicle { + @Override + public void build() { + System.out.println("Future Vehicle Motorcycle"); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/MotorVehicle.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/MotorVehicle.java new file mode 100644 index 0000000000..f71d5636e2 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/MotorVehicle.java @@ -0,0 +1,5 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public interface MotorVehicle { + void build(); +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenCorporation.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenCorporation.java new file mode 100644 index 0000000000..3cb965cf93 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenCorporation.java @@ -0,0 +1,13 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public class NextGenCorporation extends Corporation { + @Override + public MotorVehicle createMotorVehicle() { + return new NextGenMotorcycle(); + } + + @Override + public ElectricVehicle createElectricVehicle() { + return new NextGenElectricCar(); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenElectricCar.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenElectricCar.java new file mode 100644 index 0000000000..716d4060be --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenElectricCar.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public class NextGenElectricCar implements ElectricVehicle { + @Override + public void build() { + System.out.println("NextGen Electric Car"); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenMotorcycle.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenMotorcycle.java new file mode 100644 index 0000000000..c475503616 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/abstract_factory/NextGenMotorcycle.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.abstract_factory; + +public class NextGenMotorcycle implements MotorVehicle { + @Override + public void build() { + System.out.println("NextGen Motorcycle"); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Car.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Car.java new file mode 100644 index 0000000000..0c7b0d9eef --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Car.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.method; + +public class Car implements MotorVehicle { + @Override + public void build() { + System.out.println("Build Car"); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/CarFactory.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/CarFactory.java new file mode 100644 index 0000000000..2cd78e0394 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/CarFactory.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.method; + +public class CarFactory extends MotorVehicleFactory { + @Override + protected MotorVehicle createMotorVehicle() { + return new Car(); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicle.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicle.java new file mode 100644 index 0000000000..a9517e887a --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicle.java @@ -0,0 +1,5 @@ +package com.baeldung.factory_pattern.method; + +public interface MotorVehicle { + void build(); +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicleFactory.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicleFactory.java new file mode 100644 index 0000000000..0b719ff19f --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorVehicleFactory.java @@ -0,0 +1,11 @@ +package com.baeldung.factory_pattern.method; + +public abstract class MotorVehicleFactory { + public MotorVehicle create() { + MotorVehicle vehicle = createMotorVehicle(); + vehicle.build(); + return vehicle; + } + + protected abstract MotorVehicle createMotorVehicle(); +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Motorcycle.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Motorcycle.java new file mode 100644 index 0000000000..be3e736a06 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/Motorcycle.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.method; + +public class Motorcycle implements MotorVehicle { + @Override + public void build() { + System.out.println("Build Motorcycle"); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorcycleFactory.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorcycleFactory.java new file mode 100644 index 0000000000..10acef7bce --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factory_pattern/method/MotorcycleFactory.java @@ -0,0 +1,8 @@ +package com.baeldung.factory_pattern.method; + +public class MotorcycleFactory extends MotorVehicleFactory { + @Override + protected MotorVehicle createMotorVehicle() { + return new Motorcycle(); + } +} diff --git a/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/abstract_factory/AbstractFactoryUnitTest.java b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/abstract_factory/AbstractFactoryUnitTest.java new file mode 100644 index 0000000000..54f3fe4011 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/abstract_factory/AbstractFactoryUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.factory_pattern.abstract_factory; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class AbstractFactoryUnitTest { + @Test + public void givenFutureVehicleFactory_whenCreateMotorVehicle_thenInstanceOf() { + Corporation corporation = new FutureVehicleCorporation(); + MotorVehicle motorVehicle = corporation.createMotorVehicle(); + assertThat(motorVehicle).isInstanceOf(MotorVehicle.class); + assertThat(motorVehicle).isInstanceOf(FutureVehicleMotorcycle.class); + } + + @Test + public void givenFutureVehicleFactory_whenCreateElectricVehicle_thenInstanceOf() { + Corporation corporation = new FutureVehicleCorporation(); + ElectricVehicle electricVehicle = corporation.createElectricVehicle(); + assertThat(electricVehicle).isInstanceOf(ElectricVehicle.class); + assertThat(electricVehicle).isInstanceOf(FutureVehicleElectricCar.class); + } + + @Test + public void givenNextGenFactory_whenCreateMotorVehicle_thenInstanceOf() { + Corporation corporation = new NextGenCorporation(); + MotorVehicle motorVehicle = corporation.createMotorVehicle(); + assertThat(motorVehicle).isInstanceOf(MotorVehicle.class); + assertThat(motorVehicle).isInstanceOf(NextGenMotorcycle.class); + } + + @Test + public void givenNextGenFactory_whenCreateElectricVehicle_thenInstanceOf() { + Corporation corporation = new NextGenCorporation(); + ElectricVehicle electricVehicle = corporation.createElectricVehicle(); + assertThat(electricVehicle).isInstanceOf(ElectricVehicle.class); + assertThat(electricVehicle).isInstanceOf(NextGenElectricCar.class); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/method/FactoryMethodUnitTest.java b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/method/FactoryMethodUnitTest.java new file mode 100644 index 0000000000..9fd77ef139 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factory_pattern/method/FactoryMethodUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.factory_pattern.method; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class FactoryMethodUnitTest { + @Test + public void givenCarFactory_whenCreateMotorVehicle_thenInstanceOf() { + MotorVehicleFactory factory = new CarFactory(); + MotorVehicle car = factory.createMotorVehicle(); + assertThat(car).isInstanceOf(MotorVehicle.class); + assertThat(car).isInstanceOf(Car.class); + } + + @Test + public void givenMotorcycleFactory_whenCreateMotorVehicle_thenInstanceOf() { + MotorVehicleFactory factory = new MotorcycleFactory(); + MotorVehicle motorcycle = factory.createMotorVehicle(); + assertThat(motorcycle).isInstanceOf(MotorVehicle.class); + assertThat(motorcycle).isInstanceOf(Motorcycle.class); + } +} \ No newline at end of file