我有这样的课:
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();
}
在这里,我创建了一个类的实例,在class
属性上进行同步。
我的项目中有很多这样的课程。有没有办法创建一个基类来实现该getInstance
方法,所以我不需要在我的所有类中保留这个代码?我尝试使用泛型,但没有运气。也许有一个我试图实现的例子?