Java – create a new instance class reference
I have an enumeration like this:
public static enum TestEnum { // main ENUM_A (1,"test1",TestADto.class),ENUM_B (2,"test2",TestBDto.class),ENUM_C (3,"test3",TestCDto.class),... private Class<? extends Dto> dtoClass; public Class<? extends Dto getDtoClass() { return dtoClass; } ... }
All of these dto classes extend the same abstract (dto) class:
public abstract class AbstractDto { private String foo; private int bar; ... AbstractDto(AbstractClassNeededForInitialization o) { this.foo = o.getFoo(); this.bar = o.getBar(); } ... some functions here ... }
This will be an example dto implementation of testadto:
@Getter @Setter public class TestADto extends AbstractDto { private String anotherFoo; private int anotherBar; ... public TestADto(AbstractClassNeededForInitialization o) { super(o); } ... some functions here ... }
Is it possible (that is, using java 8) to create concrete instances of these in enumerated reference classes without knowing what it is?
Let me say that at some point in a function, instant messaging has enum_ A. Now I want to create a dto instance (testadto. Class) What is the best way or model for this?
Imagine an enumeration containing more than 100 entries, each with a different dto, which extends the same abstract dto or implements an interface
How to create these concrete objects without writing a large number of if else or switch statements, or deal with them one by one
I read something about reflection and proxy, but I'm not sure if it's the right way Or is there a bad design in the current state? All I want to do is assign the dto name to the enumeration so that it can be created later at some specific point But if possible, don't create huge conditions
@Edit
I forgot to mention that creating each instance of dto requires an object passed to the constructor This object passed to the constructor also implements an interface
Solution
If you need to call an empty constructor for a dto subclass, you can provide a supplier in the enumeration constructor stored in the field:
... ENUM_A (1,TestADto::new),TestBDto::new),TestCDto::new); private supplier<Dto> supplierDto; TestEnum(int i,String name,supplier<Dto> supplierDTO){ this.supplierDto = supplierDTO; ... } ...
You can then call supplierdto Get() to create a dto instance;
After editing:
Vendor < dto > is no longer appropriate because it is not a constructor designed to provide a parameter
Suppose your constructor is like this:
public class TestADto{ ... private MyInterface myInterface; public TestADto (MyInterface myInterface){ this.myInterface = myInterface; } ... }
You can declare a function < myinterface, dto > parameter matching this constructor in the enumeration constructor
... ENUM_A (1,TestCDto::new); private Function <MyInterface,Dto> dtoConstructor; TestEnum(int i,Function <MyInterface,Dto> dtoConstructor){ this.dtoConstructor = dtoConstructor; ... } public Dto createInstance(MyInterface myInterface){ return myInterfaceToDtoFunction.apply(myInterface); } ...