Created
August 23, 2017 14:37
-
-
Save hbradio/3e99744fec61c1cbde2902f429d9ecbd to your computer and use it in GitHub Desktop.
This file contains 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
import lib | |
# A not-great example | |
class MyLessGoodClass: | |
def do_something(self): | |
x = lib.get_thing() | |
return x + 1 | |
def do_another_thing(self): | |
x = lib.get_thing() | |
return x + 2 | |
a_less_good_instance = MyLessGoodClass() | |
a_less_good_instance.do_something() | |
_less_good_instance.do_another_thing() | |
# An example showing better dependency injection | |
class MyBetterClass: | |
__init__(self, thing_provider): | |
self.thing_provider = thing_provider | |
def do_something(self): | |
x = self.thing_provider.get_thing() | |
return x + 1 | |
def do_another_thing(self): | |
x = self.thing_provider.get_thing() | |
return x + 2 | |
a_better_instance = MyBetterClass(thing_provider=lib) | |
a_better_instance.do_something() | |
a_better_instance.do_another_thing() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment