我们如何确定 OOP 中对象的责任?

发布于 2024-11-19 13:48:51 字数 395 浏览 4 评论 0原文

我刚刚开始学习 OOP,我发现很难决定功能所属的位置。让我们在 SO 中使用 down 投票作为示例:

当我们投出一票时,交易中必须发生以下操作:

  1. 减少投票者的 repdownVotes 计数。
  2. 减少收件人的rep
  3. 减少帖子分数

那么...

  1. 我们如何确定哪个动作属于哪个对象呢?
  2. 这样的功能会存在于哪里?在 DAO 层、服务层还是实际对象本身?

当对象相互交互时,事情变得越来越棘手,就像我的例子一样。通常很难决定哪个函数属于哪个对象等等......

I just started learning OOP and I'm finding it really hard to decide where functionality belongs. Let's use a down vote in SO for our example:

When we cast one, the following must happen in a transaction:

  1. Decrement the voter's rep and downVotes count.
  2. Decrement the recipient's rep.
  3. Decrement the post score.

So...

  1. How do we determine which action belongs to which object?
  2. Where would such functionality live? In the DAO layer, services layer, or the actual objects themselves?

It becomes increasingly tricky when objects interact with each other, such as in my example. It's often hard to decide what function belongs to what object and so on...

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

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

发布评论

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

评论(2

挽你眉间 2024-11-26 13:48:51

看看SOLID OO 设计、耦合和优化的原则。凝聚。

OO 可以用在很多地方,它不仅限于业务层。您可以编写面向对象的 Javascript。

我会对您的示例 SO 域进行建模,与此类似(在 C# 中)。这是理想化的 OO 代码,在现实世界中会做出一些妥协,例如为我的 ORM 公开字段。我想要展示的是——每个对象都对其数据负责,没有其他人可以直接更改它;他们必须通过调用其中一个公共方法来要求该对象执行某些操作。

public class User
{
    private int _reputation;
    private int _downvotes;

    public void Downvote(Post post)
    {
        DecreaseReputation();
        IncrementDownvotes();
        post.Downvote();
    }

    public void RegisterDownvote()
    {
        DecreaseReputation();
    }

    private void DecreaseReputation()
    {
        _reputation--;
    }

    private void IncrementDownvotes()
    {
        _downvotes++;
    }
}

public class Post
{
    private int _score;
    private User _poster;

    public void Downvote()
    {
        DecreaseScore();
        _poster.RegisterDownvote();
    }

    private void DecreaseScore()
    {
        _score--;
    }
}

Take a look at SOLID principles of OO design, Coupling & Cohesion.

OO can be used in many places, it is not limited to e.g. your Business Layer. You can write your Javascript object-oriented.

I'd model your example SO domain similar to this (in C#). This is idealistic OO code, and in real world some compromises would be made, such as making fields public for my ORM. What I am trying to show - each object is responsible for its data, noone else can change it directly; they must ask that object to do something, by calling one of the public methods.

public class User
{
    private int _reputation;
    private int _downvotes;

    public void Downvote(Post post)
    {
        DecreaseReputation();
        IncrementDownvotes();
        post.Downvote();
    }

    public void RegisterDownvote()
    {
        DecreaseReputation();
    }

    private void DecreaseReputation()
    {
        _reputation--;
    }

    private void IncrementDownvotes()
    {
        _downvotes++;
    }
}

public class Post
{
    private int _score;
    private User _poster;

    public void Downvote()
    {
        DecreaseScore();
        _poster.RegisterDownvote();
    }

    private void DecreaseScore()
    {
        _score--;
    }
}
罪歌 2024-11-26 13:48:51

这不是一个容易回答的问题,听起来更像是一个设计模式问题,而不是 OOP 问题本身。在 SO 的情况下(我根据其站点的假设设计模式做出假设),设计模式的所有“层”都涉及您所说的“事务”(不是数据库术语,我假设您使用它的方式)。 UI 层或视图接受“否决票”,并向处理业务规则的层发出看似 ajax 请求,该请求决定了当对用户投出“否决票”时实际发生的情况。此时,业务层向数据层发出请求,以更新某处的数据库,以更新用户的分数、声誉等。使用 Web 服务执行此操作也可能略有不同,谁知道 SO 的幕后情况是什么? 。就OOP而言;我确信在幕后有很多 OOP,无处不在,在所有层、脚本和其他语言中,但我想在你的例子中,SO 不会传递一个“User”类对象进行投票;没有必要。

例如,以下是非常流行的 MVC 设计模式: http: //en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

This is not an easy question to answer and sounds more like a design-pattern question than an OOP question per se. In the case of SO (I am making an assumption based on assumed design patterns for their site), all the "layers" of the design-pattern are involved in what you are calling a "transaction" (not a DB term I assume the way you are using it). The UI layer or View accepts the "down vote" and makes a what appears to be an ajax request most likely to a layer that handles business rules, which determines what actually happens when a "down vote" is cast against a user. At that point, the business layer makes requests of the data layer to update a database somewhere to update the user's score, reputation, etc. This also may be performed a little bit differently using web services, who knows what's under the hood here at SO. As far as OOP; I am sure there is a lot of OOP under the hood, everywhere, in all the layers, scripting and other languages perhaps, but I would imagine that in the case of your example, SO is not passing around a "User" class object when a vote is cast; there is no need to.

Here is the very popular MVC design pattern for example: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

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