OOP - 帖子应该删除自己吗?

发布于 2024-10-14 20:00:12 字数 170 浏览 2 评论 0原文

如果我有一个帖子类(例如,一篇博客文章),它将有一些方法:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海蓝天 2024-10-21 20:00:12

deletePost() 很可能会进入 Post 类的主控中(可能是 Wall 或 BlogPage 或类似的东西)。由于 Wall 将包含帖子(因此具有某种 addPost() 方法,因此您还可以在其中包含 deletePost() 方法!示例(粗略的 Java 语言)如下:

public class Wall {
     ArrayList<Post> wallposts;
     public addPost(Post newpost) {
          this.wallposts.add(newpost);
     }
     public deletePost(Post p) {
          this.wallposts.remove(p);
     }
}

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:

public class Wall {
     ArrayList<Post> wallposts;
     public addPost(Post newpost) {
          this.wallposts.add(newpost);
     }
     public deletePost(Post p) {
          this.wallposts.remove(p);
     }
}
小嗲 2024-10-21 20:00:12

看起来您正在使用 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.

凯凯我们等你回来 2024-10-21 20:00:12

deletePost() 应该放在 Post Manager 类中。在这里,您应该调用Post类的Delete函数。

假设您有一个包含所有帖子的帖子管理器,每当您想要删除某些内容时,帖子管理器都会告诉帖子自行删除。这允许帖子处理有关其自身的所有内容(删除对帖子的任何回复、评论等),以及帖子管理员不应该知道的事情。使用代码

 public class Post{
      .....
      public delete() {
         // Do database stuff here or any clean up that the Post needs to do....
      }
 }


 public class PostManager{
      ArrayList<Post> posts;
      ....
      public deletePost(Post p){
        p.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

 public class Post{
      .....
      public delete() {
         // Do database stuff here or any clean up that the Post needs to do....
      }
 }


 public class PostManager{
      ArrayList<Post> posts;
      ....
      public deletePost(Post p){
        p.delete();
      } 
  }

EDIT: To answer your question, yes, the Post should delete itself. Provided the Post Manager tells it to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文