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

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

本文介绍了我们如何确定对象在OOP中的责任?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习OOP,发现很难确定功能的归属.让我们在示例中使用SO中的down投票:

当我们强制转换时,必须在事务中发生以下情况:

  1. 减少选民的repdownVotes计数.
  2. 减少收件人的rep.
  3. 减少帖子score.

所以...

  1. 我们如何确定哪个动作属于哪个对象?
  2. 此类功能将在哪里生活?在DAO层,服务层还是实际对象本身?

当对象彼此交互时,例如在我的示例中,这变得越来越棘手.通常很难确定哪个函数属于哪个对象等等.

解决方案

看看 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--;
    }
}

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...

解决方案

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--;
    }
}

这篇关于我们如何确定对象在OOP中的责任?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:06