-
Notifications
You must be signed in to change notification settings - Fork 1
/
Factory.java
31 lines (25 loc) · 857 Bytes
/
Factory.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
// Factory Class
package creational.factories;
// called PointClass to distinguish from Point class in package
class PointClass {
private double x, y;
// Simple constructor
private PointClass(double x, double y) { // making a constructor private forces user to use factory methods
this.x = x;
this.y = y;
}
public static class Factory {
public static PointClass newCartesianPoint(double x, double y) {
return new PointClass(x,y);
}
public static PointClass newPolarPoint(double rho, double theta) {
return new PointClass(rho*Math.cos(theta),
rho*Math.sin(theta));
}
}
}
class FactoryDemo {
public static void main(String[] args) {
PointClass point = PointClass.Factory.newPolarPoint(2, 3); // using a factory method
}
}