Java 简化抽象类的多个继承类(具有所有相同的内容)
我喜欢在我的应用程序中计算多个内容,并将值保存到android SharedReferences。一切都正常,但我对课堂设计总体上不满意 非常简单的抽象类。类参数用于命名SharedReference中的键Java 简化抽象类的多个继承类(具有所有相同的内容),java,inheritance,abstract-class,Java,Inheritance,Abstract Class,我喜欢在我的应用程序中计算多个内容,并将值保存到android SharedReferences。一切都正常,但我对课堂设计总体上不满意 非常简单的抽象类。类参数用于命名SharedReference中的键 public abstract class Counter { private Context mContext; private Class mClass; Counter(Class myClass, Context context) { th
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个类从计数器继承了所有相同的内容
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;
}
}
我有我喜欢从不同类调用的计数器,并在那里递增(例如CounterAPICall或CounterOnResumeCallerExample)。这段代码很好用。在依赖项注入和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;
}
}
公共计数器{
私人整数计数;
公众柜位(){
计数=0;
}
public int getValue(){
返回计数;
}
公共空间增量(){
计数器++;
}
}
公共反储存(){
私有静态HashMap counterMap=newhashmap();
公共静态计数器getInstance(字符串str){
if(counterMap.containsKey(str))返回counterMap.get(str);
计数器newCounter=新计数器();
counterMap.add(str,newCounter);
返回新计数器;
}
}
在本例中,计数器不是抽象类。出于任何目的,您都可以为计数器指定一个名称,该名称存储在映射中 这在我的情况下是行不通的。我喜欢给我喜欢的每一个班级的同一个柜台打电话。在本例中,对CounterStorage.getInstance(MyClassA,上下文)和CounterStorage.getInstance(MyClassB,上下文)的调用将产生两个不同的计数器。使用我现有的代码,我从任何地方都可以得到相同的计数器和值。编辑我的问题以符合此要求。但我仍将对此进行投票。计数器映射方法可以在其他地方帮助我。所以,你想得到一个全局计数器,但是每个类有一个单独的计数?我想有多个全局计数器。