Python设计模式编程中解释器模式的简单程序示例分享

作者:ponder008 时间:2023-01-16 08:44:29 

模式特点:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。

我们来看一下下面这样的程序结构:


class Context:
 def __init__(self):
   self.input=""
   self.output=""

class AbstractExpression:
 def Interpret(self,context):
   pass

class Expression(AbstractExpression):
 def Interpret(self,context):
   print "terminal interpret"

class NonterminalExpression(AbstractExpression):
 def Interpret(self,context):
   print "Nonterminal interpret"

if __name__ == "__main__":
 context= ""
 c = []
 c = c + [Expression()]
 c = c + [NonterminalExpression()]
 c = c + [Expression()]
 c = c + [Expression()]
 for a in c:
   a.Interpret(context)

那么它所体现出的类图是这样的:

Python设计模式编程中解释器模式的简单程序示例分享

再来看一个例子:


#encoding=utf-8
#
#by panda
#解释器模式

def printInfo(info):
 print unicode(info, 'utf-8').encode('gbk'),

#上下文类:演奏内容
class PlayContext():
 text = None
 PlayText = None

#抽象表达式类
class Expression():
 def Interpret(self, context):
   if len(context.PlayText) == 0:
     return
   else:
     playKey = context.PlayText[0:1]
     context.PlayText = context.PlayText[2:]
     tmp = context.PlayText.index(' ') #找出第一个空格出现的位置
     playValue = context.PlayText[0:tmp]
     context.PlayText = context.PlayText[tmp+1:]
     self.Excute(playKey,playValue)

def Excute(self,playKey,playValue):
   pass

#音高
class Pitch(Expression):
 pitch = None
 def Excute(self, key, value):
   value = int(value)
   if value == 1:
     self.pitch = '低音'
   elif value == 2:
     self.pitch = '中音'
   elif value == 3:
     self.pitch = '高音'
   printInfo(self.pitch)

#音符
class Note(Expression):
 Notes = {
 'C':1,  
 'D':2,
 'E':3,  
 'F':4,  
 'G':5,  
 'A':6,  
 'B':7,  
 }
 note = None
 def Excute(self, key, value):    
   self.note = self.Notes[key]
   printInfo('%d' % self.note)

def clientUI():
 context = PlayContext()
 context.PlayText = "O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 "
 expression = None;
 while(len(context.PlayText) > 0):
   str = context.PlayText[0:1];
   if(str == 'O'):
     expression = Pitch()
   elif(str == 'C' or str == 'D' or str == 'E' or str == 'F' or str == 'G' or str == 'A' or str == 'B' or str == 'P'):
     expression = Note()
   expression.Interpret(context)

return

if __name__ == '__main__':
 clientUI();


类图:

Python设计模式编程中解释器模式的简单程序示例分享

标签:Python,设计模式
0
投稿

猜你喜欢

  • 数字人组件反写[asp组件开发实例2]

    2009-06-09 13:15:00
  • python实现的分层随机抽样案例

    2022-06-07 16:10:42
  • 用正则替换所有URL

    2009-03-13 13:51:00
  • Python自动化测试笔试面试题精选

    2021-05-17 03:32:59
  • 用XML和XSL来生成动态页面

    2008-09-04 10:35:00
  • 使用python实现微信小程序自动签到功能

    2021-05-30 10:04:11
  • 浅析设计与内容呈现的关系

    2009-08-06 18:12:00
  • python爬虫之urllib3的使用示例

    2023-01-24 07:40:03
  • golang中使用匿名结构体的方法

    2023-07-10 07:26:56
  • 如何实现文本的卷屏浏览?

    2010-05-24 18:36:00
  • Python shapefile转GeoJson的2种方式实例

    2023-02-20 01:07:52
  • 对python中各个response的使用说明

    2023-01-20 02:43:16
  • 用Python的Django框架完成视频处理任务的教程

    2022-05-07 05:38:25
  • Javascript的错还是浏览器的问题——2009年为何显示为109年

    2009-01-11 18:19:00
  • python的endswith()的使用方法及实例

    2023-04-30 04:42:11
  • 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗

    2011-06-06 10:28:00
  • Python+selenium 获取一组元素属性值的实例

    2021-06-06 02:28:27
  • PyCharm搭建一劳永逸的开发环境

    2022-12-23 20:24:23
  • 详解tensorflow载入数据的三种方式

    2023-07-22 19:35:56
  • Python对象的属性访问过程详解

    2023-09-02 13:40:04
  • asp之家 网络编程 m.aspxhome.com