File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed
Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change 22# -*- coding : utf-8 -*-
33
44"""
5- Port of the Java example of "Constructor Injection" in
5+ Dependency Injection (DI) is a technique whereby one object supplies the dependencies (services)
6+ to another object (client).
7+ It allows to decouple objects: no need to change client code simply because an object it depends on
8+ needs to be changed to a different one. (Open/Closed principle)
9+
10+ Port of the Java example of Dependency Injection" in
611"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
712(ISBN-10: 0131495054, ISBN-13: 978-0131495050)
813
9- production code which is untestable:
14+ In the following example `time_provider` (service) is embedded into TimeDisplay (client).
15+ If such service performed an expensive operation you would like to substitute or mock it in tests.
1016
1117class TimeDisplay(object):
1218
1319 def __init__(self):
14- self.time_provider = datetime.datetime
20+ self.time_provider = datetime.datetime.now
1521
1622 def get_current_time_as_html_fragment(self):
17- current_time = self.time_provider.now ()
23+ current_time = self.time_provider()
1824 current_time_as_html_fragment = "<span class=\" tinyBoldText\" >{}</span>".format(current_time)
1925 return current_time_as_html_fragment
26+
2027"""
2128
2229import datetime
You can’t perform that action at this time.
0 commit comments