"""
解释器模式
定义一个解释器,使用给定的文本表示来解释一段语句
"""
from abc import ABCMeta, abstractmethod
class Interpreter(object):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
def interpre(self, context):
pass
class ConInterpreter(Interpreter):
def __init__(self):
self.text = "abc"
super(ConInterpreter, self).__init__()
def interpre(self, context):
my_word = []
for word in context.word:
my_word.append(word + self.text)
return my_word
class Context(object):
def __init__(self):
self._word = []
pass
@property
def word(self):
return self._word
@word.setter
def word(self, word):
self._word = word
def append(self, text):
self._word.append(text)
if __name__ == '__main__':
context = Context()
context.append("abc")
context.append("def")
context.append("sds")
inter = ConInterpreter()
print(inter.interpre(context))
解释器模式(python)
最新推荐文章于 2024-02-23 07:16:58 发布