Favor object composition over class inheritance.

The two most common techniques for reusing functionality in object-oriented systems are class inheritance
and object composition. As we've explained, class inheritance lets you define the implementation of one
class in terms of another's. Reuse by subclassing is often referred to as white-box reuse. The term "whitebox"
refers to visibility: With inheritance, the internals of parent classes are often visible to subclasses.
Object composition is an alternative to class inheritance. Here, new functionality is obtained by assembling
or composing objects to get more complex functionality. Object composition requires that the objects being
composed have well-defined interfaces. This style of reuse is called black-box reuse, because no internal
details of objects are visible. Objects appear only as "black boxes."
Inheritance and composition each have their advantages and disadvantages. Class inheritance is defined
statically at compile-time and is straightforward to use, since it's supported directly by the programming
language. Class inheritance also makes it easier to modify the implementation being reused. When a
subclass overrides some but not all operations, it can affect the operations it inherits as well, assuming they
call the overridden operations.
But class inheritance has some disadvantages, too. First, you can't change the implementations inherited
from parent classes at run-time, because inheritance is defined at compile-time. Second, and generally
worse, parent classes often define at least part of their subclasses' physical representation. Because
inheritance exposes a subclass to details of its parent's implementation, it's often said that "inheritance
breaks encapsulation" [Sny86]. The implementation of a subclass becomes so bound up with the
implementation of its parent class that any change in the parent's implementation will force the subclass to
change.
Implementation dependencies can cause problems when you're trying to reuse a subclass. Should any aspect
of the inherited implementation not be appropriate for new problem domains, the parent class must be
rewritten or replaced by something more appropriate. This dependency limits flexibility and ultimately
reusability. One cure for this is to inherit only from abstract classes, since they usually provide little or no
implementation.
Object composition is defined dynamically at run-time through objects acquiring references to other objects.
Composition requires objects to respect each others' interfaces, which in turn requires carefully designed
interfaces that don't stop you from using one object with many others. But there is a payoff. Because objects
are accessed solely through their interfaces, we don't break encapsulation. Any object can be replaced at runtime
by another as long as it has the same type. Moreover, because an object's implementation will be
written in terms of object interfaces, there are substantially fewer implementation dependencies.
Object composition has another effect on system design. Favoring object composition over class inheritance
helps you keep each class encapsulated and focused on one task. Your classes and class hierarchies will
remain small and will be less likely to grow into unmanageable monsters. On the other hand, a design based
on object composition will have more objects (if fewer classes), and the system's behavior will depend on
their interrelationships instead of being defined in one class.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在新版本的 statsmodels 库中,已经弃用了 statsmodels.tsa.arima_model.arma 和 statsmodels.tsa.arima_model.arima,推荐使用 statsmodels.tsa.arima.model.arima(注意 arima 和 model 之间的句点)和 statsmodels.tsa.sarimax。 ### 回答2: statsmodels.tsa.arima_model.arma 和 statsmodels.tsa.arima_model.arima 已经被移除,取而代之的是 statsmodels.tsa.arima.model.arima 和 statsmodels.tsa.sarimax。 这个改动涉及到时间序列分析建模的两个主要类,ARMA 和 ARIMA。在 statsmodels 的早期版本中,这两个类是使用 tsa(time series analysis)模块下的 arima_model 子模块来实现的。 但是,随着 statsmodels 的不断发展和更新,这些类已经被迁移至新的位置。在更新的 statsmodels 版本中,ARIMA 类被移至 tsa 模块下的 model 子模块中,ARMA 类被移至 tsa 模块下的 sarimax 子模块中。 这个变更的主要原因是为了提高模块的可读性,使模块的结构更加清晰和易于使用。新模块的名称更加直观,能够更好地反映自己的功能。在新模块中使用这些类时,只需要在类名中指定子模块(如 arima.model 或 sarimax),即可引用这些类。这样做既方便了用户,也能够使使用者更好地理解模块的结构和组成。同时,这个变更也反映了 statsmodels 的一贯风格,即通过持续不断的优化和创新来提升模块的性能和可用性,以满足更广泛的用户需求。 ### 回答3: statsmodels是一个Python库,提供了各种统计模型和数据分析工具。其中,tsa(时间序列分析)模块是statsmodels的一个重要组成部分。 在tsa模块中,ARMA和ARIMA是两个常用的时间序列分析模型,它们可以用于预测未来的时间序列数据。对于ARMA和ARIMA模型,过去的版本中提供了arma和arima函数进行拟合和预测。 然而,在新版本的statsmodels中,为了更好地进行统计和编程,直接使用arma和arima函数已经被弃用。相反,新的ARIMA函数位于statsmodels.tsa.arima.model子模块中,并且需要在调用函数时添加一个model的点。也就是说,新的调用方式应该是statsmodels.tsa.arima.model.ARIMA()。 另外,SARIMAX是一个新的时间序列模型,它提供了一些ARIMA模型不具备的功能,例如对外生变量的处理和季节性分析等。因此,对于SARIMAX模型,我们需要使用statsmodels.tsa.sarimax中的相应函数进行拟合和预测。 总之,虽然旧版本的arma和arima函数已经被弃用,但是在新版本的statsmodels中,我们可以使用更加复杂和灵活的ARIMA和SARIMAX模型进行时间序列分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值