forked from john-bai/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposite.java
More file actions
64 lines (55 loc) · 1.66 KB
/
Copy pathComposite.java
File metadata and controls
64 lines (55 loc) · 1.66 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
55
56
57
58
59
60
61
62
63
64
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package designpatterns;
import java.util.ArrayList;
/**
*
* @author jtherrell
*/
abstract class Graphic {
abstract public void draw(Log log);
abstract public boolean add(Graphic g);
abstract public boolean remove(Graphic g);
abstract public Graphic getChild(int index);
}
class Picture extends Graphic {
private ArrayList<Graphic> children;
public Picture() {
children = new ArrayList<Graphic>();
}
public void draw(Log log) {
for (int i = 0; i < children.size(); i++)
children.get(i).draw(log);
}
public boolean add(Graphic g) {
children.add(g);
return children.contains(g);
}
public boolean remove(Graphic g) {
children.remove(g);
return !children.contains(g);
}
public Graphic getChild(int index) {
return index < children.size() ? children.get(index) : null;
}
}
class Line extends Graphic {
public void draw(Log log) {log.append("Line");}
public boolean add(Graphic g) { return false; } // do nothing
public boolean remove(Graphic g) { return false; } // do nothing
public Graphic getChild(int index) {return null;}
}
class Rectangle extends Graphic {
public void draw(Log log) {log.append("Rect");}
public boolean add(Graphic g) { return false; } // do nothing
public boolean remove(Graphic g) { return false; } // do nothing
public Graphic getChild(int index) {return null;}
}
class Text extends Graphic {
public void draw(Log log) {log.append("Text");}
public boolean add(Graphic g) { return false; } // do nothing
public boolean remove(Graphic g) { return false; } // do nothing
public Graphic getChild(int index) {return null;}
}