在桌面应用程序中,我正在为城市开发一个模型。
class City(object):
def __init__(self, name, population):
self._name = name
self._population = population
我想实现一个编辑方法来改变它的私有属性。这个想法是打开一个带有文本输入字段的窗口,以便用户可以编写新值。为了使其可测试和解耦,我这样做了:
# Within City class
def edit(self, get_properties_function=None):
"""
Edit properties. Don't pass get_properties_function except for testing
"""
if get_properties_function is None:
get_properties_function = self._get_city_properties
new_name, new_population = get_new_properties()
self._name = new_name
self._population = new_population
def _get_city_properties(self):
# launch GUI and get the new values
return new_name, new_population
现在的问题:
- 询问信息的对话框是视图,对吗?这引出了下一个问题
- 如果我有看法,我想我应该更进一步,考虑实现一个控制器。那么,这里如何实现 MVC 呢?
我的想法是拥有三个类(MVC),每次我实例化我的“城市概念”时,我都会实例化模型,但也会实例化视图和控制器。而Controller变成了“City”的公共接口这听起来有点矫枉过正,过于复杂了。
由于网络编程,我觉得我误解了真正的 MVC 模式。