简化抽象 class 的多重继承 classes(内容相同)
Simplify multiple inheriting classes (with all same content) of abstract class
我喜欢在我的应用程序中计算多项内容并将值保存到 android sharedpreferences。一切正常,但总体上我对 class 设计不满意。
非常简单的抽象class。 Class 参数用于命名 sharedPreferences 中的键。
public abstract class Counter {
private Context mContext;
private Class mClass;
Counter(Class myClass, Context context) {
this.mClass = myClass;
this.mContext = context;
}
public Integer getValue() {
return PrefManager.with(mContext).getInt(mClass.getName(), 0);
//return UniversalPreferences.getInstance().get(counterName, 1);
}
public void increment() {
PrefManager.with(mContext).save(mClass.getName(), getValue() + 1);
//UniversalPreferences.getInstance().put(counterName, getCurrentValue(counterName) + 1);
}
}
到目前为止,我已经有 5 个 class 继承自 Counter,内容完全相同。
public class CounterAppLaunch extends Counter {
@SuppressLint("StaticFieldLeak")
private static CounterAppLaunch instance;
private CounterAppLaunch(Context context) {
super(CounterAppLaunch.class, context);
}
public static CounterAppLaunch getInstance(Context context) {
if(CounterAppLaunch.instance == null) {
CounterAppLaunch.instance = new CounterAppLaunch(context);
}
return CounterAppLaunch.instance;
}
}
我有一些计数器,我喜欢从不同的 classes 调用并在那里递增(例如 CounterAPICall 或 CounterOnResumeCallExample)。这适用于这段代码。
借助依赖注入和HasA,我们可以试试,
public class CounterAppLaunch{
@Autowired
CounterAdapter counterAdapter;
@SuppressLint("StaticFieldLeak")
private static CounterAppLaunch instance;
private CounterAppLaunch(Context context) {
super(CounterAppLaunch.class, context);
}
public static CounterAppLaunch getInstance(Context context) {
if(CounterAppLaunch.instance == null) {
CounterAppLaunch.instance = new CounterAppLaunch(context);
}
return CounterAppLaunch.instance;
}
}
CounterAdapter extends Counter{
@Autowired
private Context mContext;
@Autowired
private Class mClass;
// getter and setter
}
public abstract class Counter {
private Context mContext;
private Class mClass;
Counter(Class myClass, Context context) {
this.mClass = myClass;
this.mContext = context;
}
public Integer getValue() {
return PrefManager.with(mContext).getInt(mClass.getName(), 0);
//return UniversalPreferences.getInstance().get(counterName, 1);
}
public void increment() {
PrefManager.with(mContext).save(mClass.getName(), getValue() + 1);
//UniversalPreferences.getInstance().put(counterName, getCurrentValue(counterName) + 1);
}
}
此代码可能有助于检索适当的计数器:
public Counter{
private int count;
public Counter(){
count = 0;
}
public int getValue(){
return count;
}
public void increment(){
counter++;
}
}
public CounterStorage(){
private static HashMap<String, Counter> counterMap = new HashMap<>();
public static Counter getInstance(String str){
if (counterMap.containsKey(str)) return counterMap.get(str);
Counter newCounter = new Counter();
counterMap.add(str, newCounter);
return newCounter;
}
}
在这种情况下,Counter 不是抽象的 class。无论出于何种目的,您都可以为计数器命名,该名称存储在地图中。
我喜欢在我的应用程序中计算多项内容并将值保存到 android sharedpreferences。一切正常,但总体上我对 class 设计不满意。
非常简单的抽象class。 Class 参数用于命名 sharedPreferences 中的键。
public abstract class Counter {
private Context mContext;
private Class mClass;
Counter(Class myClass, Context context) {
this.mClass = myClass;
this.mContext = context;
}
public Integer getValue() {
return PrefManager.with(mContext).getInt(mClass.getName(), 0);
//return UniversalPreferences.getInstance().get(counterName, 1);
}
public void increment() {
PrefManager.with(mContext).save(mClass.getName(), getValue() + 1);
//UniversalPreferences.getInstance().put(counterName, getCurrentValue(counterName) + 1);
}
}
到目前为止,我已经有 5 个 class 继承自 Counter,内容完全相同。
public class CounterAppLaunch extends Counter {
@SuppressLint("StaticFieldLeak")
private static CounterAppLaunch instance;
private CounterAppLaunch(Context context) {
super(CounterAppLaunch.class, context);
}
public static CounterAppLaunch getInstance(Context context) {
if(CounterAppLaunch.instance == null) {
CounterAppLaunch.instance = new CounterAppLaunch(context);
}
return CounterAppLaunch.instance;
}
}
我有一些计数器,我喜欢从不同的 classes 调用并在那里递增(例如 CounterAPICall 或 CounterOnResumeCallExample)。这适用于这段代码。
借助依赖注入和HasA,我们可以试试,
public class CounterAppLaunch{
@Autowired
CounterAdapter counterAdapter;
@SuppressLint("StaticFieldLeak")
private static CounterAppLaunch instance;
private CounterAppLaunch(Context context) {
super(CounterAppLaunch.class, context);
}
public static CounterAppLaunch getInstance(Context context) {
if(CounterAppLaunch.instance == null) {
CounterAppLaunch.instance = new CounterAppLaunch(context);
}
return CounterAppLaunch.instance;
}
}
CounterAdapter extends Counter{
@Autowired
private Context mContext;
@Autowired
private Class mClass;
// getter and setter
}
public abstract class Counter {
private Context mContext;
private Class mClass;
Counter(Class myClass, Context context) {
this.mClass = myClass;
this.mContext = context;
}
public Integer getValue() {
return PrefManager.with(mContext).getInt(mClass.getName(), 0);
//return UniversalPreferences.getInstance().get(counterName, 1);
}
public void increment() {
PrefManager.with(mContext).save(mClass.getName(), getValue() + 1);
//UniversalPreferences.getInstance().put(counterName, getCurrentValue(counterName) + 1);
}
}
此代码可能有助于检索适当的计数器:
public Counter{
private int count;
public Counter(){
count = 0;
}
public int getValue(){
return count;
}
public void increment(){
counter++;
}
}
public CounterStorage(){
private static HashMap<String, Counter> counterMap = new HashMap<>();
public static Counter getInstance(String str){
if (counterMap.containsKey(str)) return counterMap.get(str);
Counter newCounter = new Counter();
counterMap.add(str, newCounter);
return newCounter;
}
}
在这种情况下,Counter 不是抽象的 class。无论出于何种目的,您都可以为计数器命名,该名称存储在地图中。