【问题标题】:Simplify multiple inheriting classes (with all same content) of abstract class简化抽象类的多个继承类(内容相同)
【发布时间】:2019-09-26 11:37:42
【问题描述】:

我喜欢在我的应用中计算多个事物并将值保存到 android sharedpreferences。一切正常,但我对一般的类设计不满意。

非常简单的抽象类。类参数用于命名 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 个类继承自 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;
    }
}

我喜欢从不同的类调用计数器并在那里递增(例如 CounterAPICall 或 CounterOnResumeCallExample)。与这段代码配合得很好。

【问题讨论】:

    标签: java inheritance abstract-class


    【解决方案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 不是抽象类。出于任何目的,您可以为计数器命名,该名称存储在地图中。

    【讨论】:

    • 这在我的情况下不起作用。我喜欢从我喜欢的每个班级呼叫同一个柜台。在此示例中,调用 CounterStorage.getInstance(MyClassA, Context) 和 CounterStorage.getInstance(MyClassB, Context) 将产生两个不同的计数器。使用我现有的代码,我可以从任何地方获得相同的计数器和值。
    • 编辑了我的问题以符合此要求。
    • 但我仍然会对此表示赞同。 counterMap 方法可以在其他地方帮助我。
    • 那么,您想获得一个全局计数器,但每个类都单独计数?
    • 我喜欢拥有多个全局计数器。
    【解决方案2】:

    借助依赖注入和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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多