MVC模式,将代码分为数据模型、视图、控制逻辑三层。
class Model(object):
def __init__(self):
self.data = []
def add(self, one):
self.data.append(one)
def delete(self, n):
self.data.remove(n)
class View(object):
def __init__(self):
super().__init__()
def show_top_3(self, data):
print(sorted(data)[:3])
def show_all(self, data):
print(data)
class Control(object):
def __init__(self):
pass
def run(self):
view = View()
model = Model()
model.add("a")
model.add("d")
model.add("c")
model.add("b")
n = int(input("0. Show All\n1. Show Top 3\n2. Exit\n"))
if n == 0:
view.show_all(model.data)
elif n == 1:
view.show_top_3(model.data)
else:
pass
def main():
control = Control()
control.run()
if __name__ == '__main__':
main()