BAEL-7388, Builder Pattern and Inheritance
This commit is contained in:
parent
e2b84b5919
commit
1cb6e41c85
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.builder.inheritance.withgenerics;
|
||||||
|
|
||||||
|
public class Car extends Vehicle {
|
||||||
|
|
||||||
|
private String make;
|
||||||
|
private String model;
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Car(Builder builder) {
|
||||||
|
super(builder);
|
||||||
|
this.make = builder.make;
|
||||||
|
this.model = builder.model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMake() {
|
||||||
|
return make;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder<T extends Builder<T>> extends Vehicle.Builder<T> {
|
||||||
|
|
||||||
|
protected String make;
|
||||||
|
protected String model;
|
||||||
|
|
||||||
|
public T make(String make) {
|
||||||
|
this.make = make;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T model(String model) {
|
||||||
|
this.model = model;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Car build() {
|
||||||
|
return new Car(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.builder.inheritance.withgenerics;
|
||||||
|
|
||||||
|
public class ElectricCar extends Car {
|
||||||
|
private String batteryType;
|
||||||
|
|
||||||
|
public String getBatteryType() {
|
||||||
|
return batteryType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ElectricCar(Builder builder) {
|
||||||
|
super(builder);
|
||||||
|
this.batteryType = builder.batteryType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder<T extends Builder<T>> extends Car.Builder<T> {
|
||||||
|
protected String batteryType;
|
||||||
|
|
||||||
|
public T batteryType(String batteryType) {
|
||||||
|
this.batteryType = batteryType;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ElectricCar build() {
|
||||||
|
return new ElectricCar(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.builder.inheritance.withgenerics;
|
||||||
|
|
||||||
|
public class Vehicle {
|
||||||
|
|
||||||
|
private String colour;
|
||||||
|
private String fuelType;
|
||||||
|
|
||||||
|
public Vehicle(Builder builder) {
|
||||||
|
this.colour = builder.colour;
|
||||||
|
this.fuelType = builder.fuelType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColour() {
|
||||||
|
return colour;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFuelType() {
|
||||||
|
return fuelType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder<T extends Builder> {
|
||||||
|
|
||||||
|
protected String colour;
|
||||||
|
protected String fuelType;
|
||||||
|
|
||||||
|
T self() {
|
||||||
|
return (T) this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T colour(String colour) {
|
||||||
|
this.colour = colour;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T fuelType(String fuelType) {
|
||||||
|
this.fuelType = fuelType;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vehicle build() {
|
||||||
|
return new Vehicle(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.builder.inheritance.withoutgenerics;
|
||||||
|
|
||||||
|
public class Car extends Vehicle {
|
||||||
|
|
||||||
|
private String make;
|
||||||
|
private String model;
|
||||||
|
|
||||||
|
public String getMake() {
|
||||||
|
return make;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Car(CarBuilder builder) {
|
||||||
|
super(builder);
|
||||||
|
this.make = builder.make;
|
||||||
|
this.model = builder.model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CarBuilder extends VehicleBuilder {
|
||||||
|
|
||||||
|
protected String make;
|
||||||
|
protected String model;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CarBuilder colour(String colour) {
|
||||||
|
this.colour = colour;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CarBuilder fuelType(String fuelType) {
|
||||||
|
this.fuelType = fuelType;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CarBuilder make(String make) {
|
||||||
|
this.make = make;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CarBuilder model(String model) {
|
||||||
|
this.model = model;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Car build() {
|
||||||
|
return new Car(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.builder.inheritance.withoutgenerics;
|
||||||
|
|
||||||
|
public class Vehicle {
|
||||||
|
|
||||||
|
private String fuelType;
|
||||||
|
private String colour;
|
||||||
|
|
||||||
|
public String getFuelType() {
|
||||||
|
return fuelType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColour() {
|
||||||
|
return colour;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vehicle(VehicleBuilder builder) {
|
||||||
|
this.colour = builder.colour;
|
||||||
|
this.fuelType = builder.fuelType;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class VehicleBuilder {
|
||||||
|
|
||||||
|
protected String fuelType;
|
||||||
|
protected String colour;
|
||||||
|
|
||||||
|
public VehicleBuilder fuelType(String fuelType) {
|
||||||
|
this.fuelType = fuelType;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VehicleBuilder colour(String colour) {
|
||||||
|
this.colour = colour;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vehicle build() {
|
||||||
|
return new Vehicle(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
@startuml
|
||||||
|
'https://plantuml.com/class-diagram
|
||||||
|
'skinparam Handwritten true
|
||||||
|
skinparam ClassBorderColor black/#63b175
|
||||||
|
skinparam BackgroundColor #fdf8f6
|
||||||
|
skinparam class {
|
||||||
|
ArrowColor #63b175
|
||||||
|
}
|
||||||
|
|
||||||
|
hide empty methods
|
||||||
|
hide empty attributes
|
||||||
|
|
||||||
|
class Vehicle {
|
||||||
|
+fuelType : String
|
||||||
|
+colour : String
|
||||||
|
}
|
||||||
|
|
||||||
|
class VehicleBuilder {
|
||||||
|
+fuelType : String
|
||||||
|
+colour : String
|
||||||
|
+build() : Vehicle
|
||||||
|
+fuelType(String fuelType) : VehicleBuilder
|
||||||
|
+colour(String colour) : VehicleBuilder
|
||||||
|
}
|
||||||
|
class Car {
|
||||||
|
+make : String
|
||||||
|
+model : String
|
||||||
|
}
|
||||||
|
class CarBuilder {
|
||||||
|
+make : String
|
||||||
|
+model : String
|
||||||
|
+build() : Car
|
||||||
|
+make(String make): CarBuilder
|
||||||
|
+model(String make): CarBuilder
|
||||||
|
}
|
||||||
|
|
||||||
|
class ElectricCar {
|
||||||
|
+batteryType : String
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ElectricCarBuilder {
|
||||||
|
+batteryType : String
|
||||||
|
+build() : ElectricCar
|
||||||
|
+batteryType(String batteryType): ElectricCarBuilder
|
||||||
|
}
|
||||||
|
CarBuilder -left-|> VehicleBuilder: extends
|
||||||
|
ElectricCarBuilder <|-left- CarBuilder: extends
|
||||||
|
VehicleBuilder -down-> Vehicle: builds
|
||||||
|
CarBuilder -down-> Car:builds
|
||||||
|
ElectricCarBuilder -down-> ElectricCar: builds
|
||||||
|
|
||||||
|
Vehicle -right-|> Car: extends
|
||||||
|
Car <|-right- ElectricCar: extends
|
||||||
|
|
||||||
|
@enduml
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.builder.inheritance.withoutgenerics;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class BuilderInheritanceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNonGenericImpl_whenBuild_thenReturnObject() {
|
||||||
|
Car car = new Car.CarBuilder().colour("red")
|
||||||
|
.fuelType("Petrol")
|
||||||
|
.make("Ford")
|
||||||
|
.model("F")
|
||||||
|
.build();
|
||||||
|
assertEquals("red", car.getColour());
|
||||||
|
assertEquals("Ford", car.getMake());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baledung.builder.inheritance.withgenerics;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.baeldung.builder.inheritance.withgenerics.Car;
|
||||||
|
import com.baeldung.builder.inheritance.withgenerics.ElectricCar;
|
||||||
|
|
||||||
|
public class BuilderInheritanceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGenericImpl_whenBuild_thenReturnObject() {
|
||||||
|
Car.Builder<?> carBuilder1 = new Car.Builder();
|
||||||
|
Car car = carBuilder1.colour("red")
|
||||||
|
.fuelType("Petrol")
|
||||||
|
.make("Ford")
|
||||||
|
.model("F")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ElectricCar.Builder<?> ElectricCarBuilder = new ElectricCar.Builder();
|
||||||
|
ElectricCar eCar = ElectricCarBuilder.make("Mercedes")
|
||||||
|
.colour("White")
|
||||||
|
.model("G")
|
||||||
|
.fuelType("Electric")
|
||||||
|
.batteryType("Lithium")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals("red", car.getColour());
|
||||||
|
assertEquals("Ford", car.getMake());
|
||||||
|
|
||||||
|
assertEquals("Electric", eCar.getFuelType());
|
||||||
|
assertEquals("Lithium", eCar.getBatteryType());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user