如何将数据与业务逻辑解耦

发布于 2024-09-29 00:52:23 字数 1264 浏览 7 评论 0原文

这是场景,

假设我有一个像这样的用户类:

public class User{
  private String firstName;
  private String lastName;
//...
// setter, getters
}

然后我有一个像这样的类来处理用户评论:

public class Comments{
  // some fields
  public static loadComments(User user, int count){...}
}

到目前为止非常基本的东西。但是,我当然想添加一些帮助程序,以便更轻松地为用户加载评论。所以我可以在 User 类中创建一些东西:

final static int defaultCount = 10;
...
public Comment comments(){
  return Comments.loadComments(this, defaultCount);
}

我认为这是一种不必传递用户实例的简单方法。但此时,我很不高兴,因为我已将我的用户 bean 对象与加载评论的业务逻辑耦合在一起。我还在用户类中保存了不应该属于那里的默认计数。那么最好的方法是什么?我的目标是将此对象传递给 jsp,以便可以调用 JSTL 函数。我有一个想法来创建一个 UserWrapper ,它看起来像这样......

public class UserWrapper{
  private final static defaultCount = 10;
  private final User user;
  public UserWrapper(User user){
    this.user = user;
  }

  // should probably cache this but i am not going to show this for simplicity
  public Comments getComments(){return Comments.loadComments(user, 10);}
}

我希望我说得很清楚。我不喜欢使用 useBean 标签,因为对于这样的事情来说它是不必要的。我希望有一种更干净的方法来处理这样的事情!任何帮助将不胜感激!

编辑:我忘记提及一件事。我希望能够在 JSTL 中使用这段代码。这意味着它必须是吸气剂。 DAO 模型众所周知,但当我的前端开发人员需要编写脚本或者我需要将其加载到他可能需要或不需要的地方时,它并没有太大帮助。

Here is the scenario,

Let's say I have a user class like so:

public class User{
  private String firstName;
  private String lastName;
//...
// setter, getters
}

Then I have a class like so to handle user Comments:

public class Comments{
  // some fields
  public static loadComments(User user, int count){...}
}

So far very basic stuff. However, I want to add some helpers of course to make it easier load comments for user. So I could create something in User class:

final static int defaultCount = 10;
...
public Comment comments(){
  return Comments.loadComments(this, defaultCount);
}

I think this is an easy way to not have to pass around user instances around. But at this point, I am unhappy because I have coupled my user bean object with business logic that loads the comment. I have also saved the default count in user class which shouldn't belong there. So what is the best way to do this? My goal is to pass this object to a jsp so that JSTL functions can be called. I had an idea to create a UserWrapper which would look like this...

public class UserWrapper{
  private final static defaultCount = 10;
  private final User user;
  public UserWrapper(User user){
    this.user = user;
  }

  // should probably cache this but i am not going to show this for simplicity
  public Comments getComments(){return Comments.loadComments(user, 10);}
}

I hope I was clear. I don't like using useBean tag because its just not necessary for something like this. I am hoping there is a cleaner way to approach something like this! Any help would be appreciated!

Edit: One thing I forgot to mention. I want to be able to use this code in JSTL. Which means it must be a getter. The DAO model is well known but it doesn't help too much when my front-end developer needs to write a scriplet or I need load it for places that he may or may not need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最丧也最甜 2024-10-06 00:52:23

从独立于技术的角度来看......

是的,关于这种耦合是令人望而却步的,你是完全正确的。请参阅层架构模式,以提供有关构建业务逻辑和数据。例如,设计两个子系统可能是一个好主意:一个用于逻辑,另一个用于层。通过允许逻辑将消息传递到数据层来限制它们之间的通信。此外,您可以使用外观模式来表示每个子系统,从而进一步减少耦合。将每一层视为一个黑盒子。

另一种可能派上用场的模式是数据访问对象模式。它将帮助您定义层之间的合同,以便在必要时传递数据。

From a technology independent stand-point...

Yes, you're absolutely right about that coupling being prohibitive. Look to the Layers architectural pattern to offer some guidance about structure business logic and data. For example, it might be an excellent idea to design two subsystems: One for your Logic, the other for Layers. Restrict communication between them by only allowing the Logic to pass messages to the Data Layer. Furthermore, you can use the Facade pattern to represent each subsystem, and thus decrease coupling even more. Treat each Layer as a black box.

Another pattern that might come in handy is the Data Access Object pattern. It will help you in defining a contract between the Layers to pass data around when it's necessary.

溺ぐ爱和你が 2024-10-06 00:52:23

我喜欢为此使用数据访问对象 (DAO)。您的 UserComment 类将仅包含这些对象的字段,然后您创建单独的类来检索有关这些对象的数据。 DAO 是您包含有关如何检索、更新、选择等的逻辑的地方,例如,

public class UserDAO {

     public UserDAO() {
        //maybe setup a Hibernate connection or JDBC connection here
     }

     public User getUser(int userId) {
        User user;
        //code to retrieve user from datasource
        return user;
     }

     //other methods for update, delete, select, etc
  }

  public class CommentsDAO {
     private static int DEFAULT_COUNT = 10;

     public CommentsDAO() {
        //maybe setup a Hibernate connection or JDBC connection here
     }

     public Collection getCommmentsForUserWithCount(User user, int count) {
        Collection comments = new Set();
        //retrieve comments from datasource limit to count

        return comments;
     }

     public Collection getCommentsForUser(User user) {
        return getCommentsForUserWithCount(user, DEFAULT_COUNT);
     }

     //other methods for update, delete, etc
  }

I like to use Data Access Objects (DAOs) for this. Your User and Comment classes will only contain the fields for those objects and then you create separate classes to retrieve data about those objects. The DAOs are where you include the logic about how to retrieve, update, select, etc For example,

public class UserDAO {

     public UserDAO() {
        //maybe setup a Hibernate connection or JDBC connection here
     }

     public User getUser(int userId) {
        User user;
        //code to retrieve user from datasource
        return user;
     }

     //other methods for update, delete, select, etc
  }

  public class CommentsDAO {
     private static int DEFAULT_COUNT = 10;

     public CommentsDAO() {
        //maybe setup a Hibernate connection or JDBC connection here
     }

     public Collection getCommmentsForUserWithCount(User user, int count) {
        Collection comments = new Set();
        //retrieve comments from datasource limit to count

        return comments;
     }

     public Collection getCommentsForUser(User user) {
        return getCommentsForUserWithCount(user, DEFAULT_COUNT);
     }

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