具有相同实现但基类不同的 2 个类的 C# 设计模式

发布于 2024-10-05 16:07:27 字数 179 浏览 1 评论 0原文

采用两个相似但最好不同的基类 A 和 B。每个都有一个子类(A'和B'),它们向各自的类添加相同的功能和成员。是否有一种设计模式可以让我不必在 A' 和 B' 中重复代码?

我看过 BridgeDecorator,但我不明白它们是如何工作的。

谢谢, 抢

Take two base classes A and B which are similar but are preferred to be distinct. Each has a sub class (A' and B') which add the same functionality and members to their respective classes. Is there a design pattern to allow me to not have to duplicate code in A' and B'?

I've looked at Bridge and Decorator, but I can't see how these would work.

Thanks,
Rob

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

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

发布评论

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

评论(5

巷雨优美回忆 2024-10-12 16:07:28

策略模式。在第三个类中创建公共代码,并在 A' 和 B' 中保存它的实例。

The strategy pattern. Create the common code in a third class, and hold an instance to it in A' and B'.

雨轻弹 2024-10-12 16:07:28

将相同的功能和成员添加到各自的类中。听起来很接近接口,尽管仍然有单独的实现。也许类似于 Provider 模式? http://msdn.microsoft.com/en-us/library/ms972319.aspx

add the same functionality and members to their respective classes. Sounds close to an interface, though that would still have separate implementations. Maybe something similar to the Provider pattern? http://msdn.microsoft.com/en-us/library/ms972319.aspx

八巷 2024-10-12 16:07:28

mixins 的概念很接近你想要的。然而,C# 本身并不支持这个概念。您可以使用接口和扩展方法来模仿 mixin 的概念,但它看起来不太漂亮。

查看此问题以获取更多信息: Is it possible to Implement mixins在 C# 中?

The concept of mixins comes close to that what you want. However this concept is not natively supported by C#. You can use interfaces and extension methods to mimic the concept of mixins, but it does not look very pretty.

Have a look on this question for further information: Is it possible to implement mixins in C#?

迷你仙 2024-10-12 16:07:27

您可以使用组合并将共享代码重构为另一个 C 类吗?

Could you use composition and refactor the shared code to another class C?

咆哮 2024-10-12 16:07:27

采用两个基类 A 和 B,其中
相似但更倾向于
清楚的。每个都有一个子类(A' 和
B') 添加相同的功能
以及各自的成员
类。

这是一个有点奇怪的起点。如果是我,我会考虑改变这部分设计。

假设这不合理,无论出于何种原因,我认为装饰器是避免代码重复的最佳选择。

您将需要创建子类 A' 和 A' 。 B'。您必须在每个中声明添加的方法和属性。这些成员应该委托给可以实现通用功能的另一个类的成员。该类的实例将包含在 A' 和 A' 中。 B'。

如果这些公共成员的实现很重要,这实际上只会节省您的重复工作。

Take two base classes A and B which
are similar but are preferred to be
distinct. Each has a sub class (A' and
B') which add the same functionality
and members to their respective
classes.

This is a bit of an odd place to start from. If it were me, I'd be looking to change this part of the design.

Assuming that's not reasonable, for whatever reason, I think Decorator is about the best you can do to avoid code duplication.

You will need to create the subclasses, A' & B'. And you'll have to declare the added methods and properties in each. Those members should delegate to members of another class that can implement the common functionality. An instance of that class will be contained in both A' & B'.

This really only saves you duplication if the implementation of those common members is significant.

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