|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +@author: Gordeev Andrey <[email protected]> |
| 6 | +
|
| 7 | +Specification provide recombination business logic by |
| 8 | +chaining together using boolean logic |
| 9 | +""" |
| 10 | + |
| 11 | +fromabcimportabstractmethod |
| 12 | + |
| 13 | + |
| 14 | +classSpecification(object): |
| 15 | + |
| 16 | +defand_specification(self, candidate): |
| 17 | +raiseNotImplementedError() |
| 18 | + |
| 19 | +defor_specification(self, candidate): |
| 20 | +raiseNotImplementedError() |
| 21 | + |
| 22 | +defnot_specification(self): |
| 23 | +raiseNotImplementedError() |
| 24 | + |
| 25 | +@abstractmethod |
| 26 | +defis_satisfied_by(self, candidate): |
| 27 | +pass |
| 28 | + |
| 29 | + |
| 30 | +classCompositeSpecification(Specification): |
| 31 | +@abstractmethod |
| 32 | +defis_satisfied_by(self, candidate): |
| 33 | +pass |
| 34 | + |
| 35 | +defand_specification(self, candidate): |
| 36 | +returnAndSpecification(self, candidate) |
| 37 | + |
| 38 | +defor_specification(self, candidate): |
| 39 | +returnOrSpecification(self, candidate) |
| 40 | + |
| 41 | +defnot_specification(self): |
| 42 | +returnNotSpecification(self) |
| 43 | + |
| 44 | + |
| 45 | +classAndSpecification(CompositeSpecification): |
| 46 | +_one=Specification() |
| 47 | +_other=Specification() |
| 48 | + |
| 49 | +def__init__(self, one, other): |
| 50 | +self._one=one |
| 51 | +self._other=other |
| 52 | + |
| 53 | +defis_satisfied_by(self, candidate): |
| 54 | +returnbool(self._one.is_satisfied_by(candidate) and |
| 55 | +self._other.is_satisfied_by(candidate)) |
| 56 | + |
| 57 | + |
| 58 | +classOrSpecification(CompositeSpecification): |
| 59 | +_one=Specification() |
| 60 | +_other=Specification() |
| 61 | + |
| 62 | +def__init__(self, one, other): |
| 63 | +self._one=one |
| 64 | +self._other=other |
| 65 | + |
| 66 | +defis_satisfied_by(self, candidate): |
| 67 | +returnbool(self._one.is_satisfied_by(candidate) or |
| 68 | +self._other.is_satisfied_by(candidate)) |
| 69 | + |
| 70 | + |
| 71 | +classNotSpecification(CompositeSpecification): |
| 72 | +_wrapped=Specification() |
| 73 | + |
| 74 | +def__init__(self, wrapped): |
| 75 | +self._wrapped=wrapped |
| 76 | + |
| 77 | +defis_satisfied_by(self, candidate): |
| 78 | +returnbool(notself._wrapped.is_satisfied_by(candidate)) |
| 79 | + |
| 80 | + |
| 81 | +classUser(object): |
| 82 | + |
| 83 | +def__init__(self, super_user=False): |
| 84 | +self.super_user=super_user |
| 85 | + |
| 86 | + |
| 87 | +classUserSpecification(CompositeSpecification): |
| 88 | + |
| 89 | +defis_satisfied_by(self, candidate): |
| 90 | +returnisinstance(candidate, User) |
| 91 | + |
| 92 | + |
| 93 | +classSuperUserSpecification(CompositeSpecification): |
| 94 | + |
| 95 | +defis_satisfied_by(self, candidate): |
| 96 | +returngetattr(candidate, 'super_user', False) |
| 97 | + |
| 98 | + |
| 99 | +if__name__=='__main__': |
| 100 | +print('Specification') |
| 101 | +andrey=User() |
| 102 | +ivan=User(super_user=True) |
| 103 | +vasiliy='not User instance' |
| 104 | + |
| 105 | +root_specification=UserSpecification().\ |
| 106 | +and_specification(SuperUserSpecification()) |
| 107 | + |
| 108 | +print(root_specification.is_satisfied_by(andrey)) |
| 109 | +print(root_specification.is_satisfied_by(ivan)) |
| 110 | +print(root_specification.is_satisfied_by(vasiliy)) |
| 111 | + |
| 112 | + |
| 113 | +### OUTPUT ### |
| 114 | +# Specification |
| 115 | +# False |
| 116 | +# True |
| 117 | +# False |
0 commit comments