MVC 是 OOP吗?

先扯些闲话,刚刚走到工作岗位,部门里的”牛人“就告诉我这个小菜鸟,咱们项目要用MVC。对于当时稚嫩的我,自然把这些奉为圣经。恰好我用的开发语言是JAVA,所以我也就顺理成章的认为MVC 就是面向对象设计。现在已经三年过去了,今天在回家的路上突然想起一个问题:MVC是OOP吗?

作为一个虽然工作了三年的菜鸟,我首先想到的是Google一下,然后我就在StackExchange上发现了一个类似的问题: Isn’t MVC anti OOP?

Question Description:
The main idea behind OOP is to unify data and behavior in a single entity - the object. In procedural programming there is data and separately algorithms modifying the data.
In the Model-View-Controller pattern the data and the logic/algorithms are placed in distinct entities, the model and the controller respectively. In an equivalent OOP approach shouldn’t the model and the controller be placed in the same logical entity?

翻译过来就是

面向对象编程背后的主要思想就是将数据与行为统一到一个实体- 对象中去。相反地,在面向过程编程中,存在数据与操作(修改)数据的算法分开的情况。
在 Model(模型)-View(视图)-Controller(控制器)模式中,数据与逻辑/算法被分别地放置在有区分的实体-模型 和控制器。难道在同等的面向对象编程方法中不应该被放在相同的逻辑实体中吗?

下面是投票最高的一个答案:

MVC is an exercise in Separation of Concerns, a UI architecture. It is a way to corral the complexity that can occur in user interfaces due to the presentation not being separated from the content.

In theory, all objects can have behavior that operate on the data they
contain, and that data and behavior remain encapsulated. In practice,
a given OOP object may or may not have logic that corresponds to its
data, or may not have any logic at all (a Data Transfer Object, for
example).

In MVC, the business logic goes in the model, not the controller. The
controller is really just a go-between to glue together the View and
the Model. So in the model, you can have data and behavior in the same
place.

But even that arrangement does not guarantee strict data/behavior
fusion. Objects containing only data can be operated on by other
classes containing only logic, and this is a perfectly acceptable use
of OOP.

翻译过来就是

MVC是一种关注点分离的实践,是一个UI架构(这一点很重要,后面会再讨论)。它是一种将发生在在用户接口由表现没有与内容分离引起的复杂度密封住的方式。

理论上,所有的对象都可以有操作他们拥有的数据的行为,数据与行为保持密封。实践中,一个既存的对象可以也可以没有操作相应数据的逻辑,甚至是不含有任何逻辑(例如:DTO -Data Transfer Object)。

在MVC架构中, 业务逻辑在Model 中开展,而不是Controller。 Controller实际上只是一个将View与Model黏合在一起 的中间人。所以在Model中,我们可以在同一个地方同时拥有数据和行为。

但是即使这样的安排也不能保证严格的数据/行为融合。只包含数据的对象们可以被那些只拥有逻辑行为的对象们操作,这也是一种完全可接受的面对对象编程的使用方式。

另一个答案:

MVC works at a much higher level of abstraction than single objects,
and in fact each of the three (model, view and controller) will
typically consists of many objects that each have both data and
behavior.

That objects which encapsulate data and behavior are a good
fundamental building block for programs in general doesn’t mean it’s
the best pattern at all levels of abstraction and for all purposes.

翻译:
MVC 是工作在比面向对象编程的更高层次的抽象,实际上,MVC中任何一个部分都可以包含很多个对象,
这些对象可能含有数据和行为。
面向对象是一个很好的将数据与行为封装在一起的基础,通常以此来构建程序模块,但是这并不意味这它在所有的抽象级别和所有应用目标上都是最佳的模式。
(和我的观点不谋而合)

讨论点:

  • MVC 只是一种针对UI的架构,也就是说针对的某一个领域的模式,这种设计方式是在OOP之上的,甚至个人理解,MVC 可以脱离OOP
    来实现(有待确认)。所以这也印证了我开头的引子,我加入工作的项目正是一个基于JAVA的Web项目,自然而然要试用MVC架构设计,讲用户接口
    与业务逻辑分离,也就是将关注点分离开,每一个部分解决自己的问题。
  • MVC is a high level architecture pattern. it is applied not only in
    JAVA, but also in C, C++, SmallTalk

下一个思考的问题是:
三层架构与MVC是一回事吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值