0
点赞
收藏
分享

微信扫一扫

4.16_template_method_行为型模式:模板方法模式

from abc import ABCMeta, abstractmethod
from time import sleep


class Window(metaclass=ABCMeta):
"""定义抽象的原子操作 / 钩子操作,需要在子类实现"""

@abstractmethod
def start(self):
pass

@abstractmethod
def repaint(self):
pass

@abstractmethod
def stop(self):
pass

def run(self):
"""模板方法 (运行逻辑不用重写)"""

self.start()

while True:
try:
self.repaint()
sleep(1)
except KeyboardInterrupt:
break

self.stop()


class MyWindow(Window):
def __init__(self, msg):
self.msg = msg

def start(self):
print('窗口开始运行')

def stop(self):
print('窗口结束运行')

def repaint(self):
print(self.msg)


MyWindow('Hello...').run()

 

举报

相关推荐

0 条评论