-
Notifications
You must be signed in to change notification settings - Fork 0
/
specifications.py
108 lines (70 loc) · 2.89 KB
/
specifications.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from abc import ABC, ABCMeta, abstractmethod
from products import Product
class CanSatisfied(metaclass=ABCMeta):
__methods__ = ('isSatisfied', )
@classmethod
def __subclasshook__(cls, subclass):
for method in cls.__methods__:
if not (hasattr(subclass, method) and callable(getattr(subclass, method))):
return False
return True
@abstractmethod
def isSatisfied(self, product:Product):
raise NotImplementedError('[-] this method not implemented yet!')
class BaseSpecification(ABC, CanSatisfied):
def __init__(self, subject):
self._subject = subject
@property
def subject(self):
return self._subject
@abstractmethod
def isSatisfied(self, procuct:Product):
raise NotImplementedError("not implemented yet")
class BaseAggrigageSpec(ABC, CanSatisfied):
def __init__(self, first:CanSatisfied, second:CanSatisfied):
self._first = first
self._second = second
@abstractmethod
def isSatisfied(self, product:Product):
raise NotImplementedError("not implemented yet")
class ColorSpec(BaseSpecification):
def __init__(self, subject):
super().__init__(subject)
def isSatisfied(self, procuct: Product):
return self._subject == procuct.color
class PriceGreateThanSpec(BaseSpecification):
def __init__(self, subject):
super().__init__(subject)
def isSatisfied(self, procuct: Product):
return procuct.price > self._subject
class PriceGreateThanEqSpec(BaseSpecification):
def __init__(self, subject):
super().__init__(subject)
def isSatisfied(self, procuct: Product):
return procuct.price >= self._subject
class PriceLessThanSpec(BaseSpecification):
def __init__(self, subject):
super().__init__(subject)
def isSatisfied(self, procuct: Product):
return procuct.price < self._subject
class PriceLessThanEqSpec(BaseSpecification):
def __init__(self, subject):
super().__init__(subject)
def isSatisfied(self, procuct: Product):
return procuct.price <= self._subject
class AndSpec(BaseAggrigageSpec):
def __init__(self, first:BaseSpecification, second:BaseSpecification):
super().__init__(first, second)
self._second = second
def isSatisfied(self, procuct: Product):
return self._first.isSatisfied(procuct) and self._second.isSatisfied(procuct)
class OrSpec(BaseAggrigageSpec):
def __init__(self, first: CanSatisfied, second: CanSatisfied):
super().__init__(first, second)
def isSatisfied(self, product: Product):
return self._first.isSatisfied(product) or self._second.isSatisfied(product)
class NotSpec(CanSatisfied):
def __init__(self, spec:CanSatisfied):
self._spec = spec
def isSatisfied(self, product: Product):
return not self._spec.isSatisfied(product)