Builer 设计模式

Builer设计模式介绍又称构建这模式,Builer功能存储参数。


定义.png
Builer 设计模式_第1张图片
使用场景.png

以添加头部为例

一、创建规则

public interface INavigation {
void createNavigationBar();

/**
 * 绑定参数
 */
void attachNavigationParams();

/**
 * 将 NavigationView添加到父布局
 */
void attachParent(View navigationBar, ViewGroup parent);
}

二、创建实现类

   public class AbsNavigationBar implements INavigation {
   private B mBuilder;
   private View mNavigationBar;
  protected AbsNavigationBar(B builder) {
    this.mBuilder = builder;
    createNavigationBar();
  }

 @Override
 public void createNavigationBar() {

    mNavigationBar = LayoutInflater.from(mBuilder.mContext)
            .inflate(mBuilder.mLayoutId, mBuilder.mParent, false);
    // 添加头布局
    attachParent(mNavigationBar, mBuilder.mParent);
    // 绑定参数
    attachNavigationParams();
}

/**
 * 绑定参数
 */
@Override
public void attachNavigationParams() {

    // 设置文本
    Map textMaps = mBuilder.mTextMaps;
    for (Map.Entry entry : textMaps.entrySet()) {
        TextView textView = findViewById(entry.getKey());
        textView.setText(entry.getValue());
    }
    // 设置点击事件
    Map clickListenerMaps = mBuilder.mCLickListenerMaps;
    for (Map.Entry entry : clickListenerMaps.entrySet()) {
        View view = findViewById(entry.getKey());
        view.setOnClickListener(entry.getValue());
    }

}

public  T findViewById(int viewId) {
    return (T) mNavigationBar.findViewById(viewId);
}
/**
 * 添加进头布局
 * @param navigationBar
 * @param parent
 */
@Override
public void attachParent(View navigationBar, ViewGroup parent) {
    parent.addView(navigationBar, 0);
}
/**
 * 返回 Builder
 *
 * @return
 */
public B getBuilder() {
    return mBuilder;
}

public static abstract class Builder {

    public Context mContext;
    public int mLayoutId;//头布局id
    public ViewGroup mParent;//父布局
    public Map mTextMaps;//存储文字
    public Map mCLickListenerMaps;//存储监听
    public Builder(Context context, int layoutId, ViewGroup parent) {
        this.mContext = context;
        this.mLayoutId = layoutId;
        this.mParent = parent;
        mTextMaps = new HashMap<>();
        mCLickListenerMaps = new HashMap<>();
    }

    /**
     * 用来创建 NavigationBar
     *
     * @return
     */
    public abstract AbsNavigationBar create();

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     * @return
     */
    public B setText(int viewId, String text) {
        mTextMaps.put(viewId, text);
        return (B) this;
    }

    /**
     * 设置点击事件
     *
     * @param viewId
     * @param clickListener
     * @return
     */
    public B setOnClickListener(int viewId, View.OnClickListener clickListener) {
        mCLickListenerMaps.put(viewId, clickListener);
        return (B) this;
    }
}
 }

三、实现使用

  public class DefaultNavigationBar extends AbsNavigationBar {
  protected DefaultNavigationBar(Builder builder) {
    super(builder);
}
@Override
public void attachNavigationParams() {
    super.attachNavigationParams();

    // 处理特有的  是否显示返回
    LinearLayout linearLayout = findViewById(R.id.ll_back);
    linearLayout.setVisibility(getBuilder().mLeftVisible);
}

public static class Builder extends AbsNavigationBar.Builder{

    public int mLeftVisible = View.VISIBLE;

    public Builder(Context context, ViewGroup parent) {
        super(context, R.layout.item_title_haveback, parent);
    }

    @Override
    public DefaultNavigationBar create() {
        return new DefaultNavigationBar(this);
    }


    public Builder setTitleText(String text){
        setText(R.id.tv_title_text,text);
        return this;
    }

    public Builder setLeftClickListener(View.OnClickListener clickListener){
        setOnClickListener(R.id.ll_back, clickListener);
        return this;
    }

    public Builder hideCallBack() {
        mLeftVisible = View.INVISIBLE;
        return this;
    }
  }
}

使用;

  DefaultNavigationBar navigationBar =
            new DefaultNavigationBar.Builder(this, rlHome)
                    .setTitleText("首页")
                    .hideCallBack()
                    .setLeftClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    })

                    .create();

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