Java: create a generic type by returning a provider or returning a new instance

I read how to create an instance of the generic and after reading and apply this answer ; I would like to know what the differences between waiting Supplier<T>and waiting for a new instance will be T.

Example:

abstract class AbstractService<T extends AbstractEntity> {
    protected Supplier<T> makeNewThing();  // supplier is expected

    public T myMethod(){
        T object = makeNewThing().get(); // local object by calling supplier
        object.doStuff();
        return object;
    }
}

class CarService extends AbstractService<Car> {
    public Supplier<Car> makeNewThing(){
        return Car::new;
    }
}

vs.

abstract class AbstractService<T extends SomeAbstractEntity> {
    protected T makeNewThing();  // object is expected, newness is assumed

    public T myMethod(){
        T object = makeNewThing(); // local object by calling constructor
        object.doStuff();
        return object;
    }
}

class CarService extends AbstractService<Car> {
    public Car makeNewThing(){
        return new Car();
    }
}

The only thing I can think of is that the pending provider guarantees that a new object will be created, but when waiting for the object, we can only assume that the implementation classes call the constructor and not reuse the existing instance.

I would like to know about other objective differences and possible use cases , if any. Thanks in advance.

+6
3

Supplier .

, .

, , makeNewThing() .

public void makeNewThingSometimes (T newInstance)
{
    if (someCondition) {
        this.instance = newInstance;
    }
}

public void makeNewThingSometimes (Supplier<T> supplier)
{
    if (someCondition) {
        this.instance = supplier.get();
    }
}

T, .

T.

Consumer ( ), ( ).

+9

, , , , ,

. Supplier :

 return SomeEntityImplementation::new;

:

 if (myCachedObject != null){
    return (()-> myCachedObject);
 }
 return SomeEntityImplementation::new;

.


Supplier Supplier : Supplier.get().

, Supplier , ( Supplier), : factory,
, , Supplier<T> as, Eran Dasblinkenlight.


Supplier factory, .
Supplier , , Java Reflection.

, Enum, :

public enum MyBaseClassFactory {

  ENUM_A (A::new),
  ENUM_B (B::new),
  ENUM_C (C::new),
  ENUM_D (D::new);

  private Supplier<BaseClass> supplier;

  MyBaseClassFactory (Supplier<BaseClass> supplier){
    this.supplier = supplier;
  }

  public BaseClass createObject(){
       return supplier.get();
  }
}

:

BaseClass base = MyBaseClassFactory.ENUM_A.createObject();

Supplier Reflection ( ) .

, Reflection:

public enum MyEnumFactoryClass {

    ENUM_A(A.class), ENUM_B(B.class), ENUM_C(C.class), ENUM_D(D.class);

    private Class<BaseClass> clazz;

    MyEnumFactoryClass(Class<BaseClass> clazz) {
       this.clazz = clazz;
    }

    public BaseClass createObject() {
       return clazz.newInstance();
    }

}

, , :

public enum MyEnumFactoryClass {

  ENUM_A {
     @Override
     public BaseClass createObject() {
        return new A();
     }
    },
    ENUM_B {
     @Override
     public BaseClass createObject() {
        return new B();
     }
    },
    ENUM_C {
    @Override
     public BaseClass createObject() {
        return new C();
     }
    },
    ENUM_D {
    @Override
     public BaseClass createObject() {
        return new D();
     }
    };
    public abstract BaseClass createObject();

}

, , Supplier, Map<String, Supplier<BaseClass>>.

+4

, ServiceImpl<SomeEntityImplementation>.

Supplier<T> ServiceImpl, . ServiceImpl :

class ServiceImpl<SomeEntityImplementation> {
    private final Supplier<SomeEntityImplementation> supplier;
    public Supplier<T> makeNewThing(){
        return supplier;
    }
    public ServiceImpl(Supplier<SomeEntityImplementation> s) {
        supplier = s;
    }
}

ServiceImpl Supplier<T>, , .

+3

All Articles