一般继承行为/需要最佳实践

发布于 2024-10-03 17:44:02 字数 1431 浏览 1 评论 0原文

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

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

发布评论

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

评论(2

<逆流佳人身旁 2024-10-10 17:44:02

不知道以前是否已经描述过,但我会这样设计:

首先,为每个类提供一个“属性集”。在 C++ 中,这可能是一个映射,将标识(字符串、数字、GUID...)映射到值。

然后,为您的类提供一个像这样工作的 getColor 方法(这是不完整的代码,但应该足以让您开始):

PropertySet::const_iterator it = m_propertySet.find("Color");
// property found, return it
if (it!=m_propertySet.end()) return it->second;
// if property is not found, ask our parent
if (m_parent) m_parent->getColor();
// if no parent, return black
return BLACK;

虽然这有效,但这种方法存在一些陷阱:

  • 在属性集中查找值比在属性集中查找值慢得多仅使用字符串作为属性集中的键返回数据成员的
  • 速度很慢,但可以更轻松地区分您的属性,
  • 使用整数作为属性集中的键要快得多,但会使区分您的属性变得更加
  • 困难set 总是比仅仅存储数据成员占用更多的内存,这可能只有在你有一个很深的层次结构时才可行,其中有很多属性,其中只有几个 set

Don't know if this has been described before but I would design it like this:

First, give each of your classes a 'property set'. In C++ this could be a map, mapping an identification (string, number, GUID, ...) to a value.

Then, give your classes a getColor method that works like this (this is incomplete code but should be enough to get you started):

PropertySet::const_iterator it = m_propertySet.find("Color");
// property found, return it
if (it!=m_propertySet.end()) return it->second;
// if property is not found, ask our parent
if (m_parent) m_parent->getColor();
// if no parent, return black
return BLACK;

Although this works, there are some pitfalls in this approach:

  • looking up values in a property set is much slower than just returning a data member
  • using a string as key in the property set is slow, but makes it easier to distinguish your properties
  • using an integer as key in the property set is much faster, but can make it more difficult to distinguish your properties
  • the property set will always take up more memory than just storing a data member, which probably only makes it feasible if you have a deep hierarchy, with many many properties of which there are only a few set
迷你仙 2024-10-10 17:44:02

我认为这是委托设计模式的一个例子。

这种行为的实现取决于您所使用的语言。基于原型的语言,如 Javascript 或 Self,本身就使用这种机制。这意味着您可以在两个对象之间建立父子关系,并且子对象立即继承所有父对象属性。当您更改父对象中的属性值时,您也会在其子对象中看到新值。如果更改子对象中的属性,新值将存储在子对象中,而其父对象不受影响。

如果您使用 Java 或 C++/C# 等语言,您通常会模仿这种行为,在对象(子对象)的成员变量中保存对另一个对象(其父对象)的引用,并将必要的逻辑放入子对象的 getter 中以查找属性值存储在对象本身中(在成员变量或映射中),或者如果子级无法自行解析属性,则将责任委托给其父级。通常子级和父级都实现一个通用接口,但我认为并不总是需要它。

I think this is a case of the Delegation Design Pattern.

Implementation of this kind of behavior depends on the language you are using. Prototype based languages, like Javascript or Self, use this mechanism natively. It means that you can establish a parent-child relation between two objects and immediately the child inherits all parent properties. When you change the value of a property in the parent object, you will see the new value in its children too. If you change a property in a child object, the new value is stored in the child and its parent is not affected.

If you use a language like Java or C++/C# you usually mimic this behavior holding in a member variable of an object (the child) a reference to another (its parent) and put in child's getters the necesary logic to look for a property value stored in the object itself (in a member variable or map), or delegate responsibility to its parent if child can't resolve the property by itself. Usually both child and parent implement a common interface, but I supose it's not always required.

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