• 3.5 消灭全局变量 源码


    1.

    public class GlobalVariables {
        public static UserBean User;
    }

    2. 

    public class GlobalVariables implements Serializable, Cloneable {
        /**
         * @Fields: serialVersionUID
         */
        private static final long serialVersionUID = 1L;
    
        private static GlobalVariables instance;
    
        private GlobalVariables() {
    
        }
    
        public static GlobalVariables getInstance() {
            if (instance == null) {
                Object object = Utils.restoreObject(
                                AppConstants.CACHEDIR + TAG);
                if(object == null) {    //App首次启动,文件不存在则新建之
                    object = new GlobalVariables();
                    Utils.saveObject(
                            AppConstants.CACHEDIR + TAG, object);
                }
                
                instance = (GlobalVariables)object;
            }
    
            return instance;
        }
        
        public final static String TAG = "GlobalVariables";
        
        private UserBean user;
    
        public UserBean getUser() {
            return user;
        }
    
        public void setUser(UserBean user) {
            this.user = user;
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
        
        // —————以下3个方法用于序列化————————
        public GlobalVariables readResolve() 
                throws ObjectStreamException,
                CloneNotSupportedException {
            instance = (GlobalVariables) this.clone();
            return instance;
        }
    
        private void readObject(ObjectInputStream ois) 
                throws IOException, ClassNotFoundException {
            ois.defaultReadObject();
        }
    
        public Object Clone() throws CloneNotSupportedException {
            return super.clone();
        }
        
        public void reset() {
            user = null;
    
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
    }

    3.

        public static final void saveObject(String path, Object saveObject) {
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            File f = new File(path);
            try {
                fos = new FileOutputStream(f);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(saveObject);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (oos != null) {
                        oos.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    4.

        public static final Object restoreObject(String path) {
            FileInputStream fis = null;
            ObjectInputStream ois = null;
            Object object = null;
            File f = new File(path);
            if (!f.exists()) {
                return null;
            }
            try {
                fis = new FileInputStream(f);
                ois = new ObjectInputStream(fis);
                object = ois.readObject();
                return object;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ois != null) {
                        ois.close();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return object;
        }    

    5.

    private void gotoLoginActivity() {
            UserBean user = new UserBean();
            user.setUserName("Jianqiang");
            user.setCountry("Beijing");
            user.setAge(32);        
    
            Intent intent = new Intent(LoginNew2Activity.this, 
                    PersonCenterActivity.class);
            
            GlobalVariables.getInstance().setUser(user);
            
            startActivity(intent);
        }

    6.

    protected void initVariables() {
            UserBean user = GlobalVariables.getInstance().getUser(); 
            int age = user.getAge();
        }

    7.

    GlobalVariables.getInstance().reset();

    8.

        public void reset() {
            user = null;
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }

    9.

    public class GlobalVariables3 implements Serializable, Cloneable {
        /**
         * @Fields: serialVersionUID
         */
        private static final long serialVersionUID = 1L;
    
        private static GlobalVariables3 instance;
    
        private GlobalVariables3() {
    
        }
    
        public static GlobalVariables3 getInstance() {
            if (instance == null) {
                Object object = Utils.restoreObject(AppConstants.CACHEDIR + TAG);
                if(object == null) {    //App第一次启动,文件不存在,则新建之
                    object = new GlobalVariables3();
                    Utils.saveObject(AppConstants.CACHEDIR + TAG, object);
                }
                
                instance = (GlobalVariables3)object;
            }
    
            return instance;
        }
        
        public final static String TAG = "GlobalVariables3";
        
        private String userName;
        private String nickName;
        private String country;
    
        public void reset() {
            userName = null;
            nickName = null;
            country = null;
    
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
        
        public String getNickName() {
            return nickName;
        }
    
        public void setNickName(String nickName) {
            this.nickName = nickName;
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
        
        // -----------以下3个方法用于序列化-----------------
        public GlobalVariables3 readResolve() throws ObjectStreamException,
                CloneNotSupportedException {
            instance = (GlobalVariables3) this.clone();
            return instance;
        }
    
        private void readObject(ObjectInputStream ois) throws IOException,
                ClassNotFoundException {
            ois.defaultReadObject();
        }
    
        public Object Clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    10.

    public class GlobalVariables4 implements Serializable, Cloneable {
        /**
         * @Fields: serialVersionUID
         */
        private static final long serialVersionUID = 1L;
    
        private static GlobalVariables4 instance;
    
        private GlobalVariables4() {
    
        }
    
        public static GlobalVariables4 getInstance() {
            if (instance == null) {
                Object object = Utils.restoreObject(AppConstants.CACHEDIR + TAG);
                if(object == null) {    //App第一次启动,文件不存在,则新建之
                    object = new GlobalVariables4();
                    Utils.saveObject(AppConstants.CACHEDIR + TAG, object);
                }
                
                instance = (GlobalVariables4)object;
            }
    
            return instance;
        }
        
        public final static String TAG = "GlobalVariables3";
        
        private String userName;
        private String nickName;
        private String country;
        private HashMap<String, String> rules;
        private String strCinema;
        private String strPersons;
    
        public void reset() {
            userName = null;
            nickName = null;
            country = null;
            
            rules = null;
            strCinema = null;
            strPersons = null;
            guides = null;
    
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName, boolean needSave) {
            this.userName = userName;        
            if(needSave) {
                Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
            }
        }
        
        public String getNickName() {
            return nickName;
        }
    
        public void setNickName(String nickName, boolean needSave) {
            this.nickName = nickName;
            if(needSave) {
                Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
            }
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country, boolean needSave) {
            this.country = country;
            if(needSave) {
                Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
            }
        }
        
        public HashMap<String, String> getRules() {
            return rules;
        }
    
        public void setRules(HashMap<String, String> rules) {
            this.rules = rules;
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
        
        public JSONObject getCinema() {
            if(strCinema == null)
                return null;
    
            try {
                return new JSONObject(strCinema);
            } catch (JSONException e) {
                return null;
            }
        }
    
        public void setCinema(JSONObject cinema) {
            if(cinema == null) {
                this.strCinema = null;
                Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
                return;
            }
    
            this.strCinema = cinema.toString();
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
    
        public JSONArray getPersons() {
            if(strPersons == null)
                return null;
    
            try {
                return new JSONArray(strPersons);
            } catch (JSONException e) {
                return null;
            }
        }
    
        public void setPersons(JSONArray persons) {
            if(persons == null) {
                this.strPersons = null;
                Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
                return;
            }
    
            this.strPersons = persons.toString();
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
        
        HashMap<String, Object> guides;
        
        public HashMap<String, Object> getGuides() {
            return guides;
        }
    
        public void setGuides(HashMap<String, Object> guides) {
            if (guides == null) {
                this.guides = new HashMap<String, Object>();
                Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
                return;
            }
    
            this.guides = new HashMap<String, Object>();
            Set set = guides.entrySet();
            java.util.Iterator it = guides.entrySet().iterator();
            while (it.hasNext()) {
                java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
    
                Object value = entry.getValue();
                String key = String.valueOf(entry.getKey());
                
                this.guides.put(key, String.valueOf(value));
            }
    
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);        
        }
        
        // -----------以下3个方法用于序列化-----------------
        public GlobalVariables4 readResolve() throws ObjectStreamException,
                CloneNotSupportedException {
            instance = (GlobalVariables4) this.clone();
            return instance;
        }
    
        private void readObject(ObjectInputStream ois) throws IOException,
                ClassNotFoundException {
            ois.defaultReadObject();
        }
    
        public Object Clone() throws CloneNotSupportedException {
            return super.clone();
        }
        
        public void save() {
            Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
        }
    }
  • 相关阅读:
    fsockopen
    Ambari安装之部署3个节点的HA分布式集群
    怎么让普通用户达到root用户也可以拥有权限修改文件(CentOS系统)
    谈大数据里各子项目搭建时的环境变量配置(深入)
    Zookeeper的多节点集群详细启动步骤(3或5节点)
    IntelliJ IDEA(Ultimate版本)的下载、安装和WordCount的初步使用(本地模式和集群模式)
    IDEA里如何多种方式打jar包,然后上传到集群
    Zookeeper(1、3、5节点)集群安装
    Hadoop Hive概念学习系列之HDFS、Hive、MySQL、Sqoop之间的数据导入导出(强烈建议去看)
    Hadoop概念学习系列之Java调用Shell命令和脚本,致力于hadoop/spark集群
  • 原文地址:https://www.cnblogs.com/Jax/p/4638108.html
Copyright © 2020-2023  润新知