-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposite.java
More file actions
54 lines (43 loc) · 1.45 KB
/
Composite.java
File metadata and controls
54 lines (43 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Composite
import java.util.*;
public class Composite implements Component{
private List<Component> Childs;
//Costruttore
public Composite(){
Childs = new LinkedList<Component>();
}
//Mostra gli oggetti
public void show(){
System.out.println(this.getClass().getSimpleName()+" ("+this.getSize()+")");
for(Component Child : Childs) Child.show();
}
//Aggiunge l'oggetto alla composizione
public void add(Component obj,boolean txt){
if(txt) System.out.println("È stato aggiunto l'oggetto "+obj.getClass().getSimpleName()+" al oggetto "+this.getClass().getSimpleName()+".");
Childs.add(obj);
}
//Rimuove l'oggetto dalla composizione
public void remove(Component obj){
System.out.println("È stato tolto l'oggetto "+obj.getClass().getSimpleName()+" al oggetto "+this.getClass().getSimpleName()+".");
Childs.remove(obj);
}
//Rimuove tutti gli oggetti
public void removeAll(){
while(getSize() > 0){
if(getComponent(0) instanceof Composite) ((Composite) getComponent(0)).removeAll();
this.remove(getComponent(0));
}
}
//Ritorna il numero di elementi della lista
public int getSize(){
return Childs.size();
}
//Ritorna la lista
public List<Component> getComponents(){
return Childs;
}
//Ritorna i-esimo elemento della lista
public Component getComponent(int i){
return Childs.get(i);
}
}