简化复杂的类层次结构

发布于 2024-09-15 07:44:32 字数 698 浏览 4 评论 0原文

为了最大限度地提高代码重用率,我为我的 Python 代码创建了一个相当复杂的类层次结构。它是这样的:

class ElectionMethod
    class IterativeMethod
        class SNTV
        ...
    class NonIterativeMethod
        class OrderDependent
            class CambridgeSTV
            ...
        class OrderIndependent
            class WIGM
                class ERS97STV
                ...
            class Recursive
                class MeekSTV
                ...

只有类层次结构的叶子才会被实例化为对象。这确实达到了最大化代码重用的目的,但代码本身很复杂(某些类有 20 个左右的方法),并且考虑到许多级别,查找某些内容的实现位置可能会很耗时。

我尝试过提取功能块并将该功能放入单独的对象中,但我还没有找到实现此目的的好方法,因为几乎所有功能都依赖于通用数据。即,我可以将一些代码移动到不同的对象,但它需要引用大的复杂层次结构来获取它需要的数据,因此它并不是真正的封装。

有人对如何使其更易于管理有任何建议吗?

With the goal of maximizing code reuse, I have created a rather complex class hierarchy for my Python code. It goes something like this:

class ElectionMethod
    class IterativeMethod
        class SNTV
        ...
    class NonIterativeMethod
        class OrderDependent
            class CambridgeSTV
            ...
        class OrderIndependent
            class WIGM
                class ERS97STV
                ...
            class Recursive
                class MeekSTV
                ...

Only the leaves of the class hierarchy are ever instantiated into objects. This does serve its purpose of maximizing code reuse, but the code itself is complicated (some classes have 20 or so methods) and given the many levels it can be time consuming to find where something is implemented.

I've tried pulling out chunks of functionality and putting that functionality in a separate object, but I haven't found a good way to do this since nearly all the functionality depends on common data. I.e., I can move some code to a different object but it needs to refer back to the big complex hierarchy to get the data it needs so it isn't really encapsulation.

Does anyone have any advice as to how to make this more manageable?

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

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

发布评论

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

评论(3

拒绝两难 2024-09-22 07:44:32

多种方法可能是另一种有效的方法,具体取决于您的架构此类问题的解决方案。这个想法是使方法模块级函数可以作用于任何需要传递给它们的类实例。这是另一个链接,将它们作为多重继承的替代方案进行检查。它称它们为通用函数,但想法是相似的。它还讨论了 python 未记录的内置实现这一概念。

如果您可以将与设置或获取特定实例值无关的所有内容从类移至模块级函数,并且只让类级代码与实现状态暴露/修改接口有关,那么您可能不会甚至需要多种方法。

Multi-methods might, depending on your architecture, be another valid solution to this sort of problem. The idea is to make the methods module level functions that can act on any class instance which it would be desirable to pass to them. Here's a another link that examines them as an alternative to multiple inheritance. It call's them generic functions but the idea is similar. It also discusses python's un-documented built in implementation of the concept.

If you could move everything that doesn't have to do with setting or getting a particular instances values out of a class to module level functions and just have the class level code concerned with implementing a state-exposure/modification interface, then you might not even need multi-methods.

稳稳的幸福 2024-09-22 07:44:32

如果唯一的困难是找到实现某些内容的位置,也许您不需要重构。相反,寻找当前编辑器的扩展,或者切换到支持跳转到定义的编辑器。

例如,当光标放在对象名称上并且 < code>Ctrl-c g 被按下。

我确信如果你使用 vi/vim 也会有类似的技巧。

If the only difficulty is finding where something is implemented, perhaps you don't need to refactor. Instead, look for extensions to your current editor, or switch to one that supports jumping-to-definitions.

For example, emacs with ropemacs installed jumps to definitions when the the cursor is placed over an object name and Ctrl-c g is pressed.

I'm sure there are similar tricks if you use vi/vim.

浅紫色的梦幻 2024-09-22 07:44:32

这是经典问题,因此没有通用答案(否则它不是问题)。但有几种常用的解决方案。其中之一 - SOLID 设计原则。因此,您不必在层次结构的各个级别中搜索功能,您可以轻松地在适当的单一责任代码元素中找到或实现它。如果您需要访问一些公共数据,您应该创建负责访问这些数据的对象。

契约式设计,尤其是依赖注入技术,一般需要一些框架的支持,所以要寻找合适的平台

This is classic problem, so there isn't universal answer ( otherwise it wasn't a problem ). But has several commonly used solutions. One of them - SOLID principles in design. So you haven't to search functionality trough the levels of your hierarchy, you can easily find or implement it in appropriate single-responsible code element. And if you need to access to some common data, you should create objects responsible for access to this data.

Design by Contract and especially Dependency Injection technique generally needs for some framework support, so make search for suitable platform.

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