OOP,函数式编程,过程 [英] OOP vs Functional Programming vs Procedural

查看:110
本文介绍了OOP,函数式编程,过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些编程范例之间有什么区别,它们是否更适合于特定问题,还是任何用例都比其他用例更受青睐?

What are the differences between these programming paradigms, and are they better suited to particular problems or do any use-cases favour one over the others?

赞赏建筑实例!

推荐答案

所有方法都有其自身的优点-它们只是解决相同问题的不同方法.

All of them are good in their own ways - They're simply different approaches to the same problems.

在纯粹的过程样式中,数据往往与在其上运行的功能高度脱钩.

In a purely procedural style, data tends to be highly decoupled from the functions that operate on it.

在面向对象的样式中,数据倾向于带有一系列功能.

In an object oriented style, data tends to carry with it a collection of functions.

在一种功能样式中,数据和功能趋向于彼此之间有更多的共同点(如Lisp和Scheme),同时在实际使用功能方面提供了更大的灵活性.还倾向于根据递归和组合来定义算法,而不是根据循环和迭代来定义.

In a functional style, data and functions tend toward having more in common with each other (as in Lisp and Scheme) while offering more flexibility in terms of how functions are actually used. Algorithms tend also to be defined in terms of recursion and composition rather than loops and iteration.

当然,语言本身仅影响首选的样式.即使使用像Haskell这样的纯功能语言,也可以使用过程样式(尽管不鼓励这样做)进行编写,甚至可以使用像C这样的过程语言来编写面向对象的样式(例如,使用GTK +和EFL API).

Of course, the language itself only influences which style is preferred. Even in a pure-functional language like Haskell, you can write in a procedural style (though that is highly discouraged), and even in a procedural language like C, you can program in an object-oriented style (such as in the GTK+ and EFL APIs).

需要明确的是,每个范例的优势"仅在于算法和数据结构的建模.例如,如果您的算法涉及列表和树,那么功能性算法可能是最明智的.或者,例如,如果您的数据是高度结构化的,则如果这是您语言的本机范式,则将其作为对象进行组合可能更有意义-或者,可以将其简单地编写为monad的功能抽象,是Haskell或ML之类的语言的原生范例.

To be clear, the "advantage" of each paradigm is simply in the modeling of your algorithms and data structures. If, for example, your algorithm involves lists and trees, a functional algorithm may be the most sensible. Or, if, for example, your data is highly structured, it may make more sense to compose it as objects if that is the native paradigm of your language - or, it could just as easily be written as a functional abstraction of monads, which is the native paradigm of languages like Haskell or ML.

您使用哪种选择仅仅是对您的项目和语言支持的抽象更有意义.

The choice of which you use is simply what makes more sense for your project and the abstractions your language supports.

这篇关于OOP,函数式编程,过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆