Python中的BaseBean反模式

2024-09-30 22:12:41 发布

您现在位置:Python中文网/ 问答频道 /正文

背景:来自Perl世界,我目前正在学习Python 工作。除了做这项工作,我还试着获得更多的“真正的OOP” 而“Python”思维进入我的脑细胞流中,大多是通过阅读所以,Python 文件和各种文章。在

现在在Anti-pattern页面的OO设计部分,Wikipedia列出了这一点 BaseBean,在its own page上描述为:

In object-oriented programming, a BaseBean is a utility object from which concrete entities are derived (via subclassing). Proper design suggests that the inherited functionality should be provided via delegation instead. The BaseBean is an example of an anti-pattern (where the "Bean" part of the name comes from the standard Java naming convention for a generic entity object, or JavaBean).

可能是因为我没有Java的经验,也可能是其他原因, 但我很难理解这个解释。但是,我 感觉几行代码就值几千字。在

有人能帮我澄清一下吗?最好使用Python中的一个简单示例?在


Tags: ofthefromanobjectis世界java
1条回答
网友
1楼 · 发布于 2024-09-30 22:12:41

根据维基百科文章中的这句话:

Using inheritance causes the derived class to rely on the internals of a base class which may be out of the control of the developer. While the utility classes are typically stable and fairly similar across implementations, some innocuous change in the future could break the derived class (since the relationship is not well defined). In addition, it muddies the business meaning of the derived class. For example, an order is not a vector, although an order may contain a vector of line items.

它似乎是指这样的东西:

class Order(dict):
    def __repr__(self):
        return """
Customer: {customer}
Address: {address}
        """.format(**self)

o = Order(customer = "Joe Customer", address = "123 Market St")

相关问题 更多 >