* process가 :remote로 돌아가는 서비스가 있습니다. 이건 바꿀 수가 없어요.
* 그리고 singleton으로 사용 중인 SharedPreferences가 있습니다.
SharedPreferences는 context를 넘겨주기 귀찮아서 상속을 한 번 더 합니다.
public class BasePreferenceHelper
{
private SharedPreferences _sharedPreferences;
protected BasePreferenceHelper(Context $context)
{
super();
_sharedPreferences = $context.getSharedPreferences($context.getPackageName(), Context.MODE_MULTI_PROCESS);
}
// ... get, put 등의 일을 함
}
public class PreferenceHelper extends BasePreferenceHelper
{
private static PreferenceHelper _instance = null;
private Context _context;
public static synchronized PreferenceHelper instance(Context $context)
{
if (_instance == null)
_instance = new PreferenceHelper($context);
return _instance;
}
protected PreferenceHelper(Context $context)
{
super($context);
_context = $context;
}
// key를 이용한 실제 값 저장.
}
:remote 때문에 Context.MODE_MULTI_PROCESS를 사용해야하는 건 괜찮은데, 문제는 if (_instance == null) 이 부분이 없어야 변경된 값을 가져옵니다. 즉 singleton의 의미가 없어져버려요....
매번 context를 넘기지 않고, singleton은 아니어도 되지만 실제 사용할 때 매번 new PreferenceHelper()로 사용하지 않아도 되는 좋은 방법 없나요?