5 Programming Paradigms | 5 种编程范式

📚 5 Programming Paradigms | 5 种编程范式

Programming paradigms are fundamental approaches to writing code. In Cambridge International AS and A Level Computer Science, you need to compare the main paradigms and understand how they shape problem solving. This article explains five core paradigms with clear examples and exam-focused insights.

编程范式是编写代码的基本方法。在剑桥国际 AS 和 A Level 计算机科学课程中,你需要比较主要的范式,并理解它们如何影响问题的解决。本文用清晰的例子和考试重点讲解五种核心范式。


1. What Is a Programming Paradigm? | 什么是编程范式

A programming paradigm is a style or model of programming that determines how a programmer structures code, represents data, and controls execution. It is not tied to one specific language, because many modern languages support several paradigms at the same time. For example, Python allows imperative, object-oriented, and functional styles within the same project.

编程范式是一种编程风格或模型,它决定程序员如何组织代码、表示数据和控制执行流程。范式并不局限于某一种语言,因为许多现代语言同时支持多种范式。例如,Python 允许在同一个项目中使用命令式、面向对象和函数式风格。

Understanding paradigms helps you choose the right tool for a problem. Some tasks are easier to express as a sequence of commands, while others fit better as objects, pure functions, logical rules, or declarative queries. Cambridge exam questions often ask you to identify a paradigm from a code sample or to explain why one paradigm is suitable for a given scenario.

理解范式有助于你为问题选择合适的工具。有些任务更容易用一系列命令来表达,而另一些任务更适合用对象、纯函数、逻辑规则或声明式查询来表示。剑桥考试题目经常要求你从代码片段中识别范式,或者解释为什么某种范式适合给定的场景。


2. Imperative Programming | 命令式编程

Imperative programming is the oldest and most direct paradigm. It tells the computer exactly what to do, step by step, by changing the program’s state through assignment statements, loops, and conditionals. The focus is on how to achieve a result rather than on what the result should be.

命令式编程是最古老、最直接的范式。它通过赋值语句、循环和条件判断逐步改变程序状态,告诉计算机确切要做什么。其重点在于如何实现结果,而不是结果本身是什么。

In imperative code, variables represent memory locations whose values can be updated. Control flow is explicit: sequence, selection, and iteration. This makes imperative programs easy to trace for beginners, but large imperative programs can become difficult to maintain because state changes are spread across many lines.

在命令式代码中,变量代表可以更新数值的存储位置。控制流程是显式的:顺序、选择和迭代。这使得命令式程序对初学者来说容易跟踪,但大型命令式程序可能难以维护,因为状态变化分散在许多行代码中。

total ← 0
for i ← 1 to 10
  total ← total + i
output total

This pseudocode adds the numbers 1 to 10 by repeatedly updating the variable total. The state changes on every iteration, which is a classic sign of imperative programming.

这段伪代码通过反复更新变量 total,将数字 1 到 10 相加。状态在每次迭代中都会改变,这是命令式编程的典型特征。

Languages such as C, Pascal, and early versions of BASIC are closely associated with imperative programming, although most languages include imperative features.

C、Pascal 以及早期版本的 BASIC 等语言与命令式编程密切相关,不过大多数语言都包含命令式特性。


3. Object-Oriented Programming | 面向对象编程

Object-oriented programming, or OOP, organises code around objects that combine data and behaviour. Each object is created from a class, which acts as a blueprint. The main principles are encapsulation, inheritance, and polymorphism.

面向对象编程(OOP)围绕对象来组织代码,对象将数据和行为结合在一起。每个对象由一个类创建,类相当于蓝图。其主要原则是封装、继承和多态。

Encapsulation means hiding the internal state of an object and exposing only safe methods to interact with it. Inheritance allows a new class to reuse and extend the properties and methods of an existing class. Polymorphism lets different objects respond to the same method name in their own way.

封装意味着隐藏对象的内部状态,只暴露安全的方法来与它交互。继承允许新类重用并扩展现有类的属性和方法。多态让不同的对象以各自的方式响应同一个方法名。

For example, a class Car might have attributes such as colour and speed, and methods such as accelerate and brake. A class ElectricCar could inherit from Car and override the accelerate method to model different behaviour.

例如,Car 类可以包含 colour 和 speed 等属性,以及 accelerate 和 brake 等方法。ElectricCar 类可以继承 Car 类,并重写 accelerate 方法以模拟不同的行为。

OOP is especially useful for large systems that need to be maintained over time, because related data and operations stay together. Java, C++, and C# are well-known object-oriented languages, while Python and JavaScript support OOP among other styles.

面向对象编程对于需要长期维护的大型系统特别有用,因为相关的数据和操作放在一起。Java、C++ 和 C# 是著名的面向对象语言,而 Python 和 JavaScript 则支持面向对象以及其他风格。


4. Functional Programming | 函数式编程

Functional programming treats computation as the evaluation of mathematical functions. It avoids changing state and mutable data. Programs are built from pure functions that always produce the same output for the same input and have no side effects.

函数式编程把计算视为数学函数的求值。它避免改变状态和可变数据。程序由纯函数构建,纯函数对于相同的输入总是产生相同的输出,并且没有副作用。

Because pure functions do not depend on hidden state, they are easier to test, debug, and run in parallel. Functional languages also treat functions as first-class values, meaning functions can be passed as arguments, returned from other functions, and stored in variables.

由于纯函数不依赖隐藏状态,因此它们更容易测试、调试和并行运行。函数式语言还把函数视为一等值,这意味着函数可以作为参数传递、从其他函数返回,以及存储在变量中。

map(square, [1, 2, 3, 4]) → [1, 4, 9, 16]

Here the square function is applied to each element of a list without modifying the original list. This style is common in Haskell, Lisp, and modern JavaScript or Python functional code.

这里 square 函数被应用到列表的每个元素,而不会修改原始列表。这种风格在 Haskell、Lisp 以及现代 JavaScript 或 Python 的函数式代码中很常见。

Functional programming uses recursion instead of loops in many cases. Higher-order functions such as map, filter, and reduce express transformations concisely and reduce the risk of errors caused by shared mutable state.

函数式编程在许多情况下使用递归代替循环。map、filter 和 reduce 等高阶函数可以简洁地表达变换,并降低共享可变状态导致错误的风险。


5. Logic Programming | 逻辑编程

Logic programming is based on formal logic. A program consists of facts and rules, and the programmer asks questions about what can be inferred from them. The best-known logic programming language is Prolog.

逻辑编程基于形式逻辑。程序由事实和规则组成,程序员提出可以从这些事实和规则中推断出什么的问题。最著名的逻辑编程语言是 Prolog。

In Prolog, a fact might state parent(alice, bob), meaning Alice is a parent of Bob. A rule could define an ancestor: X is an ancestor of Y if X is a parent of Y, or if X is a parent of Z and Z is an ancestor of Y. The system uses unification and backtracking to find answers.

在 Prolog 中,事实可以写为 parent(alice, bob),意思是 Alice 是 Bob 的父母。规则可以定义祖先关系:如果 X 是 Y 的父母,或者 X 是 Z 的父母且 Z 是 Y 的祖先,那么 X 就是 Y 的祖先。系统使用合一和回溯来寻找答案。

Logic programming is powerful for problems involving relationships, constraints, and symbolic reasoning, such as natural language processing, expert systems, and scheduling. However, it can be less efficient for numerical computation or tasks that require fine control over hardware.

逻辑编程在涉及关系、约束和符号推理的问题中非常强大,例如自然语言处理、专家系统和排程。然而,它在数值计算或需要精细控制硬件的任务中效率可能较低。

When answering exam questions, remember that logic programming focuses on what is true and what can be inferred, rather than on a step-by-step algorithm.

回答考试问题时,请记住逻辑编程关注什么是真的、什么可以被推断出来,而不是关注逐步执行的算法。


6. Declarative Programming | 声明式编程

Declarative programming means describing what the desired result is, without specifying the exact control flow needed to achieve it. The computer or engine decides how to solve the problem. SQL is a classic declarative language for querying databases.

声明式编程意味着描述所需的结果是什么,而不指定实现结果所需的精确控制流程。由计算机或引擎决定如何解决问题。SQL 是用于查询数据库的经典声明式语言。

SELECT name FROM students WHERE grade > 80;

This SQL statement declares the rows we want, but it does not tell the database how to scan tables, filter rows, or sort results. The database management system handles those details.

这条 SQL 语句声明了我们想要的行,但没有告诉数据库如何扫描表、过滤行或排序结果。这些细节由数据库管理系统处理。

Declarative programming often leads to shorter and more readable code, because the implementation details are hidden. Web languages such as HTML and CSS are also declarative: they describe page structure and style rather than a sequence of actions.

声明式编程通常会使代码更短、更易读,因为实现细节被隐藏起来。HTML 和 CSS 等 Web 语言也是声明式的:它们描述页面结构和样式,而不是一系列动作。

Some programmers treat logic programming as a subset of declarative programming, since Prolog rules also state what should hold rather than how to compute it. In Cambridge syllabuses, however, it is often useful to discuss them separately to show their distinct features.

一些程序员把逻辑编程视为声明式编程的子集,因为 Prolog 规则也是陈述应该成立什么,而不是如何计算它。不过,在剑桥大纲中,分别讨论它们通常有助于展示各自的特点。


7. Comparing the Five Paradigms | 五种范式对比

The table below summarises the core idea, main strength, and typical languages of each paradigm. Use it as a quick revision aid before exams.

下表总结了每种范式的核心思想、主要优势和典型语言。你可以把它作为考试前的快速复习工具。

Paradigm | 范式 Core Idea | 核心思想 Main Strength | 主要优势 Typical Languages | 典型语言
Imperative | 命令式 Explicit sequence of commands that change state | 改变状态的显式命令序列 Direct control and easy tracing | 直接控制、易于跟踪 C, Pascal, BASIC
Object-oriented | 面向对象 Objects combine data and behaviour | 对象结合数据与行为 Reuse, encapsulation, maintainability | 重用、封装、可维护性 Java, C++, C#
Functional | 函数式 Evaluation of pure functions without side effects | 求值纯函数,无副作用 Testability and concurrency | 可测试性与并发性 Haskell, Lisp, Scala
Logic | 逻辑 Facts and rules with inference | 事实与规则,进行推理 Powerful for relationships and constraints | 擅长关系与约束 Prolog
Declarative | 声明式 Describe what is wanted, not how | 描述想要什么,而非如何做 Short, clear, high-level code | 代码简短、清晰、高层次 SQL, HTML, CSS

When comparing paradigms in an exam, avoid saying that one is always better. Instead, explain the trade-off: imperative gives fine control but can become messy; object-oriented improves structure but adds complexity; functional increases reliability but can be harder for beginners; logic excels at inference but is slower for general computation; declarative is concise but hides important implementation details.

在考试中比较范式时,不要说某一种总是更好。相反,要解释权衡:命令式提供精细控制但可能变得混乱;面向对象改善结构但增加复杂性;函数式提高可靠性但对初学者可能更难;逻辑擅长推理但在通用计算中较慢;声明式简洁但隐藏了重要的实现细节。


8. How Paradigms Influence Code Design | 范式如何影响代码设计

The choice of paradigm affects how you decompose a problem. In imperative programming, you think in terms of steps and state changes. In object-oriented programming, you identify real-world or conceptual objects and their interactions. In functional programming, you break the problem into pure transformations of data.

范式的选择会影响你分解问题的方式。在命令式编程中,你从步骤和状态变化的角度思考。在面向对象编程中,你识别现实世界或概念中的对象及其交互。在函数式编程中,你把问题分解为对数据的纯变换。

Logic programming changes the question entirely: instead of designing an algorithm, you encode knowledge as facts and rules, then let the system search for solutions. Declarative programming focuses on the output specification, leaving the engine to choose the best execution strategy.

逻辑编程则完全改变了问题:你不是设计算法,而是将知识编码为事实和规则,然后让系统搜索解决方案。声明式编程关注输出规范,让引擎选择最佳执行策略。

Paradigms also influence testing. Pure functional code can be tested independently, which reduces debugging time. Object-oriented code can be tested class by class using encapsulation. Imperative code may require more complex test harnesses because state changes are intertwined with control flow.

范式还会影响测试。纯函数式代码可以独立测试,从而减少调试时间。面向对象代码可以利用封装逐个类进行测试。命令式代码可能需要更复杂的测试框架,因为状态变化与控制流程交织在一起。


9. Choosing a Paradigm in Projects | 在项目中选择范式

Real-world projects rarely use only one paradigm. A web application might use declarative HTML and CSS for the interface, object-oriented JavaScript or Python for business logic, and declarative SQL for database queries. This multi-paradigm approach lets each part of the system use the most natural style.

现实世界的项目很少只使用一种范式。一个 Web 应用可能用声明式 HTML 和 CSS 做界面,用面向对象的 JavaScript 或 Python 处理业务逻辑,用声明式 SQL 查询数据库。这种多范式方法让系统的每个部分都使用最自然的风格。

When choosing a paradigm, consider the problem domain. Rule-based expert systems often suit logic programming. Data processing pipelines suit functional or declarative styles. Graphical user interfaces often fit object-oriented design. Low-level system utilities may still be written in an imperative style for performance and control.

选择范式时,要考虑问题领域。基于规则的专家系统通常适合逻辑编程。数据处理管道适合函数式或声明式风格。图形用户界面通常适合面向对象设计。底层系统工具可能仍然采用命令式风格,以便获得性能和控制。

Exam questions may ask you to justify a paradigm for a scenario. Always link your answer to the scenario: mention encapsulation for a banking system, pure functions for parallel data analysis, or facts and rules for a medical diagnosis system.

考试题目可能要求你为某个场景选择范式并说明理由。回答时一定要结合场景:银行系统可以提封装,并行数据分析可以提纯函数,医疗诊断系统可以提事实和规则。


10. Exam Tips and Common Misconceptions | 考试提示与常见误区

A common mistake is to confuse a language with a paradigm. A language such as Python is not a paradigm; it supports several paradigms. You should say ‘Python can be used in an object-oriented style’ rather than ‘Python is an object-oriented paradigm’.

一个常见的错误是把语言和范式混为一谈。像 Python 这样的语言并不是一种范式,它支持多种范式。你应该说“Python 可以以面向对象风格使用”,而不是“Python 是一种面向对象范式”。

Another misconception is that declarative and functional programming are the same. Functional programming emphasises pure functions and immutability, while declarative programming simply means stating what you want without specifying how. SQL is declarative but not functional in the usual sense.

另一个误区是认为声明式编程和函数式编程是一样的。函数式编程强调纯函数和不可变性,而声明式编程只是指说明你想要什么,而不指定如何实现。SQL 是声明式的,但通常意义上并不是函数式的。

When defining key terms, be precise. For example, encapsulation means protecting data by restricting direct access, not just grouping code together. A pure function must have no side effects and must return the same output for the same input. A logic program uses inference, not explicit step-by-step instructions.

定义关键术语时要准确。例如,封装意味着通过限制直接访问来保护数据,而不仅仅是将代码组织在一起。纯函数必须没有副作用,并且对于相同的输入必须返回相同的输出。逻辑程序使用推理,而不是显式的逐步指令。

Use short code or pseudocode examples in longer answers to show understanding. Even a one-line SQL query or a simple class diagram can make your explanation stronger, as long as it is clearly linked to the point you are making.

在较长的答案中,可以使用简短的代码或伪代码示例来展示理解。即使是一行 SQL 查询或一个简单的类图,只要与你的论点清晰相关,都能让解释更有说服力。


Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading