I have a use case related to spring applications. I have 2 child classes consider it as
@Component
public class OhioServiceCenter extends TeslaServiceCenter
@Component
public class FloridaServiceCenter extends TeslaServiceCenter
Now as we can see we have 2 child classes both of which are extending one parent abstract class. Now I have one bean which I am auto-wiring into both of the child classes which returns me an inventory, you can consider it a list of items. Let's for the sake of consideration say this bean is Tesla
and have one method in Tesla
called getCars
this method returns me a List<Car>
. Now both of the the child classes implmenting a parent abstract methods called doService
both of them have different implementations. But both of them have same dependency on the List<Cars
.
Now as per my usecase. I don't want both of the child classes holding the List<Cars
because its a memory problem both of the child classes holding same inventory is memeory wastage.(NOTE - that getCars
will return new List everytime means to say there content will be same but in memory there will be new list created everytime and that can't be changed). What I expect is that parent class should hold the inventory and then both of the child class should be able to operate over it .
APPROACHES I TRIED
- I tried to kept the parent class as an abstract class and have abstract method
getCars
which is being implemented by both the child classes. My thinking was I could autowire theTesla
object in child classes and could maintain a inventory in abstract class but the problem is for both of the child classes there are different memories are allocated for parent classes too so even if first child class could update the parent object second wont be able to access it. - Also one another problem is abstract classes can't autowire things so I can't also controll the part of getting inventory directly into abstract class
- I tried to create parent class as a normal class but then the problem is
doService
can't be abstracted as java don't allows the abstract method in normal classes
PROBLEM IN SHORT SUMMARY I want 2 child classes to be able to share the same memory references of a parent class so that both the parent classes could access and update the same object in parent class.
Aucun commentaire:
Enregistrer un commentaire