Skip to content

Commit fe7d3a1

Browse files
committed
Update pattern description
1 parent 9fe9746 commit fe7d3a1

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

patterns/dependency_injection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
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
1117
class 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

2229
import datetime

0 commit comments

Comments
 (0)