This commit is contained in:
parent
295ea4d00d
commit
69dfcd3293
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.factory_pattern.abstract_factory;
|
||||
|
||||
public abstract class Corporation {
|
||||
|
||||
public abstract MotorVehicle createMotorVehicle();
|
||||
|
||||
public abstract ElectricVehicle createElectricVehicle();
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.factory_pattern.abstract_factory;
|
||||
|
||||
public interface ElectricVehicle {
|
||||
void build();
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.factory_pattern.abstract_factory;
|
||||
|
||||
public interface MotorVehicle {
|
||||
void build();
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.factory_pattern.method;
|
||||
|
||||
public class Car implements MotorVehicle {
|
||||
@Override
|
||||
public void build() {
|
||||
System.out.println("Build Car");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.factory_pattern.method;
|
||||
|
||||
public class CarFactory extends MotorVehicleFactory {
|
||||
@Override
|
||||
protected MotorVehicle createMotorVehicle() {
|
||||
return new Car();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.factory_pattern.method;
|
||||
|
||||
public interface MotorVehicle {
|
||||
void build();
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.factory_pattern.method;
|
||||
|
||||
public class Motorcycle implements MotorVehicle {
|
||||
@Override
|
||||
public void build() {
|
||||
System.out.println("Build Motorcycle");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.factory_pattern.method;
|
||||
|
||||
public class MotorcycleFactory extends MotorVehicleFactory {
|
||||
@Override
|
||||
protected MotorVehicle createMotorVehicle() {
|
||||
return new Motorcycle();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue