Python设计模式系列之三: 创建型Factory Method模式
2007-03-30 12:55:15 来源:WEB开发网核心提示: 代码清单2:editor.pyclass Editor:""" 用户使用特定的Editor来编辑个人信息 """# 获取代表用户界面(UI)的对象def getUI(self):pass# 获取用户输入的数据def getCont
代码清单2:editor.py
class Editor:
""" 用户使用特定的Editor来编辑个人信息 """
# 获取代表用户界面(UI)的对象
def getUI(self):
pass
# 获取用户输入的数据
def getContent(self):
pass
# 提交用户输入的数据
def commitChanges(self):
pass
# 清空用户输入的数据
def resetUI(self):
pass
EditableAddress是Editable的一个具体实现,PIM使用它来处理个人地址信息,其完整的代码如清单3所示。
代码清单3:editableaddress.py
from editor import Editor
from editable import Editable
import Tkinter
class EditableAddress(Editable):
""" 用于处理个人地址信息的Editable """
# 构造函数
def __init__(self, master):
self.master = master
self.name = ""
self.province = ""
self.city = ""
self.street = ""
self.zipcode = ""
self.editor = AddressEditor(self)
# 获取相关联的Editor
def getEditor(self):
return self.editor
class AddressEditor(Editor, Tkinter.Frame):
""" 用于处理个人地址信息的Editor """
# 构造函数
def __init__(self, owner):
Tkinter.Frame.__init__(self, owner.master)
self.owner = owner
self.name = Tkinter.StringVar()
self.province = Tkinter.StringVar()
self.city = Tkinter.StringVar()
self.street = Tkinter.StringVar()
self.zipcode = Tkinter.StringVar()
self.createWidgets()
# 构造用户界面
def createWidgets(self):
# 姓名
nameFrame = Tkinter.Frame(self)
nameLabel = Tkinter.Label(nameFrame, text="Name:")
nameEntry = Tkinter.Entry(nameFrame, textvariable=self.name)
nameLabel.config(anchor=Tkinter.E, width=8, pady=3)
nameLabel.pack(side=Tkinter.LEFT)
nameEntry.pack(side=Tkinter.LEFT)
nameFrame.pack()
# 省份
provinceFrame = Tkinter.Frame(self)
provinceLabel = Tkinter.Label(provinceFrame, text="Province:")
provinceEntry = Tkinter.Entry(provinceFrame, textvariable=self.province)
provinceLabel.config(anchor=Tkinter.E, width=8, pady=3)
provinceLabel.pack(side=Tkinter.LEFT)
provinceEntry.pack(side=Tkinter.LEFT)
provinceFrame.pack()
# 城市
cityFrame = Tkinter.Frame(self)
cityLabel = Tkinter.Label(cityFrame, text="City:")
cityEntry = Tkinter.Entry(cityFrame, textvariable=self.city)
cityLabel.config(anchor=Tkinter.E, width=8, pady=3)
cityLabel.pack(side=Tkinter.LEFT)
cityEntry.pack(side=Tkinter.LEFT)
cityFrame.pack()
# 街道
streetFrame = Tkinter.Frame(self)
streetLabel = Tkinter.Label(streetFrame, text="Street:")
streetEntry = Tkinter.Entry(streetFrame, textvariable=self.street)
streetLabel.config(anchor=Tkinter.E, width=8, pady=3)
streetLabel.pack(side=Tkinter.LEFT)
streetEntry.pack(side=Tkinter.LEFT)
streetFrame.pack()
# 邮编
zipcodeFrame = Tkinter.Frame(self)
zipcodeLabel = Tkinter.Label(zipcodeFrame, text="ZIP Code:")
zipcodeEntry = Tkinter.Entry(zipcodeFrame, textvariable=self.zipcode)
zipcodeLabel.config(anchor=Tkinter.E, width=8, pady=3)
zipcodeLabel.pack(side=Tkinter.LEFT)
zipcodeEntry.pack(side=Tkinter.LEFT)
zipcodeFrame.pack()
# 重载Editor中的方法,获取代表用户界面(UI)的对象
def getUI(self):
return self
# 重载Editor中的方法,获取用户输入的数据
def getContent(self):
content = " Name: " + self.name.get() + "
"
content += "Province: " + self.province.get() + "
"
content += " City: " + self.city.get() + "
"
content += " Street: " + self.street.get() + "
"
content += "ZIP Code: " + self.zipcode.get()
return content
# 重载Editor中的方法,提交用户输入的数据
def commitChanges(self):
self.owner.name = self.name.get()
self.owner.province = self.province.get()
self.owner.city = self.city.get()
self.owner.street = self.street.get()
self.owner.zipcode = self.zipcode.get()
# 重载Editor中的方法,清空用户输入的数据
def resetUI(self):
self.name.set("")
self.province.set("")
self.city.set("")
self.street.set("")
self.zipcode.set("")
更多精彩
赞助商链接