OO设计中的耦合

发布于 2024-10-09 00:21:35 字数 467 浏览 6 评论 0原文

我有两个对象。一个会议对象和一个操作对象(会议中提出的操作)。行动也可以独立于会议而存在。我有两种方法将提出的行动与会议联系起来:

  1. 有一种关于会议的方法,我可以在其中使用 传入 Action 对象,例如 “addToMeeting(操作操作)”。 在会议的内部 然后我将操作链接到 会议。对于这种方法,虽然 会议对象需要了解的 并使用 Action 上的方法 对象因此变得耦合。
  2. 有一个我刚刚通过的会议方法 要链接的操作编号,例如 如“addToMeeting(int actionID)”。 现在很好 会议对象没有 需要了解有关 Action 的任何信息 但是......现在代码添加了 会议行动需要知道 如何获取action ID所以有 从此转向 “会议.addToMeeting(操作)”到 这 “会议.addToMeeting(action.getID())”。

对于良好的 OO 设计,应该使用哪种方法?或者还有第三条路吗......

I have two objects. A Meeting object and an Action object (action raised in a meeting). An Action can also exist independent of a Meeting. I have two ways of linking the Action raised to the Meeting:

  1. have a method on Meeting where I
    pass in the Action object such as
    "addToMeeting(Action action)".
    WIthin the internals of Meeting
    I then link the action to the
    meeting. For this approach though the
    Meeting object needs to know about
    and use the methods on the Action
    object so becomes coupled.
  2. have a method on Meeting where I just pass
    the action number to be linked such
    as "addToMeeting(int actionID)".
    Great now Meeting object does not
    need to know anything about Action
    but......now the code adding the
    action to the meeting needs to know
    how to get the action ID so has
    turned from this
    "meeting.addToMeeting(action)" to
    this
    "meeting.addToMeeting(action.getID())".

For good OO design, which approach should be used? Or is there a third way....

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

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

发布评论

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

评论(4

不交电费瞎发啥光 2024-10-16 00:21:35

如果您计划链接到 Meeting 实例的唯一想法是操作,那么让 Meeting 了解 Action 似乎是最合适的>,而不是相反。

Actions 类操作 Meeting 的内部结构会破坏封装性,并且通常会使维护此类代码变得更加困难。因此,在这种情况下,我将在 Meeting 上公开一个方法 addAction(Action a)

但是,如果还有其他内容可以链接到会议,您可能需要考虑抽象“会议项目”的概念。

而不是让 Meeting 了解 < code>Action,反之亦然,您可以定义一个接口,例如 IMeetingItem,它公开 Meeting 链接到此类项目所需的必要信息。然后,Action 将实现 IMeetingItem,从而可以执行以下操作:

meeting.addItem( action );  // action treated as an IMeetingItem in this context

请注意,在这两种方法中,都是由 Meeting 类来调节功能将项目添加到其自身,而不是让添加的项目操纵会议的内部表示。

If the only think you ever plan on linking to Meeting instances are actions, then it would seem most appropriate to make Meeting aware of Action, rather than the other way around.

Having the Actions class manipulate the internals of Meeting breaks encapsulation and generally makes it harder to maintain such code. So, I would expose a method addAction(Action a) on Meeting in this case.

However, if there are other things that can be linked to a meeting, you may want to consider abstracting the concept of "meeting items".

Rather than have Meeting know about Action, or vice verse, you could define an interface such as IMeetingItem which exposes the necessary information that Meeting would need to link to such items. Action would then implement IMeetingItem, making it possible to do something like:

meeting.addItem( action );  // action treated as an IMeetingItem in this context

Note that in both approaches, it is the Meeting class that mediates the functionality of adding an item to itself, rather than having the item being added manipulate the internal representation of a meeting.

心的位置 2024-10-16 00:21:35

我建议您创建一个接口“可识别”,其上的方法 getID() 由 Action 实现

然后您可以执行以下操作:

addToMeeting(Identifiable action);

并在方法内部执行

this.actionId = action.getID();

I would suggest that you create an interface "Identifiable" with the method getID() on it which is implemented by Action

Then you can do the following:

addToMeeting(Identifiable action);

and inside the method do

this.actionId = action.getID();
杀手六號 2024-10-16 00:21:35

只要会议以任何方式与操作相关联,就会产生一定的耦合。但是,您可能会考虑第三种方法,即构建一个生成操作对象的“操作工厂”。一旦创建(并且可能保存),ID 将是 Action 对象的一个​​属性。该会议要做的就是告诉工厂生成一个操作,并能够访问该操作的 ID 属性。

As long as meetings are associated with actions in any way, you'll have some coupling. However, you might consider a third approach, to build an "action factory" that generates action objects. ID would be a property on the Action object, once it's created (and maybe saved). All that meeting would do is tell the factory to generate an action, and be able to access the action's ID property.

○愚か者の日 2024-10-16 00:21:35

我会选择#1——在你的情况下,耦合并不是一件坏事,因为对象之间有明确的关系。我会选择选项#1。这使您可以选择让会议拥有 MeetingActions[] 属性或类似属性。

I would go with option #1 -- coupling is not a bad thing, in your case, since there is a clear relation between objects. I would go with option #1. This gives you the option for a meeting to have a property of MeetingActions[] or something similar.

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