私は次のようなクラスを持っています:
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
方法があるので、すべてのクラスでこのコードを保持する必要はありませんか? ジェネリックを使用してみましたが、うまくいきませんでした。多分私が達成しようとしていることの例がありますか?