33Separates data in GUIs from the ways it is presented, and accepted.
44"""
55
6+ # model,存储数据
7+ # view,基于模型api,写视图
8+ # 控制器,不同模型不同视图
69
10+
11+ # 模型
712class Model :
813 def __iter__ (self ):
914 raise NotImplementedError
@@ -18,6 +23,7 @@ def item_type(self):
1823 raise NotImplementedError
1924
2025
26+ # 产品模型
2127class ProductModel (Model ):
2228 class Price (float ):
2329 """A polymorphic way to pass a float with a particular
@@ -44,7 +50,7 @@ def get(self, product):
4450 except KeyError as e :
4551 raise KeyError (str (e ) + " not in the model's item list." )
4652
47-
53+ # 抽象视图
4854class View :
4955 def show_item_list (self , item_type , item_list ):
5056 raise NotImplementedError
@@ -58,6 +64,7 @@ def item_not_found(self, item_type, item_name):
5864 raise NotImplementedError
5965
6066
67+ # 终端视图
6168class ConsoleView (View ):
6269 def show_item_list (self , item_type , item_list ):
6370 print (item_type .upper () + ' LIST:' )
@@ -80,7 +87,7 @@ def show_item_information(self, item_type, item_name, item_info):
8087 def item_not_found (self , item_type , item_name ):
8188 print ('That {} "{}" does not exist in the records' .format (item_type , item_name ))
8289
83-
90+ # 控制器,有模型和视图
8491class Controller :
8592 def __init__ (self , model , view ):
8693 self .model = model
0 commit comments