返回数据结构以显示信息 [英] Returning a Data Structure to Display information
问题描述
我在SESE上阅读了有关封装和吸气剂/吸气剂的此答案.假设我赞成使用不可变的类,因此,如果实现了setter,它将返回一个反映更改的新对象,例如:
I was reading this answer on SESE about encapsulation and getters/setters. Suppose I favor the use of immutable classes, so if a setter were implemented, it would return the a new object reflecting the change, for example:
//return a new Book reflecting the price change.
public Book updatePrice(double price){}
在链接中,答案建议我有一个称为 getDisplayinformation()
的方法,该方法返回数据结构.
In the link, the answer suggested I have a method called getDisplayinformation()
that returns a data structure.
(考虑一个由枚举,结构或无方法类索引的数组)
(think an array indexed by an enum, a struct, or a methodless class)
根据此建议,我该如何退还一本包含作者名单的书?
Following this advice how would I return a Book with a List of authors?
public final class Author{
private final String id;
private final String firstname;
private final String lastname;
//Constructor
}
public final class Book{
private String bookID;
private final String title;
private List<Author> authorsList;
private double price;
//Constructor
}
假设我想返回一个 Map< String,String>
.
public Map<String,String> getDisplayinformation(){
Map<String,String> displayMap = new HashMap<String,String>();
display.put("BookTitle", title);
display.put("ID", bookID);
display.put("Price", price.toString())
//insert authorsList;
return displayMap;
}
推荐答案
您引用的答案正确了一半.为字段提供吸气剂确实会破坏封装,因为它使调用者依赖于某些内部结构.
The answer you quote got half of it right. Providing getters for fields does break encapsulation, because it makes the caller dependent on some internal structure.
现在,返回一个地图,在此地图中,调用者需要知道键并知道该信息是什么,基本上与为该字段提供获取器没有什么不同.
Now, returning a map where the caller needs to know the keys and knows what that information is, is basically not different than providing a getter for that field.
面向对象试图告诉我们功能需要与数据捆绑在一起.因此, Book
必须具有某种方法来呈现 Book
.我不会称其为 getDisplayInformation()
,而是简称为 display()
.它可以返回某些内容,也可以使用相关的参数.
Object-orientation tries to tell us that the functionality needs to be bundled with the data. So, the Book
has to have some method to present the Book
. I would not call it getDisplayInformation()
, rather simply display()
. It can return something, and it can take relevant parameters too.
重点是, display()
返回的任何内容都必须与演示文稿有关,而与书本无关.在那一刻,应该失去关于书籍的语义,否则呼叫者将紧密地联系在一起.
The point is, that anything returned by display()
must be about the presentation, and must not be about the book. Semantics about being a book should be lost at that point, otherwise the caller will be tightly coupled.
因此,可以返回XML文档,JSON文档,HTML,Wicket Component
,无论您可以使用哪种形式进行演示,而与 Book
无关
So, it's ok to return an XML document, JSON document, HTML, a Wicket Component
, whatever it is you can use for presentation that is independent of the Book
.
或者,该方法可以使用一个参数来表示自己 to .像AWT Component.paint(Graphics g)
之类的.
Or, the method could take an argument to present itself to. Like AWT Component.paint(Graphics g)
or something.
注意:这实际上是一个有争议的主题.混合范式开发(过程和ood的混合)会认为表示需要与对象分离,而面向对象的对象则认为数据和功能始终是同一对象.
Note: this is actually a controversial subject. Mixed-paradigm development (a mix of procedural and ood) would argue that presentation needs to be separated from objects, while object-orientation argues that data and function belong always together.
这篇关于返回数据结构以显示信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!