OOP - 帖子应该删除自己吗?
如果我有一个帖子类(例如,一篇博客文章),它将有一些方法:
- getReplies()
- getViews()
- logView()
等...
但是deletePost() 去了哪里?我认为它不应该出现在课后课程中?
If I have a post class (example, a blog post), it will have a few methods:
- getReplies()
- getViews()
- logView()
etc...
But where does deletePost() go? I would assume it should not go in the post class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
deletePost() 很可能会进入 Post 类的主控中(可能是 Wall 或 BlogPage 或类似的东西)。由于 Wall 将包含帖子(因此具有某种 addPost() 方法,因此您还可以在其中包含 deletePost() 方法!示例(粗略的 Java 语言)如下:
deletePost() would most likely go in the master of the Post class (perhaps Wall or BlogPage or something along those lines). Since the Wall would contain posts (and thereby have some sort of addPost() method, you would also include the deletePost() method there as well! Example (in rough Java) below:
看起来您正在使用 Active Record 模式,那么如果您正在讨论从数据库中删除,则可以在 Post 类中使用 delete() 方法。
您是否使用 DAO 类来获取您的帖子?那么删除应该在那里。
It looks like you are using the Active Record pattern, then if you are talking about deleting from the database, it's fine to have a delete() method inside your Post class.
Are you using a DAO class to get your posts? then the delete should be there.
deletePost() 应该放在 Post Manager 类中。在这里,您应该调用Post类的Delete函数。
假设您有一个包含所有帖子的帖子管理器,每当您想要删除某些内容时,帖子管理器都会告诉帖子自行删除。这允许帖子处理有关其自身的所有内容(删除对帖子的任何回复、评论等),以及帖子管理员不应该知道的事情。使用代码
编辑:要回答您的问题,是的,该帖子应该自行删除。只要邮政经理告诉它就可以。
The deletePost() should go in the Post Manager class. In here, you should call the Delete function of the Post Class.
Assuming that you have a Post Manager that contains all of your Posts, whenever you want to delete something, the Post Manager will tell the Post to delete itself. This allows the Post to handle everything about itself (delete any replies to the posts, comments, etc), things that the Post Manager should not know about. Using the code
EDIT: To answer your question, yes, the Post should delete itself. Provided the Post Manager tells it to.