from government.powerSupply import power
Case 1: Should service object be initial zed in constructor of the class and same amont of energy be available to house at all times
案例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
or
Case 2 it should be done like this that function being called outside should have option to pass in services so different amount of power can be given to room whose light being turned on.
情况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
today, I was working on updating Unittest at office and found it convenient to do the Case 2. Since in my case service can contain different amount of power voltage as well as more than one service i.e a bath room may have power service or water supply service !!
今天,我正在努力更新Unittest在办公室,并发现方便2做方便。因为在我的情况下,服务可以包含不同的电源电压以及多个服务,即浴室可能有电力服务或供水服务!!
1 个解决方案
#1
0
Consider the following arguments:
请考虑以下参数:
- If you are absolutely sure
services
will never change after theHome
home object has been initialized, then you should choose Case 1. - If you are not sure that
services
is immutable, or even if you are sure it may be changed, than definitely go for Case 2.
如果您绝对确定在Home home对象初始化后服务永远不会改变,那么您应该选择Case 1。
如果您不确定服务是不可变的,或者即使您确定它可能会被更改,那么肯定会去案例2。
The point here is that the choice is based upon how you are going to handle different variables in your business rules. If on variable is never changing than there is no reason to allow so. However if is is interesting that this element may be changed, than this possibility should be given by the Home
user. A default value as you provided in your code is an excellent approach because most cases may not need to change this variable at all.
这里的要点是选择是基于如何处理业务规则中的不同变量。如果on变量永远不会改变,那么就没有理由允许。但是,如果有趣的是这个元素可能会被更改,那么Home用户应该给出这种可能性。您在代码中提供的默认值是一种很好的方法,因为大多数情况下可能根本不需要更改此变量。
#1
0
Consider the following arguments:
请考虑以下参数:
- If you are absolutely sure
services
will never change after theHome
home object has been initialized, then you should choose Case 1. - If you are not sure that
services
is immutable, or even if you are sure it may be changed, than definitely go for Case 2.
如果您绝对确定在Home home对象初始化后服务永远不会改变,那么您应该选择Case 1。
如果您不确定服务是不可变的,或者即使您确定它可能会被更改,那么肯定会去案例2。
The point here is that the choice is based upon how you are going to handle different variables in your business rules. If on variable is never changing than there is no reason to allow so. However if is is interesting that this element may be changed, than this possibility should be given by the Home
user. A default value as you provided in your code is an excellent approach because most cases may not need to change this variable at all.
这里的要点是选择是基于如何处理业务规则中的不同变量。如果on变量永远不会改变,那么就没有理由允许。但是,如果有趣的是这个元素可能会被更改,那么Home用户应该给出这种可能性。您在代码中提供的默认值是一种很好的方法,因为大多数情况下可能根本不需要更改此变量。