Python版

https://github.com/faif/python-patterns/blob/master/dft/setter_injection.py

#!/usr/bin/python
# -*- coding : utf-8 -*-
import datetime """
Port of the Java example of "Setter Injection" in
"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
(ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on
http://xunitpatterns.com/Dependency%20Injection.html. production code which is untestable: class TimeDisplay(object): def __init__(self):
self.time_provider = datetime.datetime def get_current_time_as_html_fragment(self):
current_time = self.time_provider.now()
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
return current_time_as_html_fragment
""" class TimeDisplay(object): def __init__(self):
pass def set_time_provider(self, time_provider):
self.time_provider = time_provider def get_current_time_as_html_fragment(self):
current_time = self.time_provider.now()
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
return current_time_as_html_fragment class ProductionCodeTimeProvider(object):
"""
Production code version of the time provider (just a wrapper for formatting
datetime for this example).
""" def now(self):
current_time = datetime.datetime.now()
current_time_formatted = "{}:{}".format(current_time.hour,
current_time.minute)
return current_time_formatted class MidnightTimeProvider(object):
"""
Class implemented as hard-coded stub (in contrast to configurable stub).
""" def now(self):
current_time_is_always_midnight = "24:01"
return current_time_is_always_midnight

Python转载版

最新文章

  1. syntax error near unexpected token‘(
  2. 使用idea debug多线程
  3. Ajax基本概念和原理
  4. 2015 年开源前端框架盘点 TOP 20
  5. loadrunner 发送gzip压缩json格式(转)
  6. 对AppStore中的项目进行评分(转载)
  7. 离散型特征编码方式:one-hot与哑变量
  8. Activity、Window、View三者之间的联系
  9. myeclipse连接mysql生成数据表时中文字符乱码或问号(解决方法)
  10. 【BZOJ1426】收集邮票 期望DP
  11. python---使用字典来实现链接表图
  12. [转] 用webpack的CommonsChunkPlugin提取公共代码的3种方式
  13. Codeforces 382 D Permutations
  14. [微信小程序] 微信小程序下拉滚动选择器picker绑定数据的两种方式
  15. flask_admin 笔记三 客户化视图
  16. win10 添加项目右键用vscode打开
  17. Erlang TCP Socket的接收进程的2种方案
  18. 如何获取用户的地理位置? &amp;&amp; html5 地理位置
  19. pycharm/webstorm创建react项目
  20. C++父子类继承时的隐藏、覆盖、重载

热门文章

  1. 『学了就忘』Linux基础命令 — 28、别名和常用快捷键
  2. ansible模块及语法
  3. Invalid prop: type check failed for prop &quot;xxx&quot;. Expected Number, got String.
  4. Intellij IDEA 配置Junit
  5. FZU ICPC 2020 寒假训练 3
  6. Redis的ACID属性
  7. CODING 项目协同 2.0 —— 让协作有条不紊
  8. 如何找到mysql磁盘地址
  9. jmeter ssh command方式执行hive指令
  10. [NOIP2017 提高组] 列队