我在哪里放置格式化逻辑?
我有几个带有原始数据的类,例如:
public interface Transaction {
public double getAmount();
public Date getDate();
}
我需要在几个地方输出该数据的格式化版本。例如,我可能会在网页上将金额显示为 $1,000
,或在 Excel 下载中将金额显示为 1000.00
。我还希望能够在不同的地方重用相同的格式化代码。其中一些很简单(例如以某种格式显示日期),但有些会更复杂(例如根据另一个字段的值显示一个字段的不同值)。
我的问题是:我应该把格式化代码放在哪里?我可以想到几个地方:
向数据对象添加方法,例如
getAmountHTML()
或getAmountExcel()
。方便,但是这是否会使模型和视图的关系过于密切?显示数据时在模板中进行格式化。灵活,但由于它不在方法中,我无法轻松地在不同位置重用相同的格式。
为每个数据类创建一个格式化类,并为其提供对原始数据对象的引用。
我将有很多数据对象需要格式化,所以我想想出一个好的方法。有没有人有相关经验可以分享一下?
I have several classes with raw data, for example:
public interface Transaction {
public double getAmount();
public Date getDate();
}
I need to output formatted versions of this data in several places. For example, I might display the amount as $1,000
on a web page, or 1000.00
on an Excel download. I also want to be able to reuse the same formatting code in different places. Some of it will be simple (like displaying a date in a certain format), but some will be more complex (like displaying different values for one field depending on the value of another field).
My question is: where should I put the formatting code? I can think of a few places:
Add methods to the data object like
getAmountHTML()
orgetAmountExcel()
. Convenient, but does it make the model and view too closely related?Do the formatting in the template, when displaying the data. Flexible, but since it isn't in a method, I can't easily reuse the same formatting in different places.
Create a formatting class for each data class, and give it a reference to the original data object.
I'm going to have a lot of data objects to format, so I'd like to come up with a good approach. Is there anyone with some relevant experience to share?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意 Vivin 的观点,即您不应向数据对象添加各种方法来处理每种可以想象的格式。让视图定义显示字符串是合适的。
您拥有的另一个选项(仍然允许视图定义格式化程序)是使用某种访问者模式。在这里,您的 Transaction 对象接收格式化程序作为新方法的参数:
输出显然是格式化的字符串。这将使您为每个视图拥有一个数字格式化程序,可以将其传递给每个不同的数据对象进行格式化。
I agree with Vivin that you should not add various methods to your data object to handle every conceivable format. Having the View difine the display string is appropriate.
Another option that you have, which still allows for the view to define the formatter, is to use a sort of Visitor pattern. Here, your
Transaction
object receives a formatter as a param to a new method:The output obviously being a formatted string. This would let you have a single number formatter for each view which could be passed to each different data object for formatting.
IMO,格式化数据是一个视图问题。如果您使用 JSTL,则可以使用
。我不喜欢选项 1,因为这意味着您将视图关注点放入域对象中。这里存在方法爆炸的潜力(如果您获得 5 种新格式怎么办 - 您要为每种格式创建一种方法吗?)。
我也不喜欢选项 3,因为您似乎正在为每种格式创建一个格式化类 - 由于选项 1 中确定的原因,这可能会导致类爆炸。
我喜欢选项 2。您可以将选项 3 的某些方面合并到选项 2。您可以有一个
FormattingService
来获取数据和格式并返回格式化数据。您可以通过可在视图中使用的标签公开格式化方法。IMO, formatting the data is a view concern. If you are using JSTL, you can use
<fmt:formatNumber>
.I don't like option 1 because that means you're putting view-concerns into your domain object. There is a potential for method-explosion there (what if you get 5 new formats - are you going to create a method for each one?).
I don't like option 3 either because it seems you're creating a formatting class for each format - this can lead to class explosion for the reasons identified in option 1.
I like option 2. You can combine some aspects of option 3 into option 2. You could have a
FormattingService
that takes the data and a format and returns formatted data. You could expose the formatting method via a tag that you can use in your view.