I have a class like so:
public class Contact {
private static volatile Contact instance;
private List<Item> contacts = new ArrayList<>();
private Context context;
public static Contact getInstance(Context context) {
Contact localInstance = instance;
if (localInstance == null) {
synchronized (Contact.class) {
localInstance = instance;
if (localInstance == null) {
instance = localInstance = new Contact(context);
}
}
}
return localInstance;
}
public Contact(BaseAuthActivity context) {
this.context = context;
update();
}
Here I create an instance of the class, synchronizing on the class
property.
I have plenty of such classes in my project. Is there a way to create a base class, that would implement the getInstance
method, so I don't need to keep this code in all my classes? I tried using generics, but with no luck. Maybe there's an example of what I try to achieve?