-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.py
64 lines (44 loc) · 1.86 KB
/
factory.py
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
"""Factory pattern example.
Use a factory class when you have too many factory methods. The class can be separate
(`PointFactory`) or an 'inner' class (`Factory`). The latter eases discovery of its
existence.
"""
from math import cos, isclose, sin
class Point:
"""A point on a plane."""
def __init__(self, x: float, y: float):
"""Create a point using Cartesian coordinates."""
self.x = x
self.y = y
def __eq__(self, other: object) -> bool:
"""Two `Point` objects are equal if their coordinates are equal."""
if not isinstance(other, Point):
return NotImplemented
return isclose(self.x, other.x) and isclose(self.y, other.y)
class Factory:
"""An 'inner' class as factory of `Point` objects.
This is an alternative to having a factory separate from this (`Point`) class.
"""
def new_cartesian_point(self, x: float, y: float) -> "Point":
"""Create a point using Cartesian coordinates."""
return Point(x, y)
def new_polar_point(self, rho: float, theta: float) -> "Point":
"""Create a point using polar coordinates.
`theta` is in radians.
"""
return Point(rho * cos(theta), rho * sin(theta))
# A convenience member that can be used to get a factory.
factory = Factory()
class PointFactory:
"""A factory of `Point` objects."""
def new_cartesian_point(self, x: float, y: float) -> "Point":
"""Create a point using Cartesian coordinates.
Does not to be a static method.
"""
return Point(x, y)
def new_polar_point(self, rho: float, theta: float) -> "Point":
"""Create a point using polar coordinates.
`theta` is in radians.
Does not need to be a static method.
"""
return Point(rho * cos(theta), rho * sin(theta))