相关疑难解决方法(0)

3360
推荐指数
24
解决办法
73万
查看次数

设计模式的名称:从类级别获取类

特别是在单元测试中,我们使用这种"设计模式",我称之为"从班级获取课程"

framworktest.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)
Run Code Online (Sandbox Code Playgroud)

mytest.py:

class MyHttpClient(FrameWorkHttpClient):
    ....

class MyTestCase(FrameWorkTestCase):
    HttpClient=MyHttpClient

    def test_something(self):
        response=self.get_response()
        ...
Run Code Online (Sandbox Code Playgroud)

该方法不是通过导入来get_response()获取类self.这样子类可以修改类并使用不同的类HttpClient.

这个名称是什么(从班级获得类)"设计模式"?

这是"控制反转"还是"依赖注入"?

python inheritance design-patterns dependency-injection inversion-of-control

7
推荐指数
2
解决办法
566
查看次数