Android设计模式之Builder模式详解
程序员文章站
2023-11-29 08:37:52
builder模式使用链式结构创建复杂对象,将过程与结果分开,创建过程中可以自行组合。
使用场景
一个对象,不同组合,不同顺序生成不同的结果
优点:封装性更规范,程序...
builder模式使用链式结构创建复杂对象,将过程与结果分开,创建过程中可以自行组合。
使用场景
一个对象,不同组合,不同顺序生成不同的结果
优点:封装性更规范,程序调用不用关系内部细节,注重结果即可
缺点:如果builder对象过多,会加大内存消耗
public class tabinfobean { private int count;//tab的个数 必选 private int currenttab;//默认选中的tab 必选 private string[] tabtext;//文字必选 private int normalresid;//可选 private int selectresid;//可选 private int normaltextcolor;//可选 private int selecttextcolor;//可选 private int normaltextsizesp;//可选 private int selecttextsizesp;//可选 private tabinfobean(tabinfobuilder builder) { this.tabtext = builder.tabtext; this.count = builder.count; this.currenttab = builder.currenttab; this.normalresid = builder.normalresid; this.selectresid = builder.selectresid; this.normaltextcolor = builder.normaltextcolor; this.selecttextcolor = builder.selecttextcolor; this.normaltextsizesp = builder.normaltextsizesp; this.selecttextsizesp = builder.selecttextsizesp; } public int getcount() { return count; } public void setcount(int count) { this.count = count; } public int getcurrenttab() { return currenttab; } public void setcurrenttab(int currenttab) { this.currenttab = currenttab; } public int getnormalresid() { return normalresid; } public void setnormalresid(int normalresid) { this.normalresid = normalresid; } public int getselectresid() { return selectresid; } public void setselectresid(int selectresid) { this.selectresid = selectresid; } public int getnormaltextcolor() { return normaltextcolor; } public void setnormaltextcolor(int normaltextcolor) { this.normaltextcolor = normaltextcolor; } public int getselecttextcolor() { return selecttextcolor; } public void setselecttextcolor(int selecttextcolor) { this.selecttextcolor = selecttextcolor; } public string[] gettabtext() { return tabtext; } public void settabtext(string[] tabtext) { this.tabtext = tabtext; } public int getnormaltextsizesp() { return normaltextsizesp; } public void setnormaltextsizesp(int normaltextsizesp) { this.normaltextsizesp = normaltextsizesp; } public int getselecttextsizesp() { return selecttextsizesp; } public void setselecttextsizesp(int selecttextsizesp) { this.selecttextsizesp = selecttextsizesp; } public static class tabinfobuilder { private int count; private int currenttab; private string[] tabtext; private int normalresid; private int selectresid; private int normaltextcolor; private int selecttextcolor; private int normaltextsizesp;//可选 private int selecttextsizesp;//可选 public tabinfobuilder(string[] tabtext, int count, int currenttab) { this.tabtext = tabtext; this.count = count; this.currenttab = currenttab; } public tabinfobuilder setnormalresid(int normalresid) { this.normalresid = normalresid; return this; } public tabinfobuilder setselectresid(int selectresid) { this.selectresid = selectresid; return this; } public tabinfobuilder setnormaltextcolor(int normaltextcolor) { this.normaltextcolor = normaltextcolor; return this; } public tabinfobuilder setselecttextcolor(int selecttextcolor) { this.selecttextcolor = selecttextcolor; return this; } public tabinfobuilder setnormaltextsizesp(int size) { this.normaltextsizesp = size; return this; } public tabinfobuilder setselecttextsizesp(int size) { this.selecttextsizesp = size; return this; } public tabinfobean build() { return new tabinfobean(this); } } }
调用方式
string[] name={"我","是","谁"}; tabinfobean.tabinfobuilder tabinfobuilder=new tabinfobean.tabinfobuilder(name,5,0); /* tabinfobean tabinfobean=tabinfobuilder .setnormalresid() .setselectresid() .setnormaltextcolor() .setselecttextcolor() .setnormaltextsizesp() .setselecttextsizesp() .build();*/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。