您的位置:首页 > 其它

3.5 消灭全局变量 源码

2015-07-11 10:09 316 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: