Programming paradigm
Programming-Write program or code, specifically refers to the program ape with a specific syntax + data structure + algorithm to write code, the purpose is to tell the computer how to perform the task.
If the programming process is likened to practicing martial arts, then the programming paradigm refers to the various genres in martial arts, and the two most common genres in the programming world are: process-oriented and object-oriented
Process-oriented and object-oriented in different scenarios have their pros and cons, who is good who bad can not be generalize, let us learn more about them below.
Process-oriented programming
Concept:
The core is the word "process", "process" refers to the steps to solve the problem, that is, what to do first .... , a process-oriented design program is like designing a pipeline, which is a mechanical way of thinking. If the program starts with a big problem, the basic design of the process-oriented approach is to break the big problem down into a number of small problems or sub-processes that continue to decompose during execution until the small problem is simple enough to be solved in a small step range.
The advantages are:
The complexity of the problem flow, and then simplification (a complex problem, divided into small steps to achieve, the implementation of small steps will be very simple)
To give a typical process-oriented example, write a registration procedure, in three steps, enter the user name, password, verify, write to the database.
Import jsonimport redef Interactive (): name=input (' >>: '). Strip () pwd=input (' >>: '). Strip () return { ' name ': Name, ' pwd ':p wd, }def Check (user_info): is_valid=true If Len (user_info[' Name ']) = = 0: print (' User name cannot be null ') is_valid=false If Len (user_info[' pwd ')) < 6: print (' password cannot be less than 6 bits ') Is_valid=false return { ' is_valid ': is_valid, ' user_info ': User_info }def Register (check _info): if check_info[' is_valid ': with open (' Db.json ', ' W ', encoding= ' utf-8 ') as F: Json.dump (Check_ info[' User_info '],f) def main (): user_info=interactive () Check_info=check (user_info) Register ( Check_info) If __name__ = = ' __main__ ': Main ()
The disadvantages are:
A set of pipeline or process is used to solve a problem, such as the production line of soda can not produce a car, even if it is able to be big change, change a component, and its related components need to be modified, reaching, very poor extensibility.
For example, we want to add a mailbox verification function, then the mailbox-related code to change, the code is as follows:
Import jsonimport redef Interactive (): name=input (' >>: '). Strip () pwd=input (' >>: '). Strip () email=input (' >> '). Strip () return { ' name ': Name, ' pwd ':p wd, ' email ': Email }def Check (user_info): is_valid=true If Len (user_info[' name ']) = = 0: print (' User name cannot be null ') Is_valid=false If Len (user_info[' pwd ')) < 6: print (' password cannot be less than 6 bits ') is_valid=false if not Re.search (R ' @.*?\.com $ ', user_info[' email '): print (' Invalid mailbox format ') Is_valid=false return { ' is_valid ': Is_valid, ' User_info ': User_info }def Register (check_info): if check_info[' is_valid ': with open (' Db.json ', ' W ', encoding= ' utf-8 ') as F: json.dump (check_info[' User_info '],f) def main (): user_info=interactive () Check_info=check (user_info) Register (check_info) If __name__ = = ' __main__ ': Main ()
Application Scenarios:
Process-oriented program design ideas are typically used in scenarios where functionality is rarely changed once implemented, and if you're just writing simple scripts to do some one-off tasks, it's great to use a process-oriented approach, with examples of Linux kernel, git, and Apache HTTP server. But if the task you're dealing with is complex and needs to be constantly iterated and maintained, it's the easiest thing to do with object-oriented.
Object-Oriented Programming
Concept:
The core is the word "object", the object is a combination of characteristics and skills.
The advantages are:
Strong scalability
The disadvantages are:
High degree of programming complexity
Application Scenarios:
User needs change frequently, Internet application, game, Enterprise internal application
Python Object-oriented: What is object-oriented programming