Skip to content

Instantly share code, notes, and snippets.

@patforna
Last active May 2, 2016 10:40
Show Gist options
  • Save patforna/19a3cfd04e543d23c9a7d1889a034135 to your computer and use it in GitHub Desktop.
Save patforna/19a3cfd04e543d23c9a7d1889a034135 to your computer and use it in GitHub Desktop.
#
# starting point
#
class DeployCLI
def run
template, args = ...
TemplateRenderer.new.render(template, args)
end
end
#
# intermediate step 1 - create dependencies in constructor
#
# I normally do this as an intermediate step just to make the dependencies more
# explicit, which I find useful if there are many of them.
#
class DeployCLI
def initialize
@renderer = TemplateRenderer.new
end
def run
template, args = ...
@renderer.render(template, args)
end
end
#
# intermediate step 2 - create default dependency but also allow injection
#
# This is a further intermediate step I sometimes do when I have a lot of code
# that depends on the API I'm about to change. Benefit: I can change the
# dependent code one by one without breaking anything. Drawback: i) can't be
# done in every language. ii) the coupling to the actual implementation still
# exists.
#
class DeployCLI
def initialize(renderer = TemplateRenderer.new)
@renderer = renderer
end
...
end
#
# final step. inject dependency and break coupling
#
class DeployCLI
def initialize(renderer)
@renderer = renderer
end
def run
template, args = ...
@renderer.render(template, args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment