mardi 10 mars 2020

How to inject Parent class in other classes so that It picks the child class based on passed model?

I have the following classes in Java

class A which is inherited by class A1 and A2

A has method run and both A1, A2 are overriding this method

Another model class B

B has JSON subtypes B1 and B2 (child classes)

What I want to achieve now is if I run

InstanceOfA.run(B) where B is a reference to B1 subtype, it should execute A1's run method

and If I run InstanceOfA.run(B) where B is a reference to B2 subtype, It should execute A2's run method.

InstanceOfA is some instance of A.

If this is possible, I can inject A using @Inject annotation in all other classes (where it is required) and don't need to pass actual implementations.

Any other solution to do is also welcome.

Aucun commentaire:

Enregistrer un commentaire