在做项目的时候,经常会遇到从接口获取下来的Bean信息或者用户自己生成的信息要在多个页面上使用,以前我们一般使用SharedPreference来保存一些基本类型的数据,但是SharedPreference保存不了Bean对象,下面来用构建者模式来搭建一个简单可复用的缓存类
1、用Builder模式搭建实体类:PersonalInfo
public class PersonalInfo
{
public String name;
public String sex;
public String idNumber;
public String nation;
public String contactAddress;
public String mobilePhoneNumber;
public String unitName;
private PersonalInfo( Builder builder )
{
this.name = builder.name;
this.sex = builder.sex;
this.idNumber = builder.idNumber;
this.nation = builder.nation;
this.contactAddress = builder.contactAddress;
this.mobilePhoneNumber = builder.mobilePhoneNumber;
this.unitName = builder.unitName;
}
public static class Builder
{
private String name;
private String sex;
private String idNumber;
private String nation;
private String contactAddress;
private String mobilePhoneNumber;
private String unitName;
public Builder name( String name )
{
this.name = name;
return this;
}
public Builder sex( String sex )
{
this.sex = sex;
return this;
}
public Builder idNumber( String idNumber )
{
this.idNumber = idNumber;
return this;
}
public Builder nation( String nation )
{
this.nation = nation;
return this;
}
public Builder contactAddress( String contactAddress )
{
this.contactAddress = contactAddress;
return this;
}
public Builder mobilePhoneNumber( String mobilePhoneNumber )
{
this.mobilePhoneNumber = mobilePhoneNumber;
return this;
}
public Builder unitName( String unitName )
{
this.unitName = unitName;
return this;
}
public Builder fromPrototype( PersonalInfo prototype )
{
name = prototype.name;
sex = prototype.sex;
idNumber = prototype.idNumber;
nation = prototype.nation;
contactAddress = prototype.contactAddress;
mobilePhoneNumber = prototype.mobilePhoneNumber;
unitName = prototype.unitName;
return this;
}
public PersonalInfo build( )
{
return new PersonalInfo( this );
}
}
}
2、创建简单的缓存类:PersonalInfoCache
public class PersonalInfoCache
{
private static volatile PersonalInfoCache instance;
private PersonalInfo mPersonalInfo;
public static PersonalInfoCache getInstance( )
{
if ( instance == null )
{
synchronized ( PersonalInfoCache.class )
{
instance = new PersonalInfoCache( );
}
}
return instance;
}
private PersonalInfoCache( )
{
if ( mPersonalInfo == null )
{
mPersonalInfo = new PersonalInfo.Builder( ).build( );
}
}
public void setPersonalInfo( String name, String sex, String nation, String idNumber,
String contactAddress, String mobilePhoneNumber, String unitName )
{
//if ( bean != null )
{
mPersonalInfo.name = name;
mPersonalInfo.sex = sex;
mPersonalInfo.nation = nation;
mPersonalInfo.idNumber = idNumber;
mPersonalInfo.contactAddress = contactAddress;
mPersonalInfo.mobilePhoneNumber = mobilePhoneNumber;
mPersonalInfo.unitName = unitName;
}
}
public void setPersonalInfo( PersonalInfo loginInfo )
{
mPersonalInfo = loginInfo;
}
private void write( Context context )
{
if ( mPersonalInfo != null )
{
String loginDataStr = GsonUtil.getInstance( ).getJsonStringFromObject( mPersonalInfo );
SharedPreferenceUtil.getInstance( context.getApplicationContext( ) ).putString(
TqCommon.TQ_PERSONAL_INFO, loginDataStr );
}
}
public PersonalInfo getPersonalInfo( )
{
return mPersonalInfo;
}
public void read( Context context )
{
String loginInfoStr = SharedPreferenceUtil.getInstance(
context.getApplicationContext( ) ).getString( TqCommon.TQ_PERSONAL_INFO );
if ( ! TextUtils.isEmpty( loginInfoStr ) )
{
PersonalInfo info = GsonUtil.getInstance( ).getBeanFromJsonString( loginInfoStr,
PersonalInfo.class );
}
}
public void clear( )
{
mPersonalInfo = null;
}
}
3、使用方法
从接口拿到返回的Bean信息或者是用户自己生成的Bean信息,设置到对应的字段上
PersonalInfoCache.getInstance().setPersonalInfo( String name, String sex, String nation, String idNumber,
String contactAddress, String mobilePhoneNumber, String unitName )
或者直接保存Bean信息
PersonalInfoCache.getInstance().setPersonalInfo(Bean bean)
跳转到其它页面后,从PersonalInfoCache取出对应的信息
PersonalInfoCache mPersonalInfoCache = PersonalInfoCache .getInstance().getPersonalInfo();