-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormeFactory.java
65 lines (56 loc) · 2.55 KB
/
FormeFactory.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
import ca.etsmtl.log.util.IDLogger;
import java.util.StringTokenizer;
public class FormeFactory {
private static final String OVALE = "OVALE";
private static final String CERCLE = "CERCLE";
private static final String RECTANGLE = "RECTANGLE";
private static final String CARRE = "CARRE";
private static final String LIGNE = "LIGNE";
public static FormeFactory singleton = null;
private FormeFactory() {
}
public static FormeFactory getFactory() {
if (singleton == null) {
return new FormeFactory();
}
return singleton;
}
public Forme makeForme(String description) {
IDLogger idLogger = IDLogger.getInstance();
StringTokenizer tokenizer = new StringTokenizer(description, "<>/ ", false);
int cnt = 0;
while (tokenizer.hasMoreTokens()) {
if (cnt == 0) {
idLogger.logID(Integer.parseInt(tokenizer.nextToken()));
} else if (cnt == 1) {
return createForme(tokenizer);
}
cnt++;
}
return null;
}
public Forme createForme(StringTokenizer token) {
String type = token.nextToken();
if (type.equals(CARRE)) {
return new Carre(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken
()), Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
} else if (type.equals(CERCLE)) {
return new Cercle(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
} else if (type.equals(OVALE)) {
return new Ovale(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken
()), Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
} else if (type.equals(RECTANGLE)) {
return new Rectangle(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken
()), Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
} else if (type.equals(LIGNE)) {
return new Ligne(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken
()), Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
}
return null;
}
public static void main(String[] args) {
FormeFactory f = FormeFactory.getFactory();
String forme = "12345<CERCLE>2 3 4</CERCLE>";
System.out.println(f.makeForme(forme) instanceof Cercle);
}
}