命令式编程与声明式编程
Aspect Oriented Programming
(面向方面编程,简称AOP
)是一种Declarative Programming
(声明式编程)。Declarative Programming
(声明式编程)是和Imperative Programming
(命令式编程)相对的概念。我们平时使用的编程语言,比如C++
、Java
、Ruby
、Python
等,都属于Imperative Programming
(命令式编程)。Imperative Programming
(命令式编程)的意思是,程序员需要一步步写清楚程序需要如何做什么(How to do What
)。Declarative Programming
(声明式编程)的意思是,程序员不需要一步步告诉程序如何做,只需要告诉程序在哪些地方做什么(Where to do What
)。比起Imperative Programming
(命令式编程)来,Declarative Programming
(声明式编程)是在一个更高的层次上编程。
SQL为例:
(声明式“叫他做一件事,不用管怎么做”)
SELECT * from dogs
INNER JOIN owners
WHERE dogs.owner_id = [owners.id](https://link.zhihu.com/?target=http%3A//owners.id)
(命令式的实现,需要你教他怎么做)
//dogs = [{name: 'Fido', owner_id: 1}, {...}, ... ]
//owners = [{id: 1, name: 'Bob'}, {...}, ...] var dogsWithOwners = []
var dog, owner
for(var di=0; di < dogs.length; di++) {
dog = dogs[di]
for(var oi=0; oi < owners.length; oi++) {
owner = owners[oi]
if (owner && dog.owner_id == owner.id) {
dogsWithOwners.push ({
dog: dog,
owner: owner
})
}
}}
}
个人感觉:声明式编程思想其实就是函数响应式编程。命令式编程就是链式编程。