Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of Design Pattern: get class from class level

Especially in unittests we use this "design pattern" I call "get class from class level"

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)

mytest.py:

class MyHttpClient(FrameWorkHttpClient):
    ....

class MyTestCase(FrameWorkTestCase):
    HttpClient=MyHttpClient

    def test_something(self):
        response=self.get_response()
        ...

The method get_response() gets the class from self not by importing it. This way a subclass can modify the class and use a different HttpClient.

What's the name of this (get class from class level) "design pattern"?

Is this a way of "inversion of control" or "dependency injection"?

like image 887
guettli Avatar asked Dec 19 '14 18:12

guettli


2 Answers

Your code is very similar to Factory method pattern. The only difference is that your variant uses factory class variable instead of factory method.

like image 71
renskiy Avatar answered Oct 15 '22 03:10

renskiy


I believe this has the same purpose as just simple polymorphism implemented using Python-specific syntax. Instead of having a virtual method returning a new instances, you have the instance type stored as "an overridable variable" in a class/subclass.

This can be rewritten as a virtual method (sorry I am not fluent in Python so this is just pseudocode)

virtual HttpClient GetClient()
  return new FrameworkHttpClient()

then in the subclass, you change the implementation of the method to return a different type:

override HttpClient GetClient()
  return new MyHttpClient()

If you want to call this a pattern, I would say it is similar to Strategy GoF pattern. In your particular case, the algorithm being abstracted away is the creation of the particular HttpClient implementation.

And after second thought - as you stated, indeed this can be looked at as an IoC example.

like image 34
Marek Avatar answered Oct 15 '22 02:10

Marek