-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPenFactory.java
More file actions
62 lines (48 loc) · 1.32 KB
/
Copy pathPenFactory.java
File metadata and controls
62 lines (48 loc) · 1.32 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
package exp2;
public class PenFactory {
public static void main(String[] args) {
Factory penFactory = new Factory(PenType.Pen);
WritingTool pen = penFactory.getPen();
pen.write();
Factory ballPenFactory = new Factory(PenType.BallPen);
WritingTool ballPen = ballPenFactory.getPen();
ballPen.write();
Factory brushPenFactory = new Factory(PenType.BrushPen);
WritingTool brushPen = brushPenFactory.getPen();
brushPen.write();
}
}
class Factory {
private PenType type;
public Factory(PenType type) {
this.type = type;
}
public WritingTool getPen() {
return new WritingTool(type);
}
}
enum PenType {
Pen, BallPen, BrushPen
}
class WritingTool {
private PenType type;
public WritingTool(PenType type) {
this.type = type;
}
public void write() {
switch (type) {
case Pen:
System.out.println("[*] Writing with a regular pen.");
break;
case BallPen:
System.out.println("[*] Writing with a ball pen.");
break;
case BrushPen:
System.out.println("[*] Writing with a brush pen.");
break;
}
}
public PenType getType() {
return type;
}
}