If I don't want to expose the state of my object, but I still need to display it (in HTML, XML or JSON let's say), how would I go about doing that in an MVC environment. Does it make sense to have an export method which exports a dumbed down immutable object (a "data class" if you will). What about adding in a render method which talks to an interface? Are there any other approaches to this problem?
如果我不想暴露我的对象的状态,但我仍然需要显示它(用HTML,XML或JSON说),我将如何在MVC环境中这样做。有一个导出方法导出一个愚蠢的不可变对象(如果你愿意的话,是一个“数据类”)是否有意义。如何添加与界面对话的渲染方法呢?有没有其他方法来解决这个问题?
2 个解决方案
#1
The render method comes the closest to not exposing state. Another method (well-known to Smarty users) is to feed the view non-object data structures to work with.
渲染方法最接近不暴露状态。另一种方法(Smarty用户熟知)是提供视图非对象数据结构以供使用。
It's worth asking, though, what is the problem that these abstractions and/or the interface hiding they serve are solving? If you're going to be doing all this work, IMO you should make sure there's some work it's saving you.
但是,值得一提的是,这些抽象和/或它们所服务的界面正在解决的问题是什么?如果你要做所有这些工作,IMO你应该确保有一些工作可以帮助你。
#2
The point of encapsulation is hiding implementation. There is "state" that other objects do need to know about -- sometimes the whole purpose of an object is telling such state.
封装的重点是隐藏实现。其他对象确实需要知道“状态” - 有时对象的整个目的是告诉这种状态。
For instance, it would be pretty useless to have a Defined Finite Automata object which doesn't have a getter for whether it's on a final state or not. But it's certainly useful to encapsulate how that information is stored internally.
例如,拥有一个Defined Finite Automata对象是没有用的,因为它没有getter,无论它是否处于最终状态。但是封装内部存储信息的方式肯定是有用的。
Now, the goal of getters and setters is to provide an interface to properties of a state of an object, which might not have anything to do with how it is actually implemented. Granted, most of the time these properties are fields in the object, and the getters and setters just shallow shells. On the other hand, you have the freedom to change the internal implementation, because the getters and setters will ensure interface compatibility.
现在,getter和setter的目标是为对象状态的属性提供一个接口,这可能与它实际实现的方式无关。当然,大多数情况下,这些属性是对象中的字段,而getter和setter只是浅壳。另一方面,您可以*更改内部实现,因为getter和setter将确保接口兼容性。
If, however, these properties are too related to the actual implementation and not so much with what the object is about, then you should consider removing such getters and setters.
但是,如果这些属性与实际实现过于相关,而与对象的内容无关,那么您应该考虑删除这些getter和setter。
Having said all that, if you need to display something to the user, then this is a property other objects need to know about. An object shouldn't be concerned with how to display itself, for many reasons. One of them is that it can't know how it should be rendered without knowing how it is used by the application, which would make IT know too much about the other objects.
说了这么多,如果你需要向用户展示一些东西,那么这是其他对象需要了解的属性。出于多种原因,对象不应该关注如何显示自身。其中之一就是它不知道如何在不知道应用程序如何使用它的情况下呈现它,这将使IT对其他对象了解太多。
#1
The render method comes the closest to not exposing state. Another method (well-known to Smarty users) is to feed the view non-object data structures to work with.
渲染方法最接近不暴露状态。另一种方法(Smarty用户熟知)是提供视图非对象数据结构以供使用。
It's worth asking, though, what is the problem that these abstractions and/or the interface hiding they serve are solving? If you're going to be doing all this work, IMO you should make sure there's some work it's saving you.
但是,值得一提的是,这些抽象和/或它们所服务的界面正在解决的问题是什么?如果你要做所有这些工作,IMO你应该确保有一些工作可以帮助你。
#2
The point of encapsulation is hiding implementation. There is "state" that other objects do need to know about -- sometimes the whole purpose of an object is telling such state.
封装的重点是隐藏实现。其他对象确实需要知道“状态” - 有时对象的整个目的是告诉这种状态。
For instance, it would be pretty useless to have a Defined Finite Automata object which doesn't have a getter for whether it's on a final state or not. But it's certainly useful to encapsulate how that information is stored internally.
例如,拥有一个Defined Finite Automata对象是没有用的,因为它没有getter,无论它是否处于最终状态。但是封装内部存储信息的方式肯定是有用的。
Now, the goal of getters and setters is to provide an interface to properties of a state of an object, which might not have anything to do with how it is actually implemented. Granted, most of the time these properties are fields in the object, and the getters and setters just shallow shells. On the other hand, you have the freedom to change the internal implementation, because the getters and setters will ensure interface compatibility.
现在,getter和setter的目标是为对象状态的属性提供一个接口,这可能与它实际实现的方式无关。当然,大多数情况下,这些属性是对象中的字段,而getter和setter只是浅壳。另一方面,您可以*更改内部实现,因为getter和setter将确保接口兼容性。
If, however, these properties are too related to the actual implementation and not so much with what the object is about, then you should consider removing such getters and setters.
但是,如果这些属性与实际实现过于相关,而与对象的内容无关,那么您应该考虑删除这些getter和setter。
Having said all that, if you need to display something to the user, then this is a property other objects need to know about. An object shouldn't be concerned with how to display itself, for many reasons. One of them is that it can't know how it should be rendered without knowing how it is used by the application, which would make IT know too much about the other objects.
说了这么多,如果你需要向用户展示一些东西,那么这是其他对象需要了解的属性。出于多种原因,对象不应该关注如何显示自身。其中之一就是它不知道如何在不知道应用程序如何使用它的情况下呈现它,这将使IT对其他对象了解太多。