Created
March 27, 2015 22:18
-
-
Save vortec/18ff373155c6141515bb 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
class Dependency: | |
def get_cheese_from_singleton(self): | |
from foobar_controller import FoobarController | |
liked = FoobarController.foo_the_bar() | |
return 'I like {}'.format(liked) | |
def get_cheese_from_di(self, foobar_controller): | |
liked = foobar_controller.foo_the_bar() | |
return 'I like {}'.format(liked) |
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
class FoobarController: | |
foobar = None | |
@classmethod | |
def foo_the_bar(cls): | |
return cls.foobar |
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
if __name__ == '__main__': | |
import sys | |
from foobar_controller import FoobarController | |
from dependency import Dependency | |
# Get values for service class from somewhere and apply them | |
FoobarController.foobar = sys.argv[1] | |
dep = Dependency() | |
# Let the dependency figure out how to get the service class. | |
# For testing you could mock it. | |
print(dep.get_cheese_from_singleton()) | |
# DI | |
print(dep.get_cheese_from_di(FoobarController)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment