组合与委托

发布于 2024-08-18 18:53:58 字数 450 浏览 3 评论 0原文

组合设计与委托有何不同,在实现方面有什么不同吗?例如,下面的代码似乎正在执行委托,因为用户在不使用 b 的情况下无法访问组合对象(即“a”)。因此,用户需要调用类 b 的接口,然后“类 b”调用“类 a”的适当接口,使其委托。这有道理吗?

Class A {
friend class B;
private: 
A(){}; //dont want user to instantiate this class object since it wont sense without any context. Just like a room with no house.
void PrintStructure(){};
};

Class B{
public:
void PrintStructure(){a.PrintStructure();} //delegate

private:
A a; //composition
};

Is there any difference in terms of implementation as how a composition design can be different from delegation. For example the code below seems to be doing delegation since the user cannot access the composed object (i.e "a") without using b. Hence, the user would need to invoke interfaces of class b and then "class b" invoke appropriate interfaces of "class a" making it delegation. Does this make sense ?

Class A {
friend class B;
private: 
A(){}; //dont want user to instantiate this class object since it wont sense without any context. Just like a room with no house.
void PrintStructure(){};
};

Class B{
public:
void PrintStructure(){a.PrintStructure();} //delegate

private:
A a; //composition
};

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

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

发布评论

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

评论(3

梦幻之岛 2024-08-25 18:53:58

术语“组合”通常在对象建模方面用作“has-a”关系的表达,并且是关联的一种形式(另一种形式是聚合)。这通常与“继承”(“is-a”关系)形成对比。那么:

组合和聚合有什么区别?组合意味着如果没有父级的上下文,子级就无法存在。

例如,一栋房屋有一个或多个房间。这就是组合关系。删除房子,房间也就不复存在。房子也有许多居住者,是 Person 的实例。这是一种聚合关系,因为这些人存在于该房子的上下文之外。

委托只不过是一个实现细节。类有一个描述其状态和行为的公共接口。如何实施并不重要。它可以委托给其他对象,也可以不委托。

您会注意到示例中的 A 和 B 具有相同的外部接口。更常见的是做这样的事情:

// this represents an interface
class A {
public:
  virtual void printStructure() = 0;
}

使用具体的类:

class ConcreteA : A {
public:
  virtual void printStructure() { ... }
}

class DelegateA : A {
public:
  DelegateA(A& a) { this.a = a; }
  virtual void printStructure() { a.printStructure(); }
private:
  A a;
}

请原谅我可能的 C++ 语法错误。我有点生锈了。

The term "composition" is usually used in terms of object modelling as an expression of a "has-a" relationship and is a form of association (another being aggregation). This is usually contrasted with "inheritance" ("is-a" relationship). So:

What's the difference between composition and aggregation? Composition implies that the child cannot exist without the context of the parent.

For example, a House has one or more Rooms. That's a composition relationship. Delete the house and the rooms also cease to exist. A House also has a number of occupants, being instances of Person. That's an aggregation relationship because those people exist outside of the context of that house.

Delegation is nothing more than an implementation detail. A class has a public interface that describes its state and behaviour. How that is implemented is irrelevant. It could delegate to other objects or not.

You'll note that both A and B from your example have the same external interface. It's more common to do something like this:

// this represents an interface
class A {
public:
  virtual void printStructure() = 0;
}

with concrete classes:

class ConcreteA : A {
public:
  virtual void printStructure() { ... }
}

and

class DelegateA : A {
public:
  DelegateA(A& a) { this.a = a; }
  virtual void printStructure() { a.printStructure(); }
private:
  A a;
}

Excuse my probably C++ syntax errors. I'm a little rusty.

你与清晨阳光 2024-08-25 18:53:58

我发现有一些差异:

  • 授权涉及再导出方法;在组合关系中,内部对象方法只能私有使用,不能重新公开。
  • 组合通常意味着某种所有权语义,对对象生命周期有影响;父对象“拥有”子对象,而子对象没有太多理由单独存在。委托没有这种含义。

您显示的代码使用委托和关联;关联可能是组合,但如果没有更广泛的上下文或更多有关对象的信息,就很难区分(当关联变成组合时,它可能非常微妙和主观)。

There are a couple of differences I see:

  • Delegation involves re-exporting methods; in a composition relationship, the inner objects methods may be used only privately and not re-exposed.
  • Composition usually implies some kind of ownership semantics with implications for object lifecycle; the parent object "owns" the child and the child doesn't have much reason to exist on its own. Delegation does not have this implication.

The code you show uses delegation and association; the association may be composition, but it's difficult to tell without broader context or more information about the objects (it can be quite subtle and subjective when an association becomes a composition).

零崎曲识 2024-08-25 18:53:58

构图是关于对象之间的关系。

委派是将工作从一个对象传递到另一个对象。

这些实际上是不同的(但有时相关的)问题。

你得到的是由A组成的B(B指的是A)。 B 也将其一个方法委托给 A。

但是由于 B 对 A 的使用是私有的(完全封装在 B 的黑匣子内),所以我不会将 B 对 A 的使用称为“组合”。仅当类 A 可以从 B 访问时,我才会使用“组合”。这里重要的是 B 的逻辑模型是否“有一个”A。

在您的情况下,B 是根据 A 实现的。因为这是一个实现问题,所以可以被认为不是B的逻辑模型的一部分。也就是说,您可以明智地谈论 B,而不需要谈论或关心 A。

总而言之,这些东西实际上只对 PHB 和 UML 建模工具重要。或者如果您正在学习设计模式。我不会太沉迷于此。

[PHB =>尖头发的老板]

Composition is about the relationships between objects.

Delegation is about passing work from one object to another.

These are actually different (but sometimes related) concerns.

What you've got is B composed of A (B refers to A). B also delegates its one method to A.

But since B's use of A is private (fully encapsulated within B's black box), I wouldn't call B's use of A "composition". I would use "composition" only if class A could be accessed from B. What's important here is if B's logical model "has-a" A.

In your case, B is implemented in terms of A. Since this is an implementation concern it can be considered as not part of B's logical model. That is, you can talk intelligently about B without talking or caring about A.

That all said, this stuff is really only important to PHB's and UML modeling tools. Or perhaps if you are studying Design Patterns. I wouldn't get too hung up on it.

[PHB => Pointy Haired Boss]

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