监听模式中,建了模式父类
class Observer(ABCMate):
@abstractmethod
def update(self,waterHeater):
pass
洗澡模式:
class WashingMode(Observer):
def update(self,waterHeater):
if 50 < waterHeater.getTemperature() < 70:
print('水烧好了,可以洗澡啦!')
饮用模式:
class DrinkingMode(Observer):
def update(self,waterHeater):
if waterHeater.getTemperature() >= 100:
print('水烧开了,可以饮用了!')
实例化对象调用时:
waterHeater = WaterHeater()
washing = WashingMode()
drink = DrinkingMode()
waterHeater.addObserver(washing,drink)
waterHeater.setTemperature(60)
waterHeater.setTemperature(120)
报错:
Traceback (most recent call last):
File "E:/python设计模式/监听模式.py", line 52, in <module>
washing = WashingMode()
TypeError: __new__() missing 3 required positional arguments: 'name', 'bases', and 'namespace'
将waterHeater传入后还报错:
washing = WashingMode(waterHeater)
drink = DrinkingMode(waterHeater)
Traceback (most recent call last):
File "E:/python设计模式/监听模式.py", line 52, in <module>
washing = WashingMode(waterHeater)
TypeError: __new__() missing 2 required positional arguments: 'bases' and 'namespace'
所以去看ABCMate源码,发现ABC只是他的子类,用于更规范继承用的,而ABCMate类的__new__方法确实存在错误中的位置参数,百度无果,自己改为继承ABC类,将实例的waterHeater对象不传入washing和drink实例对象就好了,这里自己还没理解,所以记录下来,之后填坑。
###################################################
哈哈哈,蠢哭了,自己又研究了一下,在继承ABCMate类的正确写法是:metaclass=ABCMate。ABC可以直接继承,原来ABC的源码里的注释意思是ABC可以像继承类一样直接写类名,而ABCMate类就得加上mateclass,恍然大悟啊,着实尴尬,看来以后看源码得多思考了,不能太肤浅。