removed proxy, bridge, template, and strategy patterns as they were too abstract to offer much use.

This commit is contained in:
tylerlaberge
2016-08-28 20:32:45 -04:00
parent 7501604bbf
commit 6ce017a391
8 changed files with 0 additions and 385 deletions

View File

@@ -1,18 +0,0 @@
from abc import ABCMeta, abstractmethod
class Strategy(object, metaclass=ABCMeta):
"""
An abstract Strategy class.
All strategies should inherit this class.
External Strategy Pattern documentation: U{https://en.wikipedia.org/wiki/Strategy_pattern}
"""
@abstractmethod
def __call__(self, *args, **kwargs):
"""
Abstract method that must be overridden.
The overridden method should execute the classes strategy.
"""
pass

View File

@@ -1,15 +0,0 @@
from abc import ABCMeta, abstractmethod
class Template(object, metaclass=ABCMeta):
"""
Abstract Template class as part of the Template design pattern.
External Template Design Pattern Documentation: U{https://en.wikipedia.org/wiki/Template_method_pattern}
"""
@abstractmethod
def go(self):
"""
Abstract method to call the concrete templates methods.
"""
pass

View File

@@ -1,17 +0,0 @@
from abc import ABCMeta
class Bridge(object, metaclass=ABCMeta):
"""
Base Bridge class as part of the Bridge design pattern.
External Bridge Pattern documentation: U{https://en.wikipedia.org/wiki/Bridge_pattern}
"""
def __init__(self, implementor):
"""
Initialize a new Bridge instance.
@param implementor: The implementor that concrete classes should call upon to do some action.
"""
self.implementor = implementor

View File

@@ -1,30 +0,0 @@
from abc import ABCMeta
class Proxy(object, metaclass=ABCMeta):
"""
Base Proxy class as part of the Proxy design pattern.
External Proxy Pattern documentation: U{https://en.wikipedia.org/wiki/Proxy_pattern}
"""
def __init__(self, subject):
"""
Initialize a new proxy instance.
@param subject: The real subject this proxy calls upon.
"""
self._subject = subject
self._validate()
def _validate(self):
"""
Validate that the subject and this proxy follow the same interface.
@raise AttributeError: If the subject and this proxy do not follow the same interface.
"""
for attr in dir(self._subject):
if attr.startswith('_') or attr.startswith('__'):
continue
elif not callable(getattr(self._subject, attr, None)) or not callable(getattr(self, attr, None)):
raise AttributeError('Subject and Proxy must follow same interface')

View File

@@ -1,66 +0,0 @@
from unittest import TestCase
from pypatterns.behavioral.strategy import Strategy
class StategyTestCase(TestCase):
"""
Unit testing class for the Strategy pattern.
"""
def setUp(self):
"""
Initialize testing data.
"""
class AddStrategy(Strategy):
def __call__(self, a, b):
return a + b
class SubtractStrategy(Strategy):
def __call__(self, a, b):
return a - b
class Solver(object):
def __init__(self, strategy):
self.strategy = strategy
def solve(self, a, b):
return self.strategy(a, b)
self.add_class = AddStrategy
self.subtract_class = SubtractStrategy
self.solver_class = Solver
def test_add_strategy(self):
"""
Test the add strategy.
@raise AssertionError: If the test fails.
"""
solver = self.solver_class(self.add_class())
self.assertEquals(15, solver.solve(5, 10))
def test_subtract_strategy(self):
"""
Test the subtract strategy.
@raise AssertionError: If the test fails.
"""
solver = self.solver_class(self.subtract_class())
self.assertEquals(10, solver.solve(100, 90))
def test_switch_strategies(self):
"""
Test changing out strategies.
@raise AssertionError: If the test fails.
"""
solver = self.solver_class(self.add_class())
self.assertEquals(5, solver.solve(2, 3))
solver.strategy = self.subtract_class()
self.assertEquals(-1, solver.solve(2, 3))

View File

@@ -1,81 +0,0 @@
from unittest import TestCase
from pypatterns.behavioral.template import Template
class TemplateTestCase(TestCase):
"""
Unit testing class for the Template class
"""
def setUp(self):
"""
Initialize testing data.
"""
class MakeMeal(Template):
def __init__(self):
self.prepared = False
self.cooked = False
self.eaten = False
self.drank = False
def go(self):
self.prepare()
self.cook()
self.eat()
self.drink()
def prepare(self):
pass
def cook(self):
pass
def eat(self):
pass
def drink(self):
pass
class MakePizza(MakeMeal):
def prepare(self):
self.prepared = True
def cook(self):
self.cooked = True
def eat(self):
self.eaten = True
class MakeTea(MakeMeal):
def prepare(self):
self.prepared = True
def cook(self):
self.cooked = True
def drink(self):
self.drank = True
self.make_pizza = MakePizza()
self.make_tea = MakeTea()
def test_go(self):
"""
Test the go method.
@raise AssertionError: If the test fails.
"""
self.make_pizza.go()
self.make_tea.go()
self.assertTrue(self.make_pizza.prepared)
self.assertTrue(self.make_pizza.cooked)
self.assertTrue(self.make_pizza.eaten)
self.assertFalse(self.make_pizza.drank)
self.assertTrue(self.make_tea.prepared)
self.assertTrue(self.make_tea.cooked)
self.assertFalse(self.make_tea.eaten)
self.assertTrue(self.make_tea.drank)

View File

@@ -1,93 +0,0 @@
from unittest import TestCase
from pypatterns.structural.bridge import Bridge
class BridgeTestCase(TestCase):
"""
Unit testing class for the Bridge class.
"""
def setUp(self):
"""
Initialize testing data.
"""
class DrawingAPI(object):
def __init__(self): pass
def draw_circle(self): pass
def draw_rectangle(self): pass
class DrawingAPI1(DrawingAPI):
def draw_circle(self):
return 'draw circle 1'
def draw_rectangle(self):
return 'draw rectangle 1'
class DrawingAPI2(DrawingAPI):
def draw_circle(self):
return 'draw circle 2'
def draw_rectangle(self):
return 'draw rectangle 2'
class Shape(Bridge):
def draw(self): pass
def do_something_high_level(self): pass
class CircleShape(Shape):
def draw(self):
return self.implementor.draw_circle()
def do_something_high_level(self):
return 'high level circle'
class RectangleShape(Shape):
def draw(self):
return self.implementor.draw_rectangle()
def do_something_high_level(self):
return 'high level rectangle'
self.drawing_api_one = DrawingAPI1()
self.drawing_api_two = DrawingAPI2()
self.circle_shape_one = CircleShape(self.drawing_api_one)
self.circle_shape_two = CircleShape(self.drawing_api_two)
self.rectangle_shape_one = RectangleShape(self.drawing_api_one)
self.rectangle_shape_two = RectangleShape(self.drawing_api_two)
def test_init(self):
"""
Test the __init__ method.
@raise AssertionError: If the test fails.
"""
self.assertEquals(self.drawing_api_one, self.circle_shape_one.implementor)
self.assertEquals(self.drawing_api_two, self.circle_shape_two.implementor)
self.assertEquals(self.drawing_api_one, self.rectangle_shape_one.implementor)
self.assertEquals(self.drawing_api_two, self.rectangle_shape_two.implementor)
def test_implementation(self):
"""
Test an implementation of the bridge pattern
@raise AssertionError: If the test fails.
"""
self.assertEquals('draw circle 1', self.circle_shape_one.draw())
self.assertEquals('draw circle 2', self.circle_shape_two.draw())
self.assertEquals('draw rectangle 1', self.rectangle_shape_one.draw())
self.assertEquals('draw rectangle 2', self.rectangle_shape_two.draw())
self.assertEquals('high level circle', self.circle_shape_one.do_something_high_level())
self.assertEquals('high level circle', self.circle_shape_two.do_something_high_level())
self.assertEquals('high level rectangle', self.rectangle_shape_one.do_something_high_level())
self.assertEquals('high level rectangle', self.rectangle_shape_two.do_something_high_level())

View File

@@ -1,65 +0,0 @@
from unittest import TestCase
from pypatterns.structural.proxy import Proxy
class ProxyTestCase(TestCase):
"""
Unit testing class for the Proxy class.
"""
def setUp(self):
"""
Initialize testing data.
"""
class Car(object):
def drive_car(self):
return 'drive car'
class Driver(object):
def __init__(self, age):
self.age = age
self.car = Car()
self.driver = Driver(17)
def test_valid_proxy(self):
"""
Test a Proxy class following the same interface as the subject.
@raise AssertionError: If the test fails.
"""
class ProxyCar(Proxy):
def __init__(self, subject, driver):
super().__init__(subject)
self.driver = driver
def drive_car(self):
if self.driver.age > 16:
return self._subject.drive_car()
else:
return 'Driver is too young to drive'
try:
proxy = ProxyCar(self.car, self.driver)
except AttributeError:
raise AssertionError()
else:
self.assertEqual('drive car', proxy.drive_car())
proxy.driver.age = 15
self.assertEqual('Driver is too young to drive', proxy.drive_car())
def test_invalid_proxy(self):
"""
Test a Proxy class that is not following the same interface as the subject.
@raise AssertionError: If the test fails.
"""
class ProxyCar(Proxy):
def __init__(self, subject, driver):
super().__init__(subject)
self.driver = driver
self.assertRaises(AttributeError, lambda: ProxyCar(self.car, self.driver))