-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolyhedronFactory.java
112 lines (98 loc) · 2.65 KB
/
PolyhedronFactory.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
import java.lang.StringBuilder;
public class PolyhedronFactory {
/**
* Name Polyhedron Pair 2-tuple( name, model )
*/
private static class PolyhedronPair {
public String name; ///< Name of the Polyhedron to clone
public Polyhedron model; ///< Model of the Polyhedron to clone
/**
* Default Constructor - Used as sentinel
*/
public PolyhedronPair()
{
this.name = new String();
this.model = null;
}
/**
* Non-Default Constructor
*
* @param name the name of a Polyhedron
* @param Polyhedron a cloneable Polyhedron
*/
public PolyhedronPair(String name, Polyhedron p)
{
this.name = name;
this.model = p;
}
/**
* Print the PolyhedronPair
*/
public String toString()
{
return " " + this.name + "\n";
}
}
/**
* Listing of known Polyhedra
*/
private static PolyhedronPair[] knownPolyhedra = new PolyhedronPair[] {
new PolyhedronPair("sphere" , new Sphere() ),
new PolyhedronPair("cylinder" , new Cylinder() ),
new PolyhedronPair("composite" , new Composite())
};
/**
* Create a Polyhedron
*
* @param name the Polyhedron to be created
*
* @return A Polyhedron with the specified name
* or null if no matching Polyhedron is found
*/
public static Polyhedron createPolyhedron( String name )
{
for( PolyhedronPair pair : knownPolyhedra ){
if( pair.name.equals(name) ){
return pair.model.clone();
}
}
return null;
}
/**
* Determine whether a given Polyhedron is known
*
* @param name the Polyhedron for which to query
*/
public static boolean isKnown( String name )
{
for( PolyhedronPair pair : knownPolyhedra ){
if( pair.name.equals(name) ){
return true;
}
}
return false;
}
/**
* Print a list of known Polyhedrons
*/
public static String listKnown()
{
StringBuilder bld = new StringBuilder();
for( PolyhedronPair pair : knownPolyhedra ){
bld.append(pair);
}
return bld.toString();
}
/**
* Determine the number of known Polyhedrons
*
* @return the number of known Polyhedrons
*/
public static int numberKnown()
{
return knownPolyhedra.length;
}
}