Skip to content

Instantly share code, notes, and snippets.

@Ceasar Ceasar/deps.py
Created Aug 28, 2014

Embed
What would you like to do?
Different ways to resolve dependencies
# Manual constructor injection
class Charlie:
pass
class Beta:
def __init__(self, charlie):
self.charlie = charlie
class Alpha:
def __init__(self, beta):
self.beta = beta
a = Alpha(Beta(Charlie()))
# Manual setter injection
a = A()
a.x = 1
# Factory
class Charlie:
pass
class Beta:
def __init__(self, charlie):
self.charlie = charlie
class Alpha:
def __init__(self, beta):
self.beta = beta
class BetaFactory:
def make(self):
return Beta(Charlie())
class AlphaFactory:
def make(self):
return Alpha(BetaFactory().make())
a = AlphaFactory().make()
# ???
class Charlie:
pass
class Beta:
def __init__(self):
self.charlie = Charlie()
class Alpha:
def __init__(self):
self.beta = Beta()
a = Alpha()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.