设计模式将转换应用于多个类中的多个属性
我在项目中使用 WMD markdown 编辑器来处理大量字段,这些字段对应于大量实体类中的大量属性。 某些类可能具有多个需要降价的属性。
我正在存储降价本身,因为这使得以后编辑字段变得更容易。 但是,我需要将属性转换为 HTML 以便稍后显示。 问题是:是否可以使用某种模式来避免在所有实体类中编写 Markdown 转换代码?
我创建了一个实用程序类,其方法接受 Markdown 字符串并返回 HTML。 我正在使用 markdownj,效果很好。
问题是对于存储 markdown 的每个类的每个属性,我可能需要另一种转换为 HTML 的方法:
public class Course{
private String description;
.
.
.
public String getDescription(){
return description;
}
public String getDescriptionAsHTML(){
return MarkdownUtil.convert(getDescription());
}
.
.
.
}
问题是,如果 Course 类还有 2 个以上的属性 Tuition 和先决条件说,两者都需要转换器,那么我将不得不编写getTuitionAsHTML() 和 getPrecessionAsHTML()。
我发现这有点难看,想要一个更干净的解决方案。 需要此功能的类不是单个继承层次结构的一部分。
我正在考虑的另一个选择是在控制器而不是模型中执行此操作。 您对此有何看法?
谢谢。
[编辑]:新想法(感谢贾斯珀)。 由于该项目使用struts2(我之前没有说过),我可以创建一个视图组件来为我转换markdown。 然后我在需要将值显示为 HTML 的地方使用它。
I am using the WMD markdown editor in a project for a large number of fields that correspond to a large number of properties in a large number of Entity classes. Some classes may have multiple properties that require the markdown.
I am storing the markdown itself since this makes it easier to edit the fields later. However, I need to convert the properties to HTML for display later on. The question is: is there some pattern that I can use to avoid writing markdown conversion code in all my entity classes?
I created a utility class with a method that accepts a markdown string and returns the HTML. I am using markdownj and this works fine.
The problem is for each property of each class that stores markdown I may need another method that converts to HTML:
public class Course{
private String description;
.
.
.
public String getDescription(){
return description;
}
public String getDescriptionAsHTML(){
return MarkdownUtil.convert(getDescription());
}
.
.
.
}
The problem there is that if the Course class has 2 more properties Tuition and Prerequisites say, that both need converters then I will have to write getTuitionAsHTML() and getPrerequisiteAsHTML().
I find that a bit ugly and would like a cleaner solution. The classes that require this are not part of a single inheritance hierarchy.
The other option I am considering is doing this in the controller rather than the model. What are your thoughts on this?
Thanks.
[EDIT]: New thoughts (Thanks Jasper). Since the project uses struts2 (I did not say this before) I could create a view component say that will convert the markdown for me. Then I use that wherever I need to display the value as HTML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们至少应该实现一个通用接口,否则提出一个干净的通用解决方案将会很麻烦。
这显然是视图的责任。 #1 MVC 规则是模型不关心它的表示,在本例中是降价。
然而,我觉得关于您当前的架构的细节很少,无法对您的问题给出有意义的答案。
They should at least implement a common interface, otherwise coming up with a clean generic solution is going to be cumbersome.
This clearly is a responsibility of the View. The #1 MVC rule is that the Model doesn't care about its representation, the markdown in this case.
However, I feel that there is to little detail about your current architecture to give a meaningful answer to your question.
如果您无法使用继承或接口,那么您确实有一种选择来执行此操作。 我知道,我知道重构,但这是现实,*命中发生了。
您可以使用反射来迭代属性并将格式应用于它们。 您可以使用属性标记它们,也可以采用命名方案(虽然很脆弱,但仍然是一种选择)。
You do have one option for doing this if you can't use inheritance or an interface. I know, I know refactor but this is reality and *hit happens.
You can use reflection to iterate over your properties and apply the formatting to them. You could either tag them with an attribute or you could adopt a naming scheme (brittle, but still an option).
忽略架构问题,我认为简单的答案可能是:
更好的是让 MarkDownUtil 实现 IStringConverter,并且您可以
用于不同作业的几个不同的 StringConverter。
Ignoring the architectural problems, I think the simple answer could be:
Even better would be to make MarkDownUtil implement IStringConverter, and you could have
several different StringConverters for different jobs.