Skip to main content
 首页 » 编程设计

python之Compositions 和 Facade 在 python 中是一回事吗

2024年11月01日9dudu

  1. 在 python 上下文中,组合和外观是一回事。我知道外观设计模式的用途,但只是想确定我能否将实现外观的类称为组合?

  2. 我可以对 mixin 类说同样的话,还是说它只是一个不同的野兽,更接近于多重继承并解决了 facade 以外的其他问题?

  3. 我们能否粗略地将 Python 中的组合等同于 Java 中的接口(interface),或者这种说法完全不合适。

PS:我希望我的应用程序为客户提供一个标准接口(interface),但随着我们获得更多的开发带宽,确切的实现会随着时间的推移不断变化。由于客户将使用此类进行进一步开发,因此我不希望他们的代码中断,以防我们在实现方面进行任何更改。我想在开始项目之前解决我的行话困惑。

请您参考如下方法:

如上所述:

Composition is when one class contains members that are instances of other classes. The term Facade usually means that you are encapsulating a lot of logic behind the public members of a class. So notice that you can have composition without a Facade, if the composition members are all public. I won't get into the merit of a Facade without composition being valid, because that's a semantics labyrinth. BTW composition and interfaces are two different beasts, even if consider only Java or only Python.