Fake Objects

来源:互联网 发布:西门子s7一200编程视频 编辑:程序博客网 时间:2024/06/10 04:54

Spring的创始人Rod Johnson关于伪对象:

Fake Objects: objects in appearance, but which don’t exhibit some of the characteristics of objects:identity, state, and behavior。

Let’s look at some common fake objects and why to avoid them if possible:
Transfer objects, often referred to as Data Transfer Objects (DTOs) or Value Objects. Transfer
objects are not true objects because they contain only state, without behavior. Transfer objects
are a necessary evil in distributed applications (although there’s an argument that this kind of
data should really be included in an XML structure rather than Java objects). But if we don’t
want a distributed architecture, they’re redundant and harmful, creating an unwelcome
impedance mismatch between business services and callers.
❑ Entity beans or other persistent objects generated from RDBMS tables. These reflect a relational,
rather than OO, model. They have a harmful effect on code that works with them, which
is forced to navigate relationships and cannot benefit from OO concepts such as polymorphism.
There’s also inadequate decoupling between business logic and persistent data representation.
Persistent objects in general that contain only getters and setters, without behavior. Such fake
objects force behavior that they should encapsulate to be moved into control classes such as session
beans. Not only is encapsulation violated, but the result tends to be verbose code in control
classes.

UI+BLL+DAL+Model 是当前最常用的企业级开发的三层架构。但通常BLL层弱化为控制部分,即业务对象只有行为无状态,而Model层弱化为数据库的持久化表示,即只有状态而无行为。DAL变为BLL和DB的中介,而媒介就是Model。

如何由伪对象过度到真正的OO? 创建真正的BO,即有行为又有状态,这样就取消了Model层。



0 0