Skip to content

Instantly share code, notes, and snippets.

@jiraguha
Created April 16, 2019 23:01
Show Gist options
  • Save jiraguha/aa08e72aa1e097127db53b0cab11206f to your computer and use it in GitHub Desktop.
Save jiraguha/aa08e72aa1e097127db53b0cab11206f to your computer and use it in GitHub Desktop.
generic Java Pojo by a polymorphic implementation or inheritance to conserve builder pattern
// implementation, I can only put only fruits inside (by polymorphism)
public class Container<T extends Fruit> {
private String containerId;
private T fruit;
public String containerId() {
return eventId;
}
public Container<T> setContainerId(String containerId) {
this.containerId = containerId;
return this;
}
public String getContainerId() {
return containerId;
}
public Container<T> seFruit(T fruit) {
this.fruit = fruit;
return this;
}
public T getFruit() {
return fruit;
}
}
// where Pomme implement Fruit
Container<Apple> appleContainer = new Container<Apple>();
// inheritance, Can be extended with as much proprieties I want
public class ApplePearContent<T extends ApplePearContent<?>> {
private Apple apple;
private Pear pear;
public Apple getApple() {
return apple;
}
public T setApple(Apple apple) {
this.apple = apple;
return (T) this;
}
public Pear getPear() {
return pear;
}
public T setPear(Pear pear) {
this.pear = pear;
return (T) this;
}
}
public class ApplePearSaladContent extends ApplePearContent<ApplePearSaladContent> {
private Salad salad;
public Salad getSalad() {
return salad;
}
public this seSaladSalad salad) {
this.salad = salad;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment