File tree Expand file tree Collapse file tree 9 files changed +433
-89
lines changed Expand file tree Collapse file tree 9 files changed +433
-89
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package composite ;
1
+ package composite . example2 ;
2
2
3
3
import java .util .List ;
4
4
Original file line number Diff line number Diff line change 1
- package composite ;
1
+ package composite . example2 ;
2
2
3
3
import java .util .List ;
4
4
Original file line number Diff line number Diff line change 1
- package composite ;
1
+ package composite . example2 ;
2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
Original file line number Diff line number Diff line change @@ -9,6 +9,5 @@ public Client(Prototype prototype){
9
9
10
10
public void operation (Prototype example ){
11
11
Prototype copyPrototype =(Prototype ) example .clone ();
12
-
13
12
}
14
13
}
You can’t perform that action at this time.
0 commit comments