dimanche 25 février 2018

How to share data across multiple instances with a common parent?

I have the following code

//parent class
class Parent {
 private Object data;
 Parent (Object data){
  this.data=data;
 }
 method1(){}
 ...
 methodN(){}
}
//wrapper class
class Wrapper extends Parent {
 Wrapper(Object object){
   super(object)
  }
 class Son1{
  //do something
 }
 class SonN {
  //do something
 }
}

Parent has useful methods for their sons, the wrapper is meant to create only one instance of the Parent in order to give access to the inner classes the Parent's methods and data

All works fine, however, the wrapper became huge due to all the sons.

I'm figuring out how to move each son to its own class (In a separate file) without impact the existing code

what I did so far is to make each son to extend directly from Parent and passing the data to the parent through the constructor

class SonN extends Parent{
  SonN(Object object){
  super(object)
  }
}

it works, but I see a downside, each check that gets created will have its own instance of Parent

I'm not really worried about the extra memory used, I think it won't make any notorious difference in the software but I have some questions

  • Is there a design pattern for this cases?

  • If the number of sons will continue growing up, would it be better to remain with the original design?

  • is there a better way to share the methods to the sons keeping the memory used low?

Note

there are multiple instances of wrapper at the same time pointing to different objects what limits the usage of static variables

Aucun commentaire:

Enregistrer un commentaire