Android 构建者模式
为什么要写构建者模式?
1、不需要在Activity中去管功能的逻辑。
2、相互独立,有利于功能的扩展,需要就用,不需要就不用。
3、高内聚,低耦合,模块之间相互独立,不会对其他的模块产生影响。
为什么我要写构建者模式!
因为项目中列表里面出现了很多的小东西(比如:列表上面有年龄,性别,生日,名字是否要变颜色,好友关系)这些东西。有的列表上面又有,有的上面又没有,每次都要去单独的判断,所以我就想写一个统一的方法俩管理这些小的图标。就像下面这种-_-!
经典的例子。
AlertDialog
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.mipmap.test)
.setTitle("dialog")
.setPositiveButton()
.setNegativeButton()
.create();
这种一看链式结构就很整洁,需要什么东西就设置什么东西。不需要就不设置就行了。这种就完全符合我上面项目中遇到的场景。于是就自己写了一个构建这模式。这里记录一下。
public class MyLabelUtils {
private Context context;
public MyLabelUtils(Builder builder) {
this.context = builder.context;
}
public static class Builder {
private Context context;
private MyLabelUtils myLabelUtils;
public Builder(Context context) {
this.context = context;
}
public MyLabelUtils build() {
myLabelUtils = new MyLabelUtils(this);
return myLabelUtils;
}
//清空一次
public void clear() {
}
}
}
把列表上面的逻辑搬进来就行了。
public class MyLabelUtils {
private Context context;
private TextView nameTv;
private String nameStr;
private int vip;
private ImageView relationImg;
private int relationType;
private int sexType;
private ImageView sexImg;
private TextView birthdayTv;
private String birthdayStr;
public MyLabelUtils(Builder builder) {
this.context = builder.context;
this.nameTv = builder.nameTv;
this.nameStr = builder.nameStr;
this.vip = builder.vip;
this.relationImg = builder.relationImg;
this.relationType = builder.relationType;
this.sexType = builder.sexType;
this.sexImg = builder.sexImg;
this.birthdayTv = builder.birthdayTv;
this.birthdayStr = builder.birthdayStr;
initData();
}
private void initData() {
//名字变色
if (nameTv != null) {
nameTv.setText(initnameTextColor(nameStr, vip));
}
//设置关系
if (relationImg != null) {
initUserRelation(relationType, relationImg);
}
//设置性别
if (sexImg != null) {
initSex(sexType, sexImg);
}
//设置生日
if (birthdayTv != null) {
initBirthday(birthdayStr,birthdayTv);
}
}
//名字变色的方法
private SpannableStringBuilder initnameTextColor(String name, int level) {
SpannableStringBuilder builder = new SpannableStringBuilder(name + "");
if (null == name) {
return builder;
}
if (level == 1 || level == 2) {
builder.setSpan(new ForegroundColorSpan(0xfff34314), 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
} else {
builder.setSpan(new ForegroundColorSpan(0xff4b4c4c), 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
}
return builder;
}
//关系的方法
private void initUserRelation(int relationType, ImageView relationImg) {
if (relationType == 1) {
relationImg.setVisibility(View.VISIBLE);
relationImg.setImageResource(R.drawable.label_attention);
} else if (relationType == 2) {
relationImg.setVisibility(View.VISIBLE);
relationImg.setImageResource(R.drawable.label_fan);
} else if (relationType == 3) {
relationImg.setVisibility(View.VISIBLE);
relationImg.setImageResource(R.drawable.label_friend);
} else {
relationImg.setVisibility(View.GONE);
}
}
//设置性别
private void initSex(int sexType, ImageView sexImg) {
if (sexType == 0) {
sexImg.setVisibility(View.VISIBLE);
sexImg.setImageResource(R.drawable.label_female);
} else if (sexType == 1) {
sexImg.setVisibility(View.VISIBLE);
sexImg.setImageResource(R.drawable.label_male);
} else {
sexImg.setVisibility(View.GONE);
}
}
//设置生日
private void initBirthday(String birthdayStr, TextView birthdayTv) {
if (!JudgeNullUtil.isNull(birthdayStr)) {
birthdayTv.setVisibility(View.VISIBLE);
birthdayTv.setText(birthdayStr);
}else {
birthdayTv.setVisibility(View.GONE);
}
}
public static class Builder {
private Context context;
private MyLabelUtils myLabelUtils;
private TextView nameTv;
private String nameStr;
private int vip;
private ImageView relationImg;
private int relationType;
private int sexType;
private ImageView sexImg;
private TextView birthdayTv;
private String birthdayStr;
/**
* 设置生日
*
* @param birthdayTv
* @param birthdayStr
* @return
*/
public Builder setBirthday(TextView birthdayTv, String birthdayStr) {
this.birthdayTv = birthdayTv;
this.birthdayStr = birthdayStr;
return this;
}
/**
* 设置性别
*
* @param sexType 性别
* @param sexImg 性别控件
* @return
*/
public Builder setSexType(ImageView sexImg, int sexType) {
this.sexType = sexType;
this.sexImg = sexImg;
return this;
}
/**
* 设置等级
*
* @param vip 等级
* @return
*/
public Builder setVip(int vip) {
this.vip = vip;
return this;
}
/**
* 设置关系
*
* @param relationType 关系
* @param relationImg 关系控件
* @return
*/
public Builder setUserRelation(ImageView relationImg, int relationType) {
this.relationType = relationType;
this.relationImg = relationImg;
return this;
}
/**
* 名字变色
*
* @param nameTv 名字的控件
* @param nameStr 名字
* @return
*/
public Builder setNameColor(TextView nameTv, String nameStr) {
this.nameTv = nameTv;
this.nameStr = nameStr;
return this;
}
public Builder(Context context) {
this.context = context;
}
public MyLabelUtils build() {
myLabelUtils = new MyLabelUtils(this);
return myLabelUtils;
}
public void clear() {
nameTv = null;
nameStr = null;
vip = 0;
relationImg = null;
relationType = 0;
sexImg = null;
sexType = 0;
birthdayStr = null;
birthdayTv = null;
}
}
}
使用的时候就很方便了
MyLabelUtils.Builder myLabelUtils = new MyLabelUtils.Builder(context);
myLabelUtils.clear();
myLabelUtils.setVip(1)
.setNameColor(holder.name,"洒家卖蘑菇")
.setSexType(holder.sex,1)
.setUserRelation(holder.relation,3)
.setBirthday(holder.birthdayText,"90后")
.build();
这样你需要的时候就用不需要的时候就不用就行了。
来源:CSDN
作者:洒家卖蘑菇
链接:https://blog.csdn.net/qq_36510659/article/details/103610243