| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9fe9746 commit fe7d3a1
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,21 +2,28 @@ | |||
| 2 | 2 | # -*- coding : utf-8 -*- | |
| 3 | 3 | ||
| 4 | 4 | """ | |
| 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 | ||
| 6 | 11 | "xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros | |
| 7 | 12 | (ISBN-10: 0131495054, ISBN-13: 978-0131495050) | |
| 8 | 13 | ||
| 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. | ||
| 10 | 16 | ||
| 11 | 17 | class TimeDisplay(object): | |
| 12 | 18 | ||
| 13 | 19 | def __init__(self): | |
| 14 | - self.time_provider = datetime.datetime | ||
| 20 | + self.time_provider = datetime.datetime.now | ||
| 15 | 21 | ||
| 16 | 22 | def get_current_time_as_html_fragment(self): | |
| 17 | - current_time = self.time_provider.now() | ||
| 23 | + current_time = self.time_provider() | ||
| 18 | 24 | current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time) | |
| 19 | 25 | return current_time_as_html_fragment | |
| 26 | + | ||
| 20 | 27 | """ | |
| 21 | 28 | ||
| 22 | 29 | import datetime | |
| Back | FazBrowse Home | New Git URL |
0 commit comments