Skip to content

Commit a3c44ae

Browse files
authored
组合模式
组合模式
1 parent 10f2cb6 commit a3c44ae

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.java.design.composite;
2+
3+
public abstract class Component {
4+
5+
protected String name;
6+
7+
public Component(String name) {
8+
this.name = name;
9+
}
10+
11+
public abstract void add(Component component);
12+
13+
public abstract void delete(Component component);
14+
15+
public abstract void show(int index);
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.java.design.composite;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Composite extends Component {
7+
8+
List<Component> components = new ArrayList<Component>();
9+
10+
public Composite(String name) {
11+
super(name);
12+
}
13+
14+
@Override
15+
public void add(Component component) {
16+
17+
components.add(component);
18+
}
19+
20+
@Override
21+
public void show(int index) {
22+
23+
StringBuilder stringBuilder = new StringBuilder();
24+
for (int i = 0; i < index; i++) {
25+
stringBuilder.append("-");
26+
}
27+
System.out.println(stringBuilder.toString() + this.name);
28+
for (Component component : components) {
29+
component.show(index + 2);
30+
}
31+
}
32+
33+
@Override
34+
public void delete(Component component) {
35+
components.remove(component);
36+
}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.java.design.composite;
2+
3+
/**
4+
* 组合模式(Composite
5+
* Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次
6+
* 。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
7+
*
8+
* @author Administrator
9+
*
10+
*/
11+
public class CompositePattern {
12+
13+
public static void main(String[] args) {
14+
15+
Composite composite = new Composite("root");
16+
composite.add(new Leaf("leaf1"));
17+
composite.add(new Leaf("leaf2"));
18+
19+
Composite composite1 = new Composite("composite1");
20+
composite1.add(new Leaf("leaf3"));
21+
composite1.add(new Leaf("leaf4"));
22+
composite1.add(new Leaf("leaf5"));
23+
composite.add(composite1);
24+
25+
Leaf leaf6 = new Leaf("leaf6");
26+
composite.add(leaf6);
27+
// composite.delete(leaf6);
28+
composite.show(1);
29+
}
30+
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.java.design.composite;
2+
3+
public class Leaf extends Component {
4+
5+
public Leaf(String name) {
6+
super(name);
7+
}
8+
9+
@Override
10+
public void add(Component component) {
11+
12+
System.out.println("A leaf could not add component");
13+
}
14+
15+
@Override
16+
public void delete(Component component) {
17+
18+
System.out.println("A leaf could not delete component");
19+
}
20+
21+
@Override
22+
public void show(int index) {
23+
24+
StringBuilder stringBuilder = new StringBuilder();
25+
for (int i = 0; i < index; i++) {
26+
stringBuilder.append("-");
27+
}
28+
System.out.println(stringBuilder.toString() + this.name);
29+
}
30+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy