面向過程編程

面向過程編程是解決問題的一種思想,至關於武林門派,武林門派之間沒有好壞之分,所以它與咱們以後學習的面向對象編程其實沒有好壞之分。web

面向過程編程,核心是編程二字,過程指的是解決問題的步驟,即先幹什麼、後幹什麼、再幹什麼、而後幹什麼……數據庫

基於該思想編寫程序就比如在設計一條流水線,面向對稱編程實際上是一種機械式的思惟方式。編程

當咱們寫登陸功能,咱們首先須要輸入帳號、密碼,而後認證兩次密碼是否相同,而後從數據庫中讀取密碼驗證用戶密碼輸入是否正確,而後輸入驗證碼……以後,咱們就可以實現登陸功能。這樣把登陸功能問題流程化,進而是解決問題的思路很是清晰。app

56面向過程編程-流水線.jpg?x-oss-process=style/watermark

優勢:複雜的問題流程化,進而簡單化。學習

生產汽水瓶的流水線,沒辦法生產特斯拉。流水線下一個階段的輸入與上一個階段的輸出是有關聯的。所以他的擴展性極差。設計

缺點:擴展性差。code

1、註冊功能

1.1 接受用戶輸入用戶名,進行合法性校驗,拿到合法的用戶名

def check_username():
    username = input('username>>>').strip()
    if username.isalpha():
        return username
    else:
        print('用戶名必須爲字母,傻叉')

1.2 接受用戶輸入密碼,進行合法性校驗,拿到合法的密碼

def check_pwd():
    while True:
        pwd = input('password>>>').strip()
        if len(pwd) < 5:
            print('密碼長度至少五位')
            continue
        re_pwd = input('re_password>>>').strip()
        if pwd == re_pwd:
            return pwd
        else:
            print('兩次輸入密碼不一致')

1.3 將合法的用戶名和密碼寫入文件

def insert(username, pwd, path='57.txt'):
    with open(path, 'a', encoding='utf8') as fa:
        fa.write(f'{username}:{pwd}\n')

1.4 註冊

def register():
    username = check_username()
    pwd = check_pwd()
    insert(username, pwd)
    print(f'{username}註冊成功')


register()
username>>>nick
password>>>12345
re_password>>>12345
nick註冊成功

若是如今咱們須要校驗用戶的年齡,所以咱們須要增長一個check_age()方法,而且其餘有牽連的地方都須要修改,所以它的擴展性極差。對象

1.5 封裝文件讀寫功能

# def register():
#     while True:
#         username = input('username>>>').strip()
#         # 檢測用戶是否重複,若是重複則從新輸入
#         with open('db.txt', 'r', encoding='utf8') as fr:
#             for line in fr:
#                 info = line.strip('\n').split(':')
#                 if username == info[0]:
#                     print('用戶名已經存在')
#                     break
#             else:
#                 # 用戶名不存在
#                 # 跳出循環,不用重複輸入用戶名字
#         res =

# def tell_info():
#     username = input('username>>>').strip()
#     with open('db.txt', 'r', encoding='utf8') as fr:
#         for line in fr:
#             info = line.strip('\n').split(':')
#             if username == info[0]:
#                 return info

數據處理層blog

def select(username):
    with open('db.txt', 'r', encoding='utf8') as fr:
        for line in fr:
            info = line.strip('\n').split(':')
            if username == info[0]:
                return info


def tell_info():
    username = input('username>>>').strip()
    info = select(username)
    print(info)

用戶功能層

def register():
    while True:
        username = input('username>>>').strip()
        # 檢測用戶是否重複,若是重複則從新輸入
        res = select(username)
        if res:
            print('用戶名已經存在')
        else:
            break

    while True:
        pwd = input('password>>>').strip()
        re_pwd = input('re_password>>>').strip()
        if pwd != re_pwd:
            print('兩次輸入密碼不一致,請從新輸入')
        else:
            break

把註冊功能分開以後,功能與功能直接解耦合,複雜的問題流程化,更加清晰。

2、分層實現功能

  • 用戶功能層:實現用戶具體的功能。
  • 接口層:鏈接數據處理層和用戶功能層。
  • 數據處理層:處理數據後把結果交給接口層。

分層實現功能的好處:當咱們須要實現web端和app端的軟件,咱們只要把數據處理層和接口層寫好,而後實現不一樣的用戶功能層便可,web端使用web端的用戶功能層,app端使用app端的用戶功能層,可是接口層和數據處理層是通用的。

相關文章
相關標籤/搜索