Created
October 1, 2016 15:23
-
-
Save justanr/05e292fd5734ef8f6eeaa800c2f28614 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABCMeta, abstractmethod | |
ABC = ABCMeta('ABC', (object,), {}) | |
class ConversionStrategy(ABC): | |
@abstractmethod | |
def convert(self, a): | |
raise NotImplementedError | |
class IntegerConversionStrategy(ConversionStrategy): | |
def __init__(self, converter): | |
self._conveter = converter | |
def convert(self, a): | |
return self._conveter(a) | |
class EnsureBooleanResult(ConversionStrategy): | |
def __init__(self, converter): | |
self.converter = converter | |
def convert(self, a): | |
if self.converter(a) == True: | |
return True | |
else: | |
return False | |
class Comparator(ABC): | |
@abstractmethod | |
def compare(self, x, y): | |
raise NotImplementedError | |
class ComparisonStrategy(Comparator): | |
pass | |
class GreaterThanIntegerComparisonStratgey(ComparisonStrategy): | |
def compare(self, x, y): | |
return x > y | |
class GreaterThanComparator(Comparator): | |
def __init__(self, conversion_strategy, comparison_strategy): | |
self.conversion_strategy = conversion_strategy | |
self.comparison_strategy = comparison_strategy | |
def compare(self, x, y): | |
x = self.convert_x(x) | |
y = self.convert_y(y) | |
return self.compare_x_y(x, y) | |
def convert_x(self, x): | |
return self.conversion_strategy.convert(x) | |
def convert_y(self, y): | |
return self.conversion_strategy.convert(y) | |
def compare_x_y(self, x, y): | |
return self.comparison_strategy.compare(x, y) | |
class EnsureBooleanResultComparator(Comparator): | |
def __init__(self, comparator_strategy, conversion_strategy): | |
self.comparator_strategy = comparator_strategy | |
self.conversion_strategy = conversion_strategy | |
def compare(self, x, y): | |
return self.conversion_strategy.convert(self.comparator_strategy.compare(x, y)) | |
class AbstractComparatorFactory(ABC): | |
@abstractmethod | |
def greater_than(self): | |
raise NotImplementedError | |
class IntegerComparatorFactory(AbstractComparatorFactory): | |
@staticmethod | |
def greater_than(): | |
return GreaterThanComparator(IntegerConversionStrategy(int), GreaterThanIntegerComparisonStratgey()) | |
class AbstractEnsureBooleanResultComparatorFactory(AbstractComparatorFactory): | |
pass | |
class EnsureBooleanResultIntegerComparatorFactory(AbstractEnsureBooleanResultComparatorFactory): | |
@staticmethod | |
def greater_than(): | |
return EnsureBooleanResultComparator(IntegerComparatorFactory.greater_than(), EnsureBooleanResult(bool)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment