特别是在单元测试中,我们使用这种“设计模式”,我称之为“从类级别获取类”
框架测试.py:
class FrameWorkHttpClient(object):
....
class FrameWorkTestCase(unittest.TestCase):
# Subclass can control the class which gets used in get_response()
HttpClient=FrameWorkHttpClient
def get_response(self, url):
client=self.HttpClient()
return client.get(url)
我的测试.py:
class MyHttpClient(FrameWorkHttpClient):
....
class MyTestCase(FrameWorkTestCase):
HttpClient=MyHttpClient
def test_something(self):
response=self.get_response()
...
方法get_response()
从获取课程self
不是通过导入它。这样子类可以修改该类并使用不同的HttpClient
.
这个(从类级别获取类)“设计模式”的名称是什么?
这是一种“控制反转”还是“依赖注入”的方式?
你的代码非常类似于工厂方法模式。唯一的区别是您的变体使用工厂类变量而不是工厂方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)