lundi 31 juillet 2017

Share Common Variables in Multi-Instance moved from Singleton in Java

I am refactoring my code by moving from signleton to multi-instance due to some requirement. There are certain part of my objects they all share the same common properties. After the object creation, each object also has its own instance variables.

For example, my singleton way

MyLibrary.getinstance().init(a,b,c,d);

my multi-instance way

MyLibrary library1 = new MyLibrary(a,b,c,d);
MyLibrary library2 = new MyLibrary(a,b,c,d);

My question is that some of the parameter passed in to the constructor is repetitive, for example, a,b. In the old singleton way they are used to init the library, and do not get used afterward. In other words, all MyLibrary objects share a,b and only need one time init.

Can you please share some insights of how to define the common variables across all objects of MyLibrary?

Aucun commentaire:

Enregistrer un commentaire