与内部对象交互

发布于 2024-10-01 08:26:18 字数 495 浏览 0 评论 0原文

我正在开发一个案例记录系统。每个案例都可以附加注释,因此我将创建一个 Case 类和一个 Comment 类,并在 Case 类中嵌入一个 Comment 对象。

我正在考虑如何为嵌入的 Comment 对象提供接口。我可以将其设为公共成员并让程序员直接访问它($case -> comment -> addComment()),或者将其设为私有并将方法放入 Case 类中以提供通过 Comment 对象访问评论 (public function addComment () { return ($this -> comment -> addComment ()); })。

想想看,后一种风格意味着 Case 对象变得依赖于 Comment 对象。然而,在前一种情况下,程序员可能能够做一些事情,例如使 Case 对象的 comments 属性引用不属于该 case 的注释,甚至引用不属于注释对象的项目!

在这两种方法中,哪一种被认为是最佳实践?

I'm working on a case logging system. Each case can have comments attached, so I'm going to create a Case class and a Comment class and embed a Comment object in the Case class.

I'm thinking about how to provide the interface to the embedded Comment object. I could make it a public member and have the programmer access it directly, ($case -> comment -> addComment()), or make it private and put methods in the Case class to provide access to the comments through the Comment object (public function addComment () { return ($this -> comment -> addComment ()); }).

Thinking about it, the latter style would mean that the Case object becomes dependant on the Comment object. However, in the former case, the programmer may be able to do things like make the comments property of the Case object reference comments that don't belong to the case, or even to items that aren't comment objects!

Of the two approaches, which is considered to be the best practice?

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

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

发布评论

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

评论(2

溺深海 2024-10-08 08:26:18

我不太明白你的意思,但是......

我认为你需要这样的东西:

class Case {
    private $comment;

    public function getComment() {
        // Add some checks if necessary
        ...
        return $this->comment;
    }

    public function addComment(Comment $comment) {
        // Add some checks if necessary
        ...           
        $this->comment = $comment;
    }
}

你也可以选择让你的 $comment 受到适当的保护。如果您想扩展对象,这尤其有用。

总是大量使用封装,这是OOP的最大优点之一。将类财产设为受保护或私有始终是明智的。如果没有它,您在设置属性时将无法强制检查它。请记住,如果您将属性公开,每个类都可以访问和修改它,而无需任何检查。想想这可能带来的后果吧!

$comment 不是原始类型,它是对评论类实例的引用

祝你好运!

@Gordon:没关系。即使它是 Comment 的实例并且您将其公开,您仍然可以这样做:

// $Case is a Case object, doh ;)...
$Case->comment = "Just a string value";

突然您的注释是原始类型,而您期望 Comment 类的实例...这可能会给您带来一些错误。如果将其设为受保护或私有属性,则该代码行在类之外是不可能的。然后你被迫使用一个设置器,如下所示:

public function setComment(Comment $comment) {
    ...
}

你的函数需要一个注释,如果给出其他内容,就会抛出错误。这将确保您的对象是一致的。

@Gordon:它不是您在 Case 类中构建的 Comment 类方法。仅您的 Case 类需要上述方法。它们所做的唯一事情就是获取属于Case 类的Comment 对象并设置属于Case 类的Comment 对象。使用 getter 成功检索 Comment 对象后,您可以触发 Comment 类中定义的所有公共方法。所以它对 Comment 对象本身没有任何作用。

您的评论仍然是完全独立的。这些对象之间的关系(很可能)保存在 Case 对象的表中。您的 Case 类是唯一了解这种关系的类。因此,您仍然可以将 Comment 类重新用于其他目的。

您的 Case 类需要以某种方式了解您的 Comment 类,这就是正确的方法!

顺便说一句,考虑多对多关系。我假设一个案例可以有多个评论。

祝你好运!

I don´t exactly understand what you mean, but...

I think you need something like this:

class Case {
    private $comment;

    public function getComment() {
        // Add some checks if necessary
        ...
        return $this->comment;
    }

    public function addComment(Comment $comment) {
        // Add some checks if necessary
        ...           
        $this->comment = $comment;
    }
}

You can also choose to make your $comment propery protected. This is especially useful if you want to extend the object.

Always make extensive use of encapsulation, it is one of the biggest advantages of OOP. It is always wise to make a class property protected or private. Without it you can't force checks on it when you set the properties. Keep in mind if you make a property public, every class can access and modify it without any checks. Think about the possible consequences of that!

$comment isn't a primitive type, its a reference to an instance of a comment clas

Good luck!

@Gordon: Doesn't matter. Even if it is a instance of a Comment and you make it public, you can still do so something like:

// $Case is a Case object, doh ;)...
$Case->comment = "Just a string value";

And suddenly your comment is a primitive type while you expect an instance of a Comment class... This can give you several errors. If you make it a protected or private property the line of code is just not possible outside of the class. Then you're forced to use a setter for it like:

public function setComment(Comment $comment) {
    ...
}

Your function expects a Comment and will throw an error if something else is given. This will ensure your object is consistent.

@Gordon: It is not a Comment class method you're building in your Case class. The methods described above are only needed for your Case class. The only thing they do is getting the Comment object that belongs to the Case class and Setting the Comment object that belongs to the Case class. Once you succesfully retrieved the Comment object with the getter, you can fire all public methods defined in your Comment class. So it does nothing with the Comment object itself.

Your Comment is still completely independent. The relationship between these objects is (most likely) saved in the table of the Case object. Your Case class is the only class that knows about the relationship. So you can still re-use your Comment class for other purposes.

Your Case class needs to know your Comment class in some way and this is the way to go!

BTW, consider a many-to-many relationship. I assume a Case can have more then one Comment.

Good luck!

谷夏 2024-10-08 08:26:18

Case 类应该有自己的方法来添加/删除注释。这使您可以更改将来实施评论的方式。您现在可以让这些方法简单地包装对 Comment 类的调用,但将来可以完全更改它。

The Case class should have its own methods to add/remove comments. This lets you change how comments are implemented in the future. You can have these methods simply wrap calls to your Comment class now but change it totally in the future.

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