在不破坏遗留代码的情况下将类超级设置为子集

发布于 2024-10-07 02:26:06 字数 230 浏览 5 评论 0原文

看到我对 OOP 真的很陌生,如果我知道得更好的话,这本来是可以避免的。 我开始使用我的类库大约一年,并创建了一个类,我们称之为树 到目前为止,Tree 类一直运行良好,它正是我所需要的。

好吧,现在我注意到我有不同类型的树,

我想将树的一些函数移动到它自己的命名空间并创建新的函数 对于这些其他树,如何在不破坏已经实现当前树的系统的情况下做到这一点?

有什么方法可以将呼叫从现有的“转发”到新的

Seeing i am really new to OOP, This could have been avoided have i known better.
I started about a year with my Class Library and made a Class Lets call it Tree
and up until now the Tree class has been working fine , it does exactly what i need.

Well, now i've noticed that i have different types of Tree's

I would like to move some functions of tree to its own Namespace and create new ones
for these other trees , How do this without breaking systems that already implements the current Tree?

Is there some way i can "Forward" calls from the existing to the new

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

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

发布评论

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

评论(1

感受沵的脚步 2024-10-14 02:26:06

根据树的新类型,您可能拥有一个 SuperTree,其中包含一个 Tree 实例来执行其某些功能,但提供 SuperTree 独有的附加功能。您可以通过为希望由内部 Tree 实例直接处理的函数编写包装方法来执行调用“转发”。

在某些情况下,SuperTree 从 Tree 继承下来也可能有意义,但前提是 Tree 在设计时就考虑到了这种类型的可扩展性,并且您可能希望传递 SuperTree 来代替 Tree。

可以这样想:

  • 建筑物可能包含钢框架并依赖于它,但它不是钢框架的一种类型。

  • 酒店是一种建筑物,因此具有建筑物所具有的东西,例如门和窗。

如果您的树更像是较大系统(建筑物的框架)的组件,那么您可以通过将其组合为新类的私有成员来扩展其功能。如果它本身是一个丰富的实体,并且具有明确定义的扩展点,那么您可以从 Tree 派生出新的类,这些类只需扩展其现有行为。

Depending on the new types of Trees, you might have a SuperTree that contains a Tree instance to perform some of its functions, but provides additional functionality that is unique to the SuperTree. You do the call 'forwarding' by writing wrapper methods for the functions that you want directly handled by the internal Tree instance.

In some cases, it might also make sense for SuperTree to descend from Tree, but only if Tree was designed with this type of extensibility in mind and you might want to pass a SuperTree in place of a Tree.

Think of it like this:

  • A building may contain a steel frame and relies upon it, but it is not a type of steel frame.

  • A hotel is a kind of building and therefore has the things that buildings have, like doors and windows.

If your Tree is more like a component of a larger system (the framing of a building), then you expand on its functionality by composing it as a private member of a new class. If it's a rich entity of its own, with well-defined extensibility points, you can descend new classes from Tree that simply expand on its existing behavior.

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