寻找一个好的模式来在 Java/JSP 中实现对象的视图适配器
这更多的是一个设计问题。假设您有许多 bean A、B、C...然后您有一个类,可以用 bean 创建表。您需要一些东西来告诉此类要显示这些 bean 的哪些属性、列的名称以及其他呈现属性。我不想在每个 bean 类的类中对此进行硬连线,因此我正在考虑为这些 bean 提供一个像 TableAdapterProvider 这样的通用接口,该接口将提供一个向渲染类提供所有指令的类。问题是我无法在 Java 中将接口方法设为静态。由于多种原因,这看起来不太干净......例如,要创建一个空表,我必须分配一个 bean,然后调用接口方法 TableAdapterProvider 来检索“渲染”类。
我想创建一个像 TableAdapterFactory(Class beanClass) 这样的工厂,它为每个 bean 类返回正确的 TableAdapter,而不需要实例化任何 bean 类。无论如何,我必须为我创建的每个 bean 更新这个工厂。
另一种解决方案可以是使用特殊的命名约定,例如,如果 bean 名称是 Apple,则 AppleTableRenderer 将是渲染器。当 TableRenderer 启动时,我可以扫描一个包来搜索它,这样关联就会自动完成。
你知道更好的模式吗?您认为我的方法怎么样?
对于未来,是否有一本包含此类食谱的好书可以在网络应用程序中使用?
this is more a design question. Suppose you have many beans A, B, C.... Then you have a class that creates, say, tables out of beans. You need something that tells this class which properties of those beans to display, what names give to the columns and other rendering properties. I don't want to hardwire this in the class for each bean class, so I was thinking of giving the beans a common interface like TableAdapterProvider, that would come out with a class that gives all instructions to the rendering class. The problem is that I can't make an interface method static in Java. And this doesn't seem very clean for a number of reasons... for example to create an empty table I have to allocate a bean and then call the interface method TableAdapterProvider to retrieve the "rendering" class.
I thought of creating a factory like TableAdapterFactory(Class beanClass)
that returns the correct TableAdapter for each bean class without instanting any. Anyway I have to update this factory for every bean I create.
Another solution could be use special naming conventions, for example if a bean name is Apple, then AppleTableRenderer will be the renderer. I can scan a package in search of this when the TableRenderer starts so the associations are done automatically.
Do you know of a better pattern? What do you think of my approaches?
For the future, is there a good book with recipes like these to use in web apps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,任何需要显示为 HTML 表(通过某种
TableRenderer
)的 bean(A、B、C..)都应该实现一个接口,为渲染器提供所需的信息。可能的接口方法将获取列标题和表数据。也许还有一种表格样式,但这有点主观。考虑到这一点,您可能会:
那么编写
TableRenderer
就非常简单了;请注意,您可能希望使用 JSTL
语法,而不是编写TableRenderer
类,这是一种更标准的方法。Seems to me that any bean (A, B, C..) that needs to be displayed as a HTML table (by some kind of
TableRenderer
) should implement an interface that provides the renderer with the required information. The likely interface methods would get the columns headers and the table data. Maybe also one for table style, but that is a bit subjective.With this in mind you might have;
It's then pretty trivial to write your
TableRenderer
;Note that instead of writing a
TableRenderer
class you might want to use the JSTL<c:forEach>
syntax, which is a more standard approach.也许是一个组合。保留工厂,这对您的任务来说是一个很好的模式:为给定的 bean 创建正确的渲染器的工厂。
但是让工厂尝试找到渲染器 - 通过扫描某个包或从配置文件中读取渲染器类名。
如果工厂找不到渲染器,它可以使用默认渲染器来简单地反映 bean 类并为所有字段创建单元格。
Maybe a combination. Keep the factory, that's a good pattern for your task: a factory that creates the correct renderer for a given bean.
But let the factory try to find the renderer - either by scanning a certain package or by reading the renderer classname from a configuration file.
And if the factory can't find a renderer, it could use a default renderer that simply reflects the bean class and creates cells for all fields.
你可以使用抽象类。
在抽象类中实现选定的通用方法
在每个类中实现类特定方法。
添加了代码。
要生成表,您需要使用 processBean 方法生成的参数调用generateTable方法
you could use an abstract class.
implement chosen common methods in the abstract class
implement class specific method within each class.
added code.
to generate the table you need to call generateTable method with the paremeter generated by the processBean methhod