函数式编程和面向对象式编程
As Oracle Corporation has introduced some Functional constructs in Java SE 8, now-a-days most of the Interviewers are interested to ask some questions about Functional Programming.
随着Oracle Corporation在Java SE 8中引入了一些Functional构造,如今,大多数采访者都想问一些有关Functional Programming的问题。
As a Java/Groovy/Scala Developer or any Functional Programming Developer, we should learn the following questions and answers to clear interviews:
作为Java / Groovy / Scala开发人员或任何函数编程开发人员,我们应该学习以下问题和答案以进行清晰的访谈:
What is Functional Programming?什么是函数式编程? What are the “Pros & Cons” of Functional Programming?什么是函数式编程的“利弊”? What are the differences between Functional Programming and Imperative Programming?函数式编程和命令式编程之间有什么区别? What are the differences between Functional Programming and Object Oriented Programming?函数式编程和面向对象的编程之间有什么区别? What are the differences between Functional Programming, Imperative Programming(IP) and Object Oriented Programming?函数式编程,命令式编程(IP)和面向对象的编程之间有什么区别? What are the major advantages of Functional Programming over Imperative Programming or Object Oriented Programming?与命令式编程或面向对象的编程相比,函数式编程的主要优势是什么? When to use Functional Programming?何时使用函数式编程? When to use Object Oriented Programming?何时使用面向对象编程? What are the drawbacks of OOP?OOP的缺点是什么? What are the drawbacks of OOP-Inheritance?OOP继承的缺点是什么?
… And More
… 和更多
In this post, we are going to discuss about the following three popular Programming paradigms one by one and also answer above questions:
在本文中,我们将一个接一个地讨论以下三种流行的编程范例,并回答以上问题:
Functional Programming (FP)函数式编程(FP) Imperative Programming (IP)命令式编程(IP) Object Oriented Programming (OOP)面向对象编程(OOP)
什么是函数式编程? (What is Functional Programming?)
In simple words,Functional Programming (FP)is one of the popular Programming paradigms, which does computation likeMathematical FunctionswithoutChanging StateandMutating Data.
简而言之,函数式编程(FP)是一种流行的编程范式,它可以像数学函数一样进行计算,而无需更改状态和更改数据。
In Functional Programming, Functions are first class candidates. We write programs by defining a set of Functions and Immutable Data.
在函数式编程中,函数是一等候选。 我们通过定义一组函数和不可变数据来编写程序。
Examples of FP Languages:-
Scala, Haskell, Earlang etc are popular FP Languages. Java SE 8 also have some Functional Constructs (Please refer Java 8 posts for more details)
FP语言示例:-
Scala,Haskell,Earlang等是流行的FP语言。 Java SE 8也具有一些功能构造(有关更多详细信息,请参阅Java 8帖子)
FP的特点是什么? (What are the Characteristics of FP?)
Functional Programming Languages like Scala have the following Characteristics
像Scala这样的函数式编程语言具有以下特征
State does not exist.状态不存在。
FP Programs does not contain state. That means all Data is Immutable Data and Functions cannot change state.
FP程序不包含状态。 这意味着所有数据都是不可变数据,功能不能更改状态。
Low importance of Order of Execution执行命令的重要性低
In FP Languages, we write programs with a set of Independent Functions. Functions contain a set of statements.
In FP, the order of execution of those Functions does not have much importance because they do not have state and all Functions work independently. Even if we change the order of execution still they produce the same results.
在FP语言中,我们编写具有一组独立功能的程序。 函数包含一组语句。
在FP中,这些功能的执行顺序并不重要,因为它们没有状态并且所有功能都独立工作。 即使我们更改执行顺序,它们仍然会产生相同的结果。
Stateless Programming Model无状态编程模型
All FP Programs uses Immutable data and Functions, which cannot modify that data. That means FP Languages support Stateless Programming Model.
所有FP程序都使用不可变数据和函数,它们不能修改该数据。 这意味着FP语言支持无状态编程模型。
Functions are first class citizens职能是一等公民
In FP Languages, Functions are first class objects. Functions are independent units, we can execute them in any order.
在FP语言中,函数是一流的对象。 函数是独立的单元,我们可以按任何顺序执行它们。
Primary Manipulations Units主要操作单位
In FP Languages, Primary Manipulations units are Functions and Data Structures because All Programs are made up of using these units.
在FP语言中,“主要操作”单位是功能和数据结构,因为所有程序都由使用这些单位组成。
Modular Programming模块化编程
In FP Languages, we need to write smaller and independent units, called Pure Functions to support Stateless Programming model. That means FP supports better Modularity than OOP.
在FP语言中,我们需要编写较小的独立单元,称为纯函数,以支持无状态编程模型。 这意味着FP比OOP支持更好的模块化。
Higher-order Functions and Lazy Evaluation高阶函数和惰性求值
Functional Programming Languages should support Higher-order functions and Lazy Evaluation features.
函数式编程语言应支持高阶函数和惰性评估功能。
Primary Flow Controls主要流量控制
FP Languages don’t use Flow Controls like For…Loop, Do…While Loop, While…Loop etc and also don’t use Conditional statements like If..Else or Switch Statements. All FP Languages write programs using the following things:
FP语言不使用For ... Loop,Do ... While循环,While ... Loop等流控制,也不使用If..Else或Switch语句之类的条件语句。 所有FP语言都使用以下内容编写程序:
Functions功能 Function calls函数调用 Function calls with Recursion递归函数调用Abstraction,Encaspulation,Inheritance and Polymorphism抽象,包容,继承和多态
Like OOP, FP Languages supports all 4 concepts : Abstraction,Encaspulation,Inheritance and Polymorphism. FP Languages supports Inheritance with Type Classes or Implicits. They support Polymorphism with the help of Generics. It is also known as Parametric Polymorphism.
像OOP一样,FP语言支持所有4个概念:抽象,封装,继承和多态。 FP语言支持带类型类或隐式的继承。 它们在泛型的帮助下支持多态。 也称为参数多态。
FP的主要重点是什么? (What is the main Focus of FP?)
Unlike OOP Languages, All FP Language Programs mainly focus on “What you are doing” or “What is to be done”.
They mainly focus on the following things:
与OOP语言不同,所有FP语言程序都主要关注“您在做什么”或“要做什么”。
他们主要关注以下方面:
What Information is desired that is Inputs.需要什么信息就是输入。 What Transformations are required that is Actual Logic.实际逻辑需要进行哪些转换。
That means FP is mainly focused on“What is to be done”. It does not much focus on“How is it to be done”. That’s we can write Functional Programming just like Problem Domain description. That’s why, not only Developers but also other people can understand FP code very easily.
这意味着FP主要集中在“要做的事情”上。 它并没有过多地关注“如何完成”。 那就是我们可以像编写问题域描述一样编写函数式编程。 因此,不仅开发人员,而且其他人都可以非常轻松地理解FP代码。
Now we will discuss about the “Pros & Cons” (Advantages and Disadvantages) of Functional Programming.
现在,我们将讨论函数式编程的“优点和缺点”(优点和缺点)。
功能编程的优势? (Advantages of Functional Programming?)
Functional Programming Languages have the following Advantages or Benefits or Pros:
函数式编程语言具有以下优点或优点或优点:
Bugs-Free Code无错误的代码
As FP Languages do not support state, they don’t raise any side-effects that means we can write Error-free code or Bugs-free code or Less Error-prone Code.
由于FP语言不支持状态,因此它们不会产生任何副作用,这意味着我们可以编写无错误代码或无错误代码或不太易出错的代码。
Efficient Parallel Programming高效的并行编程
As FP Languages have NO Mutable state, they don’t raise any state-change issues. That means they use only Immutable Data. They use Independent Units to write programs that is “Functions”. We can write very efficient Parallel or Concurrent Programming because they run independently without changing state.
由于FP语言没有可变状态,因此它们不会引发任何状态更改问题。 这意味着他们仅使用不可变数据。 他们使用独立单元编写“功能”程序。 我们可以编写非常有效的并行或并发编程,因为它们独立运行而不更改状态。
Better Performance更好的性能
As FP Programs compose with all Independent units, They can run Parallel or Concurrently. Because of this reason, FP Applications gain better performance.
由于FP程序由所有独立单元组成,因此它们可以并行或并行运行。 因此,FP应用程序可以获得更好的性能。
Better Encaspulation更好的封装
Unlike OOP, FP supports better Encaspulation with Pure Functions. Pure functions means without side-effects.
与OOP不同,FP通过纯函数支持更好的封装。 纯功能意味着没有副作用。
Supports Nested Functions支持嵌套功能
Nested Functions means composing functions with-in other functions to solve problems. FP supports Nested Functions.
嵌套函数是指将函数与其他函数组合在一起以解决问题。 FP支持嵌套函数。
Increase Reusability提高可重用性
As FP programs are made up of Independent Units that is “Functions”, we can reuse them very easily.
由于FP程序由独立单元即“功能”组成,因此我们可以非常轻松地重用它们。
Better Modularity更好的模块化
In FP Languages, we need to write smaller and independent units, called Pure Functions to support Stateless Programming model. That means FP supports better Modularity than OOP.
在FP语言中,我们需要编写较小的独立单元,称为纯函数,以支持无状态编程模型。 这意味着FP比OOP支持更好的模块化。
Easy Lazy Evaluation轻松懒惰评估
In FP Languages, it is very easy to write Lazy Evaluation. They support Lazy Functional Constructs like Lazy Lists, Lazy Maps etc.
在FP语言中,编写惰性评估非常容易。 它们支持惰性功能构造,例如惰性列表,惰性映射等。
Increase Readability and Maintainability提高可读性和可维护性
Functional Programming (FP) also improves Readability and Maintainability because they work Independently and they don’t change state.
函数式编程(FP)还提高了可读性和可维护性,因为它们独立工作并且不更改状态。
Increase Testability提高可测试性
As we write our FP programs using Independent Units that is “Functions”, we can unit test them very easily.
当我们使用独立单元(即“功能”)编写FP程序时,我们可以非常轻松地对其进行单元测试。
Supports Abstraction over Behavior支持行为抽象
Unlike OOP, FP supports both “Abstraction over Data” and “Abstraction over Behavior”. Because Real-world contains both.
与OOP不同,FP支持“基于数据的抽象”和“基于行为的抽象”。 因为真实世界包含这两者。
Support for BigData对BigData的支持
As FP supports Parallel programming and better performance, FP is very good for developing BigData Applications.
由于FP支持并行编程和更好的性能,因此FP非常适合开发BigData应用程序。
Robust and Reliable Code健壮可靠的代码
As FP uses Immutable Data, we can easily develop Robust and Reliable Code using FP.
由于FP使用不可变数据,因此我们可以轻松地使用FP开发健壮且可靠的代码。
函数式编程的缺点? (Drawbacks of Functional Programming?)
Aside from great Benefits, Functional Programming Languages also have very Few or Minimal or Ignorable Drawbacks.
They have only the following Cons:
除了巨大的好处外,函数式编程语言的缺点也很少,很少或很少。
它们只有以下缺点:
Requires Lot of Memory需要大量内存
FP don’t have state. They always creates new Objects to perform actions instead of modifying existing objects. Because of this, FP Applications takes lot of memory.
FP没有状态。 他们总是创建新的对象来执行操作,而不是修改现有对象。 因此,FP应用程序占用大量内存。
Does NOT concentrate on Liskov Substitution不专注于Liskov换人
函数式编程的主要概念是什么? (What are the major concepts of Functional Programming?)
The following concepts are major and important concepts of Functional Programming.
以下概念是函数式编程的主要和重要概念。
First-Class Functions.一流的功能。 Lazy Evaluation.惰性评估。 Higher-Order Functions.高阶函数。 Immutability(Non-Mutable Data).不变性(非可变数据)。 Modularity.模块化。 No Side-effects.没有副作用。 Lazy Evaluation.惰性评估。 Recursive Function-Calls.递归函数调用。
什么是命令式编程? (What is Imperative Programming?)
Imperative Programming (IP) is one of the popular Programming Paradigms which executes a sequence of steps/instructions/statements in some order.
命令式编程(IP)是一种流行的编程范例,它以某种顺序执行一系列步骤/指令/语句。
Examples of IP Languages:-Java, C, C++ etc
IP语言示例:-Java,C,C ++等
命令式编程的主要特征? (Main Characteristics of Imperative Programming?)
Any Imperative Programming (IP) Languages can contain the following Characteristics:
任何命令式编程(IP)语言都可以包含以下特征:
Sequence of Statements.陈述顺序。 Order of execution of Statements is very important.语句的执行顺序非常重要。 They contain state.它们包含状态。 They use both Immutable and Mutable Data.它们同时使用不可变数据和可变数据。 They can change state.他们可以更改状态。 They may have Side-effects.他们可能有副作用。 Stateful Programming Model.有状态编程模型。 They directly change the state of Program.它们直接更改程序的状态。 They represent state with Data Fields.它们用数据字段表示状态。
什么是面向对象编程? (What is Object Oriented Programming?)
Object Oriented Programming is another kind of Programming Paradigm. It represents everything as an Object. Each Object contains Some Data Fields and Methods.
All OOP Programs contains State. They use Mutable Data and Data Structures. Like FP, We can write complete programmings by using Immutable Data, but it does not enforce this rule.
面向对象编程是另一种编程范例。 它将所有内容表示为一个对象。 每个对象都包含一些数据字段和方法。
所有OOP程序都包含状态。 他们使用可变数据和数据结构。 像FP一样,我们可以使用不可变数据编写完整的程序,但是它并不强制执行此规则。
Object Oriented Programming (OOP) is a super set of Imperative Programming. It follows all characteristics of IP with some extra features.
面向对象编程(OOP)是命令式编程的超集。 它遵循IP的所有特性并具有一些额外的功能。
Those extra features are:
这些额外的功能是:
Everything is an Object.一切都是对象。 Each Object contains Some Data Fields and Methods.每个对象都包含一些数据字段和方法。 OOPs Concepts: Abstraction,Encaspulation,Inheritance and PolymorphismOOPs概念:抽象,封装,继承和多态
Unlike Functional Programming Languages, OOP Languages are mainly focused on“How is it to be done”. That means As a Developer, we focus on “How you are doing”.
与功能编程语言不同,OOP语言主要关注“如何完成”。 这意味着作为开发人员,我们专注于“您的工作方式”。
Moreover, OOP combines both“What you are doing”and“How you are doing”. That’s why we cannot write concise and more readable code. Only Developers can understand the code, Other people can get more confusion to understand the application code, they cannot understand it.
此外,OOP结合了“您在做什么”和“您在做什么”。 这就是为什么我们不能编写简洁易读的代码。 只有开发人员才能理解代码,其他人可能会更困惑以了解应用程序代码,他们无法理解。
面向对象编程(OOP)的缺点? (Drawbacks of Object Oriented Programming(OOP)?)
Even though OOP solves many real-time problems, still have the following drawbacks(When Compared to FP):
尽管OOP解决了许多实时问题,但仍存在以下缺点(与FP相比):
It does not support full Reusability.它不支持完全的可重用性。 It is not fully Modularity.它不是完全模块化。 It breaks Encaspulation concept.它打破了包囊概念。 Inheritance has lot of drawbacks.继承有很多弊端。
继承的主要缺点: (Major Drawbacks of Inheritance:)
Breaks Encapsulation principle打破封装原则 When Inheritance levels increases, it is very tough and hard to maintain and create objects.当继承级别增加时,很难维护和创建对象。何时使用函数式编程? (When to use Functional Programming?)
We should go for Functional Programming (FP) in the following scenarios:
在以下情况下,我们应该进行函数式编程(FP):
When we are going to be performing lots of different operations on the data that has fixed.当我们要对已修复的数据执行许多不同的操作时。 In other words, when we have few things with more operations.换句话说,当我们很少进行更多操作时。
何时使用面向对象编程? (When to use Object Oriented Programming?)
We should go for Object Oriented Programming (OOP) in the following scenarios:
在以下情况下,我们应该进行面向对象编程(OOP):
When we are going to be performing few operations on lots of different variants which have common behavior.当我们要对具有共同行为的许多不同变体执行少量操作时。 In other words, when we have more things with few operations.换句话说,当我们有更多的东西而很少的操作时。
NOTE:-
Here Things are Real-world objects and operations are Real-world Actions. For example, In Java we represent these Real-world Things as “Classes” and Real-world Actions as Methods(Operations).
注意:-
这里的事物是真实世界的对象,操作是真实世界的动作。 例如,在Java中,我们将这些现实世界的事物表示为“类”,并将现实世界的动作表示为方法(操作)。
FP和OOP(IP)之间的区别? (Differences between FP and OOP(IP)?)
That’s it all about three popular Programming Paradigms.
这就是三个流行的编程范例的全部。
NOTE:-I’m basically from OOP, but started working on FP just a year ago. So if Functional Programming Experts finds any mistakes in this post, please provide me your valuable inputs.
注意:-我基本上来自OOP,但仅在一年前就开始从事FP工作。 因此,如果函数式编程专家在本文中发现任何错误,请提供您宝贵的意见。
Please drop me a comment if you like my post or have any doubts or suggestions.
如果您喜欢我的帖子或有任何疑问或建议,请给我评论。
翻译自: /8693/functional-imperative-object-oriented-programming-comparison
函数式编程和面向对象式编程