Try   HackMD
tags: python

設計模式:MVC設計模式(Model–View–Controller)

MVC介紹影片

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設計模式:

  • 在人類程式設計經歷中淬煉出來的問題解決方法(招式)
  • 跨程式語言,任何語言都可以使用,只是寫法不同
  • 不同的情境適合不同的設計模式
  • 設計模式可以參考但不用強迫使用
  • 適合的設計模式可以讓程式開發更快速、更有彈性

MVC(Model-View-Controller)設計模式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

View: 負責畫面呈現
Controller: 負責流程、邏輯處理(商業邏輯)
Model: 負責資料庫、檔案讀寫

流程:

  1. 使用者操作畫面(view)
  2. 將使用者操作或輸入資料傳controller
  3. controller根據商業邏輯處理使用者資料
  4. 將所需資料之要求傳給model
  5. model根據需要讀寫資料庫
  6. 資料庫返回處理結果或取出資料
  7. model回傳資料給controller
  8. controller根據商業邏輯處理回傳之資料
  9. 將處理好之資料回傳view
  10. view將資料整合後呈現給使用者觀看

優點:畫面、商業邏輯、資料處理分開處理,結構明確減少錯誤發生,也比較好除錯


實作MVC範例

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

範例程式下載

init.py (負責初始化設定)

import logging logging.basicConfig(level=logging.DEBUG) logging.info("載入init.py") conn = r'C:\Users\test\Desktop\test.db' # 請修改成自己電腦存放路徑!! sql = { "add_user": "insert into user (name, account, password) values ('{0}', '{1}', '{2}')", "check_user": "select * from user where account='{0}' and password='{1}';" }

controller.py (負責系統邏輯處理)

import init, model, view def main(): account, password = view.login() status, message, response = model.check_user(account, password) if status == "success": print(""" 1. 新增使用者 2. 修改使用者 3. 刪除使用者""") else: init.logging.info('status: ' + status+ ' message: ' + message) init.logging.info('response: ' + str(response)) if __name__ == '__main__': init.logging.info("執行controller.py") main()

model.py (負責資料讀寫)

import sqlite3 import init init.logging.info("載入model.py") conn = sqlite3.connect(init.conn) def check_user(account, password): init.logging.info("執行model.check_user()") try: response = conn.execute(init.sql['check_user'].format(account, password)).fetchone() if(response == None): status = 'error' message = '帳號或密碼錯誤,請重新輸入' return status, message, response else: status = 'success' message = '歡迎使用本系統' return status, message, response except Exception as e: status = 'error' message = e return status, message, response # def add_user(name, account, password): # init.logging.info("執行model.add_user()") # try: # conn.execute(init.sql['add_user'].format(name, account, password)) # conn.commit() # status = 'success' # message = '成功新增使用者' # except Exception as e: # status = 'error' # message = e # return status, message

view.py (負責畫面呈現)

import init init.logging.info("載入view.py") def login(): init.logging.info("執行view.login()") account = input("請輸入帳號: ") password = input("請輸入密碼: ") return account, password

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


參考資料

  • Python設計模式深入解析 博碩 Sakis Kasampalis著/江良志譯