BAEL-5850 - Abstract Factory vs Factory Method Pattern in Java (#12) (#13017)

This commit is contained in:
alemoles 2022-11-14 00:41:44 -03:00 committed by GitHub
parent 295ea4d00d
commit 69dfcd3293
17 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package com.baeldung.factory_pattern.abstract_factory;
public abstract class Corporation {
public abstract MotorVehicle createMotorVehicle();
public abstract ElectricVehicle createElectricVehicle();
}

View File

@ -0,0 +1,5 @@
package com.baeldung.factory_pattern.abstract_factory;
public interface ElectricVehicle {
void build();
}

View File

@ -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();
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.factory_pattern.abstract_factory;
public interface MotorVehicle {
void build();
}

View File

@ -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();
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.factory_pattern.method;
public class Car implements MotorVehicle {
@Override
public void build() {
System.out.println("Build Car");
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.factory_pattern.method;
public class CarFactory extends MotorVehicleFactory {
@Override
protected MotorVehicle createMotorVehicle() {
return new Car();
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.factory_pattern.method;
public interface MotorVehicle {
void build();
}

View File

@ -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();
}

View File

@ -0,0 +1,8 @@
package com.baeldung.factory_pattern.method;
public class Motorcycle implements MotorVehicle {
@Override
public void build() {
System.out.println("Build Motorcycle");
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.factory_pattern.method;
public class MotorcycleFactory extends MotorVehicleFactory {
@Override
protected MotorVehicle createMotorVehicle() {
return new Motorcycle();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}