Notes
Search…
Composite

介绍

定义:将对象组合成树形结构来表示“整体/部分”的层次结构。组合能让客户以一致的方式处理个别对象及对象组合。GoF:Compose objects into tree structure to represent part-whole hierarchies. Composite lets client treat individual objects and compositions of object uniformly.
优点:清楚地定义分层次的复杂对象,表示对象的全部或者部分层次。

类图

  • Component:组件,包括叶节点和组合两种。
  • Leaf:叶节点。
  • Composite:组合,持有一群孩子,孩子可以是组合也可以是叶节点。

源码

MyBatis 源码,org.apache.ibatis.scripting.xmltags
1
public interface SqlNode {
2
boolean apply(DynamicContext context);
3
}
4
5
public class MixedSqlNode implements SqlNode {
6
private List<SqlNode> contents;
7
8
public MixedSqlNode(List<SqlNode> contents) {
9
this.contents = contents;
10
}
11
12
@Override
13
public boolean apply(DynamicContext context) {
14
for (SqlNode sqlNode : contents) {
15
sqlNode.apply(context);
16
}
17
return true;
18
}
19
}
20
21
public class WhereSqlNode extends TrimSqlNode {
22
private static List<String> prefixList = Arrays.asList("AND ","OR ","AND\n", "OR\n", "AND\r", "OR\r", "AND\t", "OR\t");
23
24
public WhereSqlNode(Configuration configuration, SqlNode contents) {
25
super(configuration, contents, "WHERE", prefixList, null, null);
26
}
27
}
Copied!
  • SqlNode:对应 Component。
  • WhereSqlNode:对应 Leaf。
  • MixedSqlNode:对应 Composite。
Last modified 1yr ago
Copy link