类图
![8195388-f39fe8240f7adc66.png](https://i-blog.csdnimg.cn/blog_migrate/a63177700b40385d9a15a0accc65b0c0.webp?x-image-process=image/format,png)
Mybatis中对组合模式的使用.png
代码
树结构中节点的抽象
public interface SqlNode {
boolean apply(DynamicContext context);
}
树结构中非叶子节点的抽象
- 识别组合模式的一个要点:实现了一个接口,又聚合了这个接口的集合,那么该类很有可能是组合模式中的组合对象;
public class MixedSqlNode implements SqlNode {
private List<SqlNode> contents;
public MixedSqlNode(List<SqlNode> contents) {
this.contents = contents;
}
@Override
public boolean apply(DynamicContext context) {
for (SqlNode sqlNode : contents) {
sqlNode.apply(context);
}
return true;
}
}