Designed by 77
加载资源 ......
感谢 ♥ 作者
先不感谢了

声明式编程与命令式编程

声明式编程(Declarative Programming)和命令式编程(Imperative Programming)是两种编程范式。

Alright here’s a metaphor.

Declarative Programming is like asking your friend to draw a landscape. You don’t care how they draw it, that’s up to them.

Imperative Programming is like your friend listening to Bob Ross tell them how to paint a landscape. While good ole Bob Ross isn’t exactly commanding, he is giving them step by step directions to get the desired result.

声明式编程就像是你告诉你朋友画一幅画,但是你不用去关心他怎么画。

命令式编程就像是你的朋友完全按照你的命令,将画一步步地画出来。

比如像Spring提供的依赖注入特性,我们将只需要声明在运行过程中需要用到的Bean,Spring就会自动帮我们注入依赖,而不用我们手动去new一个。我们手动去new一个对象这样的做法就是命令式编程,而我们将new一个对象的操作交给Spring容器去实现,这样的做法就是声明式编程。注意到了吗,我们只需要声明在哪里需要用到这些Bean,而不再需要去关心这些Bean在什么时候、在哪里被创建或是怎么样被创建和销毁这样的细节,这就是声明式编程带给我们的好处。

这样的设计放到整个应用层面也是一样的道理,我们更加需要关心的是整个应用和页面的框架结构。

 

"成熟谦卑的人,知道如何面对人生中的失望。"

posted @ 2020-03-29 01:55  yanggb  阅读(630)  评论(1编辑  收藏  举报