前言:
平时在使用一个对象的时候,尤其是在自定义控件中,如果它有很多属性,每个都要用“对象.属性”来设置的话,感觉有点繁琐,实际使用过程中,我们可以针对每个属性,设置单独的方法,方法返回相同的或者上一级的对象,在builder模式中也经常这样用
一、返回相同对象
1、例子
new BottomDialog(MainActivity.this)
.title(R.string.title_item) //设置标题
.layout(BottomDialog.GRID) //设置内容layout,默认为线性(LinearLayout)
.orientation(BottomDialog.VERTICAL) //设置滑动方向,默认为横向
.inflateMenu(R.menu.menu_share) //传人菜单内容
.itemClick(new OnItemClickListener() { //设置监听
@Override
public void click(Item item) {
Toast.makeText(MainActivity.this, getString(R.string.share_title) + item.getTitle(), Toast.LENGTH_LONG).show();
}
})
.show();
2、原理:全是BottomDialog,返回的都是BottomDialog对象
private CustomDialog customDialog;
public BottomDialog(Context context) {
customDialog = new CustomDialog(context);
}
public BottomDialog setGridItemNum(int num){
GRID_ITEM_NUM=num;
return this;
}
public BottomDialog title(String title) {
customDialog.title(title);
return this;
}
public BottomDialog title(int title) {
customDialog.title(title);
return this;
}
public BottomDialog background(int res) {
customDialog.background(res);
return this;
}
public BottomDialog inflateMenu(int menu) {
customDialog.inflateMenu(menu);
return this;
}
public BottomDialog layout(int layout) {
customDialog.layout(layout);
return this;
}
public BottomDialog orientation(int orientation) {
customDialog.orientation(orientation);
return this;
}
public BottomDialog addItems(List<Item> items) {
customDialog.addItems(items);
return this;
}
public BottomDialog itemClick(OnItemClickListener listener) {
customDialog.setItemClick(listener);
return this;
}
public void show() {
customDialog.show();
}
二、返回上一级对象
1、例子:可以参考Picasso源码中设置属性的方法
picasso
.load(url) //Picasso内部的方法,返回RequestCreator对象
.tag(tag)//RequestCreator内部的方法,返回RequestCreator对象
.placeholder(R.drawable.bg_avatar_default)//RequestCreator内部的方法,返回RequestCreator对象
.error(R.drawable.bg_avatar_default)//RequestCreator内部的方法,返回RequestCreator对象
.transform(new CircleTransformation())//RequestCreator内部的方法,返回RequestCreator对象
.into(iv);
2、原理:
tag()返回的是,load()返回的RequestCreator对象
而load()返回的是Picasso对象
public RequestCreator load(String path) {
if (path == null) {
return new RequestCreator(this, null, 0);
}
if (path.trim().length() == 0) {
throw new IllegalArgumentException("Path must not be empty.");
}
return load(Uri.parse(path));
}
public RequestCreator tag(Object tag) {
if (tag == null) {
throw new IllegalArgumentException("Tag invalid.");
}
if (this.tag != null) {
throw new IllegalStateException("Tag already set.");
}
this.tag = tag;
return this;
}
public RequestCreator placeholder(int placeholderResId) {
if (!setPlaceholder) {
throw new IllegalStateException("Already explicitly declared as no placeholder.");
}
if (placeholderResId == 0) {
throw new IllegalArgumentException("Placeholder image resource invalid.");
}
if (placeholderDrawable != null) {
throw new IllegalStateException("Placeholder image already set.");
}
this.placeholderResId = placeholderResId;
return this;
}
public RequestCreator transform(Transformation transformation) {
data.transform(transformation);
return this;
}
……