Skip to content

Commit b5d4217

Browse files
透明型的组合模式
1 parent 34a82de commit b5d4217

File tree

9 files changed

+433
-89
lines changed

9 files changed

+433
-89
lines changed

.idea/workspace.xml

Lines changed: 291 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package composite.example1;
2+
3+
4+
/**
5+
* 组合模式使得用户对单个对象和组合对象的使用具有一致性。
6+
* 类似于,用户操作文件夹和操作子文件夹、文件是一样的操作。
7+
*/
8+
public class Client {
9+
public static void main(String[] args) {
10+
Component root = new Composite("服装");
11+
Component c1 = new Composite("男装");
12+
Component c2 = new Composite("女装");
13+
14+
Component leaf1 = new Leaf("衬衫");
15+
Component leaf2 = new Leaf("夹克");
16+
Component leaf3 = new Leaf("裙子");
17+
Component leaf4 = new Leaf("套装");
18+
19+
root.addChild(c1);
20+
root.addChild(c2);
21+
c1.addChild(leaf1);
22+
c1.addChild(leaf2);
23+
c2.addChild(leaf3);
24+
c2.addChild(leaf4);
25+
26+
root.printStruct("");
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package composite.example1;
2+
3+
import java.util.List;
4+
5+
/**
6+
* 抽象构件角色类
7+
*/
8+
public abstract class Component {
9+
/**
10+
* 输出组件的结构
11+
* @param preStr
12+
*/
13+
public abstract void printStruct(String preStr);
14+
15+
/**
16+
* 添加子组件对象
17+
* @param child
18+
*/
19+
public void addChild(Component child){
20+
//如果组件没有子组件就抛异常。树枝有子组件,而叶子没有子组件
21+
throw new UnsupportedOperationException("对象不支持此功能");
22+
}
23+
24+
/**
25+
* 移除子组件对象
26+
* @param index
27+
*/
28+
public void removeChild(int index){
29+
throw new UnsupportedOperationException("对象不支持此功能");
30+
}
31+
32+
/**
33+
* 获取子组件对象
34+
* @return
35+
*/
36+
public List<Component> getChild(){
37+
throw new UnsupportedOperationException("对象不支持此功能");
38+
}
39+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package composite.example1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 树枝构件角色类
8+
*/
9+
public class Composite extends Component {
10+
/**
11+
* 组合对象的名字
12+
*/
13+
private String name;
14+
/**
15+
* 用来存储子对象的集合
16+
*/
17+
private List<Component> childComponents = new ArrayList<Component>();
18+
19+
@Override
20+
public void printStruct(String preStr) {
21+
System.out.println(preStr + "+" + this.name);
22+
if (this.childComponents != null) {
23+
preStr += " ";
24+
for (Component c : childComponents) {
25+
//递归输出每个子对象
26+
c.printStruct(preStr);
27+
}
28+
}
29+
}
30+
31+
/**
32+
* 构造方法,传入组合对象的名字
33+
* @param name
34+
*/
35+
public Composite(String name){
36+
this.name=name;
37+
}
38+
39+
public void addChild(Component child) {
40+
childComponents.add(child);
41+
}
42+
43+
public void removeChild(Component child){
44+
childComponents.remove(child);
45+
}
46+
47+
/**
48+
* 聚集管理方法,返回所有子构件对象
49+
*/
50+
public List<Component> getChild(){
51+
return childComponents;
52+
}
53+
54+
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package composite.example1;
2+
3+
/**
4+
* 叶子组件
5+
*/
6+
public class Leaf extends Component {
7+
private String name;
8+
9+
public Leaf(String name) {
10+
this.name=name;
11+
}
12+
13+
@Override
14+
public void printStruct(String preStr) {
15+
System.out.println(preStr+"-"+name);
16+
}
17+
}

src/main/java/composite/Market.java renamed to src/main/java/composite/example2/Market.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package composite;
1+
package composite.example2;
22

33
import java.util.List;
44

src/main/java/composite/MarketBranch.java renamed to src/main/java/composite/example2/MarketBranch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package composite;
1+
package composite.example2;
22

33
import java.util.List;
44

src/main/java/composite/PayDemo.java renamed to src/main/java/composite/example2/PayDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package composite;
1+
package composite.example2;
22

33
import java.util.ArrayList;
44
import java.util.List;

src/main/java/prototype/Client.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ public Client(Prototype prototype){
99

1010
public void operation(Prototype example){
1111
Prototype copyPrototype=(Prototype) example.clone();
12-
1312
}
1413
}

0 commit comments

Comments
 (0)