Python MVC最简单的例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38042632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC the simplest example
提问by Kesto
I'm struggling to understand the MVC pattern. I've been working with MVC frameworks like ASP.NET MVC and Django, but project structure there is pretty much forced, so it really didn't help to understand how to build my own apps based on this pattern. To clear things up i decided to write the simplest example of my understanding of MVC (console program in Python) and figure out if there is anything wrong.
我正在努力理解 MVC 模式。我一直在使用 ASP.NET MVC 和 Django 等 MVC 框架,但是那里的项目结构非常强制,所以理解如何基于这种模式构建我自己的应用程序真的没有帮助。为了解决这个问题,我决定写一个最简单的例子来说明我对 MVC(Python 中的控制台程序)的理解,并找出是否有什么问题。
|- program:
|—— controller.py
|—— model.py
|—— view.py
|—— db.txt #simulates database
So this is my basic structure. What this program will do is display all people that are inside db.txt. I use db.txt(json) to simulate actual database.
所以这是我的基本结构。这个程序会做的是显示所有在 db.txt 中的人。我使用 db.txt(json) 来模拟实际的数据库。
controller.py
控制器.py
from model import Person
import view
def showAll():
#gets list of all Person objects
people_in_db = Person.getAll()
#calls view
return view.showAllView(people_in_db)
def start():
view.startView()
input = raw_input()
if input == 'y':
return showAll()
else:
return view.endView()
if __name__ == "__main__":
#running controller function
start()
view.py
查看.py
from model import Person
def showAllView(list):
print 'In our db we have %i users. Here they are:' % len(list)
for item in list:
print item.name()
def startView():
print 'MVC - the simplest example'
print 'Do you want to see everyone in my db?[y/n]'
def endView():
print 'Goodbye!'
model.py
模型.py
import json
class Person(object):
def __init__(self, first_name = None, last_name = None):
self.first_name = first_name
self.last_name = last_name
#returns Person name, ex: John Doe
def name(self):
return ("%s %s" % (self.first_name,self.last_name))
@classmethod
#returns all people inside db.txt as list of Person objects
def getAll(self):
database = open('db.txt', 'r')
result = []
json_list = json.loads(database.read())
for item in json_list:
item = json.loads(item)
person = Person(item['first_name'], item['last_name'])
result.append(person)
return result
So this is the scenario when the user wants to see all people in the db:
Is this approach correct?
这种方法是否正确?
采纳答案by andrralv
The MVC architecture is very broad and can change depending on the programming language and type of application you are doing, so in this case, yes your approach can be accepted as correct.
MVC 架构非常广泛,可以根据您正在使用的编程语言和应用程序类型而改变,因此在这种情况下,您的方法可以被接受为正确的。
What I have learned from static typed languages is that you define the model and views as complete separate entities, and the controller takes an instance of both model and views as parameters.
我从静态类型语言中学到的是,您将模型和视图定义为完全独立的实体,控制器将模型和视图的实例作为参数。
What you need to ask yourself to define if your app is MVC is the following:
您需要问自己来定义您的应用程序是否为 MVC 如下:
- If I change something in the view do I break anything in the model?
- If I change something in the model do I break anything in the view?
- Is the controller communicating everything in both view and model so that they don't have to communicate with each other?
- 如果我更改视图中的某些内容,我会破坏模型中的任何内容吗?
- 如果我更改模型中的某些内容,是否会破坏视图中的任何内容?
- 控制器是否在视图和模型中通信所有内容,以便它们不必相互通信?
If nothing breaks and the controller does all of the communication then yes, your application is MVC.
如果没有任何问题并且控制器完成所有通信,那么是的,您的应用程序是 MVC。
You might want to look into design patterns such as Singleton, Factory and others that all use the MVC architecture and define ways to implement it.
您可能想要研究诸如 Singleton、Factory 和其他都使用 MVC 架构并定义实现它的方法的设计模式。