toString() 和 MVC
toString() and MVC
toString()
的 API 文档指出:
Returns a string representation of the object. In general, the
toString method returns a string that "textually represents" this
object. The result should be a concise but informative representation
that is easy for a person to read. It is recommended that all
subclasses override this method.
(强调我的)
考虑到这一点,我为 Building
类型 class 写了一个 toString()
,它本质上是(一些实现被剥离)
@Override
public String toString(){
Stringbuilder sb = new StringBuilder("<p>");
sb.append (this.getDesc());
sb.append ("which contains: <br> <ul>");
for (Room r: this.getRooms()){
sb.append("<li>");
sb.append(r.getDesc());
sb.append("</li>");
}
sb.append("</ul>");
return sb.toString();
}
给予类似的东西:
A self contained student flat which contains:
- A basic study
- A basic living area
- A basic bathroom
- A basic bedroom
我认为这是一个 "concise but informative representation that is easy for a person to read" 但据我所知,它似乎与 MVC 背道而驰。 Building
和 Room
class 都在模型中,肯定那种输出格式属于视图?
我不确定我是否想多了,Java API 是否没有考虑到 MVC,或者是否有我不理解的地方。可以在 toString()
中进行演示吗?我有一个单独的 displayBuilding()
或类似的更好吗?
toString()
在 java 世界中主要用作调试或显示日志中数据的工具。
对于你所说的目的,很明显toString()
绝对不是工具。
您应该非常明确必须在视图中显示的数据,并且可以一起使用不同的模型,然后在建筑模型中设置值,然后将该模型发送到 UI.
您正在使用 toString()
为您的视图生成 HTML 代码。这不是 toString()
方法的预期用途。我认为,它主要是为了产生调试信息,例如用于日志文件或调试器。这是"easy for a person to read"的意思。 HTML 是包含在网页中的更具技术性的表示。这种方法的名称更像是 render()
.
恕我直言 Object::toString
在日志和调试中非常有用,所以,我会使用纯文本来呈现其输出。
我的建议是:
- 使用 Apache Commons Lang 和方法
ReflectionToStringBuilder::toString
等库快速获得纯文本输出。
用一种方法创建功能接口(例如HtmlRepresentable
)toHtmlString
在你的类中实现这样的方法你想要HTML对象表示。
public interface HtmlRepresentable {
String toHtmlString();
}
然后
public Building implements HtmlRepresentable {
@Override
public String toString() {
StringBuilder sb = new StringBuilder("<p>");
sb.append (this.getDesc());
sb.append ("which contains: <br> <ul>");
for (Room r: this.getRooms()) {
sb.append("<li>" + r.getDesc() + "</li>");
}
sb.append("</ul>");
return sb.toString();
}
toString()
的 API 文档指出:
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
(强调我的)
考虑到这一点,我为 Building
类型 class 写了一个 toString()
,它本质上是(一些实现被剥离)
@Override
public String toString(){
Stringbuilder sb = new StringBuilder("<p>");
sb.append (this.getDesc());
sb.append ("which contains: <br> <ul>");
for (Room r: this.getRooms()){
sb.append("<li>");
sb.append(r.getDesc());
sb.append("</li>");
}
sb.append("</ul>");
return sb.toString();
}
给予类似的东西:
A self contained student flat which contains:
- A basic study
- A basic living area
- A basic bathroom
- A basic bedroom
我认为这是一个 "concise but informative representation that is easy for a person to read" 但据我所知,它似乎与 MVC 背道而驰。 Building
和 Room
class 都在模型中,肯定那种输出格式属于视图?
我不确定我是否想多了,Java API 是否没有考虑到 MVC,或者是否有我不理解的地方。可以在 toString()
中进行演示吗?我有一个单独的 displayBuilding()
或类似的更好吗?
toString()
在 java 世界中主要用作调试或显示日志中数据的工具。
对于你所说的目的,很明显toString()
绝对不是工具。
您应该非常明确必须在视图中显示的数据,并且可以一起使用不同的模型,然后在建筑模型中设置值,然后将该模型发送到 UI.
您正在使用 toString()
为您的视图生成 HTML 代码。这不是 toString()
方法的预期用途。我认为,它主要是为了产生调试信息,例如用于日志文件或调试器。这是"easy for a person to read"的意思。 HTML 是包含在网页中的更具技术性的表示。这种方法的名称更像是 render()
.
恕我直言 Object::toString
在日志和调试中非常有用,所以,我会使用纯文本来呈现其输出。
我的建议是:
- 使用 Apache Commons Lang 和方法
ReflectionToStringBuilder::toString
等库快速获得纯文本输出。 用一种方法创建功能接口(例如
HtmlRepresentable
)toHtmlString
在你的类中实现这样的方法你想要HTML对象表示。public interface HtmlRepresentable { String toHtmlString(); }
然后
public Building implements HtmlRepresentable { @Override public String toString() { StringBuilder sb = new StringBuilder("<p>"); sb.append (this.getDesc()); sb.append ("which contains: <br> <ul>"); for (Room r: this.getRooms()) { sb.append("<li>" + r.getDesc() + "</li>"); } sb.append("</ul>"); return sb.toString(); }