概念
所谓装饰模式,就是在不改变有功能的情况下,对功能进行额外的包装,以扩展其功能。通过创建一个包装对象来包裹真实对象。
示例
public interface If {
/**
* if判断条件
*
* @return true:继续执行,false:停止执行
*/
boolean meet();
}
public interface Bundle {
void call(Context context);
}
public class IfBundle implements Bundle {
private If anIf;
private Bundle bundle;
public IfBundle(If anIf, Bundle bundle) {
this.anIf = anIf;
this.bundle = bundle;
}
@Override public void call(Context context) {
if (isNull(anIf) || anIf.meet()) {
bundle.call(context);
}
}
}
public class ForBundle implements Bundle {
private int number;
private Bundle bundle;
public ForBundle(int number, Bundle bundle) {
this.number = number;
this.bundle = bundle;
}
@Override public void call(Context context) {
if (number > 0) {
for (int i = 0; i < number; i++) {
bundle.call(context);
}
}
}
}
public class LoopBundle implements Bundle {
private If anIf;
private Bundle bundle;
public LoopBundle(If anIf, Bundle bundle) {
this.anIf = anIf;
this.bundle = bundle;
}
@Override public void call(Context context) {
while (isNull(anIf) || anIf.meet()) {
bundle.call(context);
}
}
}
网友评论