-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactory.java
More file actions
43 lines (39 loc) · 750 Bytes
/
Factory.java
File metadata and controls
43 lines (39 loc) · 750 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
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Circle");
}
}
class Rectangle implements Shape {
public void draw() {
System.out.println("Rectangle");
}
}
class ShapeFactory {
public Shape createShape(String shape) {
if (shape == null) {
return null;
}
if (shape.equals("Circle")) {
return new Circle();
}
else if (shape.equals("Rectangle")) {
return new Rectangle();
}
else {
System.err.println("Invalid Input");
}
return null;
}
}
public class Test {
public static void main(String[] args) {
ShapeFactory f1 = new ShapeFactory();
Shape s1 = f1.createShape("Circle");
Shape s2 = f1.createShape("Rectangle");
s1.draw();
s2.draw();
}
}