继承设计模式

发布于 2024-08-22 03:36:09 字数 321 浏览 4 评论 0原文

我正在编写一个程序,其中每个组件都有一个继承结构,该结构具有三个级别...ui、逻辑和数据...其中每个级别都有一个所有组件都必须实现的已定义功能的接口。每个级别还具有一些可以为整个界面通用编写的功能,而不是为每个组件重复编写。

在我看来,最好的方法是接口和组件实现之间的抽象类,它执行所有通用功能(如链接的 类图在这里)...但是C#的继承规则只允许我从未实现的接口获得多重继承。实现这种行为的最佳实践设计是什么?

I'm writing a program where each component has an inheritance structure has three levels... ui, logic, and data... where each of these levels has an interface of defined functionality that all components must implement. Each of these levels also has some functionality that could be written generically for the whole interface, rather than repeatedly for each component.

In my mind, the best approach would be an abstract class in between the interface and the component implementation that does all the generic functionality (as in the linked class diagram here)... but the inheritance rules for C# only let me have the multiple inheritance from unimplemented interfaces. What would be a best practices design to achieve this type of behavior?

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

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

发布评论

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

评论(2

浅浅 2024-08-29 03:36:09

为什么不将每个组件(UI、逻辑和数据)放在不同的类中,然后让 UI 使用逻辑类,然后让逻辑类使用数据类。

这样你就可以让每个类继承适当的泛型类。

请记住,您应该更喜欢组合而不是继承

Why not have each one of the components (UI, logic and data) in a different class and then have the UI using the logic class then have the logic class use the data class.

That way you could have each class inherit from the appropriate generic class.

Remember, you should prefer composition over inheritance

梦毁影碎の 2024-08-29 03:36:09

每个(抽象类和接口)都有自己的优点和缺点。虽然 Russell 的建议是正确的,组合优于继承,但使用模式建议针对接口进行编程,而不是实现(Head First 设计模式)。

抽象类提供了大量的灵活性,您可以在幕后实现方法,可以这么说,强制实现等。如果这是一个问题的话,两个接口都提供多态性。但抽象类需要一个基本继承槽。

Each (abstract class & interface) offers their own set of advantages and disadvantages. While Russell is correct in suggesting composition over inheritance, using patterns suggests program to an interface, not an implementation (Head First Design Patterns).

An abstract class offers tons of flexibility where you can implement methods under the covers so to speak, enforce implementation, etc. Both interfaces offer polymorphism if that is a concern. But abstract classes take a base inheritance slot.

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