public class Singleto { //持有私有静态实列, 防止被引用, private static Singleto instance ; //构造私有, 防止被实列化 private Singleto(Context context){ // 业务逻辑 比如操作数据库 // 此代码只是 列子 // this.context = context; // helper = DatabaseHelper.getHelper(context, userphone+".db"); // try { // daoOpe = helper.getDao(RoomTableEntity.class); // } catch (SQLException e) { // e.printStackTrace(); // } } //懒汉式,静态工程方法,创建实例 public static Singleto getInstance(Context context){ if(null == instance) { instance = new Singleto(context); } return instance; } // 在调用的时候 类名.getInstance() // Singleto.getInstance(Context); }
记录 android 单列模式的使用
最新推荐文章于 2024-11-15 14:34:23 发布