-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrototype.java
More file actions
56 lines (48 loc) · 949 Bytes
/
Prototype.java
File metadata and controls
56 lines (48 loc) · 949 Bytes
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
abstract class Shape implements Cloneable {
private String id;
protected String type;
abstract void draw();
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public Object clone() {
Object clone = null;
try {
clone = super.clone();
}
catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
class Circle extends Shape {
public Circle {
super();
this.type = "Circle";
}
public void draw() {
System.out.println("Circle");
}
}
class ShapeCache {
public static Map<Integer, Shape> map = new HashMap<Integer, Shape>();
public static Shape getShape(Integer id) {
return (Shape)map.get(id).clone();
}
public static void loadCache() {
Shape s1 = new Circle();
s1.setId("1");
map.put(1, s1);
}
}
public class Test {
public static void main(String[] args) {
ShapeCache.loadCache();
Shape s1 = ShapeCache.getShape(1);
s1.draw();
}
}