-
Notifications
You must be signed in to change notification settings - Fork 1
/
FactoryMethod.java
62 lines (53 loc) · 1.51 KB
/
FactoryMethod.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
// Factory Method
package creational.factories;
enum CoordinateSystem {
CARTESIAN,
POLAR
}
class Point {
private double x, y;
// Simple constructor
private Point(double x, double y) { // making a constructor private forces user to use factory methods
this.x = x;
this.y = y;
}
// // Illegal constructor - already have a constructor with two double values
// public Point (double rho, double theta) {
// x = rho * Math.cos(theta);
// y = rho * Math.sin(theta);
// }
/* Alternative constructor, using the enum above, to manage various cases
Constructor is ugly. You need documentation, as below.
*/
/**
*
* @param a is x if cartesian or rho if polar.
* @param b
* @param cs
*/
private Point(double a, double b, CoordinateSystem cs) {
switch (cs){
case CARTESIAN:
this.x = x;
this.y = y;
break;
case POLAR:
x = a * Math.cos(b);
y = a * Math.sin(b);
break;
}
}
// Factory methods
public static Point newCartesianPoint(double x, double y) {
return new Point(x,y);
}
public static Point newPolarPoint(double rho, double theta) {
return new Point(rho*Math.cos(theta),
rho*Math.sin(theta));
}
}
class FactoryMethodDemo {
public static void main(String[] args) {
Point point = Point.newPolarPoint(2, 3); // using a factory method
}
}