简体   繁体   English

Python中的BaseBean反模式

[英]BaseBean anti-pattern in Python

Background: Coming from Perl world, I'm currently learning Python in my new job. 背景:来自Perl世界,我目前正在新工作中学习Python。 Apart from doing the work, I'm also trying to get more of "real OOP" and "pythonic" thinking into my brain cell flow, mostly by reading SO, Python docs and various articles. 除了完成这项工作外,我还试图通过进入SO,Python文档和各种文章,使更多的“真正的OOP”和“ pythonic”思想进入我的大脑细胞流。

Now in OO design section of Anti-pattern page, Wikipedia lists this BaseBean , which on its own page is described as: 现在,在“ 反模式”页面的“ OO设计”部分中,Wikipedia列出了该BaseBean ,在其自己的页面上将描述为:

In object-oriented programming, a BaseBean is a utility object from which concrete entities are derived (via subclassing). 在面向对象的编程中,BaseBean是一个实用程序对象,从中派生具体实体(通过子类化)。 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). BaseBean是反模式的示例(名称的“ Bean”部分来自通用实体对象或JavaBean的标准Java命名约定)。

Maybe it's because I have no experience with Java, maybe it's for other reason, but I'm having trouble wrapping my head around this explanation. 也许是因为我没有Java经验,也许是由于其他原因,但是我很难理解这个解释。 However, I'm feeling that few lines of code would be worth thousand words here. 但是,我觉得这里很少有几行代码值得一千个单词。

Could anyone help clarify this for me? 有人可以帮我澄清一下吗? Preferably using a simple example in Python? 最好在Python中使用一个简单的示例?

Based on this quote in the Wikipedia article: 根据Wikipedia文章中的引用:

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. 例如,订单不是矢量,尽管订单可能包含订单项的矢量。

it seems to be referring to things like this: 它似乎是指这样的事情:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 中的嵌套集和字典是反模式吗? - Are nested sets and dictionaries anti-pattern in Python? Python 装饰器采用附加参数反模式 - Python decorator taking additional argument anti-pattern 直接在 function 中更改 object 是 python 中的反模式? - Change object directly in function is anti-pattern in python? 带有上下文管理器的生成器是反模式吗? - Are generators with context managers an anti-pattern? Scripts目录是Python中的反模式吗? 如果是这样,进口的正确方法是什么? - Is a Scripts directory an anti-pattern in Python? If so, what's the right way to import? 文件夹名称遮蔽 3rd-party package 名称或 Python 模块名称是反模式吗? - Is folder name shadowing 3rd-party package name or Python module name is an anti-pattern? 在 `__enter__` 中返回除 `self` 以外的值是一种反模式吗? - Is returning a value other than `self` in `__enter__` an anti-pattern? 使用回调满足依赖性的缺点是什么? 而且,这是反模式吗? - What are the drawbacks to using callbacks for satisfying dependencies? And, is this an anti-pattern? 使用 Django ModelField 选择作为字符串的枚举 — 反模式? - Enum using Django ModelField choices as string — anti-pattern? 将请求对象发送到Django中的模型方法是否是反模式? - Is it an anti-pattern to send a request object to a model method in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM