设计模式-Builder设计模式

定义

Builder模式可以将一个类的构建和表示进行分离

NavigationBar实践

1.目的效果

 new DefaultNavigation.Builder(this)
                .setTitle("所有图片")
                .setLeftOnClickListener(this)
                .create();

2.分析

设计模式-Builder设计模式_第1张图片
navigation.png

3.代码(从项目中抽取)

/**
 * 项目名称:joke
 * 类描述:
 * 创建人:woochen123
 * 创建时间:2017/5/17 23:41
 */
public class DefaultNavigation extends AbsNavigationBar {
    public DefaultNavigation(Builder.NavigationParams params) {
        super(params);
    }

    @Override
    public int bindLayoutId() {
        return R.layout.default_navigation;
    }

    @Override
    public void applyView() {
        // 给我们的导航条绑定资源
        setImageResource(R.id.iv_left, getParams().leftIconRes);
        setImageResource(R.id.iv_right, getParams().rightIconRes);
        setImageResource(R.id.iv_right_icon, getParams().textRightIconRes);
        setText(R.id.tv_title, getParams().title);
        setText(R.id.left_tv, getParams().leftTv);
        setText(R.id.tv_right, getParams().rightTv);
        setBackgroundColor(R.id.title_bar, getParams().bgColor);
        setOnClickListener(R.id.iv_left, getParams().leftOnClickListener);
        setOnClickListener(R.id.tv_right, getParams().rightOnClickListener);
    }


    /**
     * 设置背景
     * @param title_bar
     * @param bgColor
     */
    private void setBackgroundColor(int title_bar, int bgColor) {
        ViewGroup view = findViewById(title_bar);
        if (view != null) {
            view.setBackgroundColor(bgColor);
        }
    }

    public static class Builder extends AbsNavigationBar.Builder {
        public NavigationParams params;

        public Builder(Context context, ViewGroup parent) {
            super(context, parent);
            params = new NavigationParams(context,parent);
        }

        public Builder(Context context) {
            super(context,null);
            params = new NavigationParams(context,null);
        }


        public Builder setTitle(String title) {
            params.title = title;
            return this;
        }

        public Builder setRight(String right) {
            params.rightTv = right;
            return this;
        }

        public Builder setLeft(String left) {
            params.leftTv = left;
            return this;
        }

        public Builder setLeftIcon(int iconRes) {
            params.leftIconRes = iconRes;
            return this;
        }


        public Builder setRightIcon(int iconRes) {
            params.rightIconRes = iconRes;
            return this;
        }

        public Builder setTitleBackgroundColor(int bgColor) {
            params.bgColor = bgColor;
            return this;
        }

        public Builder setLeftOnClickListener(View.OnClickListener onClickListener) {
            params.leftOnClickListener = onClickListener;
            return this;
        }

        public Builder setRightOnClickListener(View.OnClickListener onClickListener) {
            params.rightOnClickListener = onClickListener;
            return this;
        }

        @Override
        public  T create() {
            DefaultNavigation defaultNavigation = new DefaultNavigation(params);
            return (T) defaultNavigation;
        }

        public static class NavigationParams extends AbsNavigationBar.Builder.NavigationParams {
            //标题
            public String title;
            //左边图片资源
            public int leftIconRes;
            //右边图片资源
            public int rightIconRes;
            //左边的点击事件
            public View.OnClickListener leftOnClickListener;
            //右边的点击事件
            public View.OnClickListener rightOnClickListener;
            public String leftTv;
            public String rightTv;
            public int bgColor;

            public NavigationParams(Context context, ViewGroup parent) {
                super(context, parent);
            }
        }

    }
}

4.总结

1.目标类的实例化操作实际上交给了Builder对象来完成,目标对象只暴露很少的api给开发者来调用(将目标类的构建抽离出来,将只有在目标类被实例化的时候才能配置或者只能配置一次的各项参数隐藏起来)

5.优点

1.有利于扩展(builder对象是独立的,可以很方便的进行api的增加和修改,而不会影响目标类的过多改动)

你可能感兴趣的:(设计模式-Builder设计模式)