【发布时间】:2015-02-25 15:51:39
【问题描述】:
from government.powerSupply import power
案例 1:服务对象是否应该在类的构造函数中初始化,并且始终有相同数量的能量可供容纳
class Home(object):
def __init__(self, rooms=4, people=2, services=None):
self._services = services or power.Service()
self.connectionType = 2 # MeghaWatts
self.roomLights = {1:False, 2:False: 3: False, 4: False}
def turnLightsOn(self, room=None, all=False):
voltage = self._services.voltage()
if all:
for light, _ in self.roomLights.iteritems():
self.roomLights[light] = True
return True
else:
if room:
self.roomLights[room]
return True
或
案例 2 应该这样做,在外部调用的函数应该可以选择传入服务,以便可以为打开灯的房间提供不同的电量。
class Home(object):
def __init__(self, rooms=4, people=2, services=None):
self._services = services or power.Service()
self.connectionType = 2 # MeghaWatts
self.roomLights = {1:False, 2:False: 3: False, 4: False}
def turnLightsOn(self, room=None, all=False, services=None):
# in this case service can be passed from outside when call to
# turnLights on is made, and service can have different voltage input
self._services = services or self._services
voltage = self._services.voltage()
if all:
for light, _ in self.roomLights.iteritems():
self.roomLights[light] = True
return True
else:
if room:
self.roomLights[room]
return True
今天,我正在办公室更新 Unittest,发现做 案例 2 很方便。因为在我的情况下,服务可以包含不同数量的电源电压以及不止一项服务,即浴室可能有电力服务或供水服务!
【问题讨论】:
-
从建模的角度来看,
House有一个powerSupply,在所有房间之间共享,具有一些固定(-ish)电压和最大功率。 MW 应该是电源的属性,而不是房屋(尽管房屋布线也可能有一些安全最大值)。 -
井房最大值是我从接线类型考虑的
标签: python unit-testing oop object-oriented-analysis