android中使用Builder构建者模式创建一个简单的缓存类

在做项目的时候,经常会遇到从接口获取下来的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();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,480评论 18 399
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,986评论 25 709
  • 从灯市街回来之后,刘栉颇有些玩味,一边把玩着手中的小玩意,一边念书。这些时日,天气好的令人嫉妒,她也不免感到这周遭...
    静女式微阅读 1,360评论 0 0
  • 荒芜的情感 如一望无际的沙漠 昔日的快乐片段 在心被撕裂的瞬间 支离破碎 随风而逝 无影 无踪
    爱无踪阅读 1,396评论 0 0

友情链接更多精彩内容