我应该实例化集合还是从集合继承? [英] Should I instantiate a collection or inherit from collection?

查看:105
本文介绍了我应该实例化集合还是从集合继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建类(尤其是涉及集合的类)时,我已经多次问过自己这个问题,但是我从来没有想出令人满意的答案.这是一个面向对象的设计问题.

I've asked myself this question a number of times when creating classes, particularly those involving collections, but I've never come up with a satisfactory answer. It's a OOP design question.

例如,在支票簿注册程序中,说我有一个BankAccount类. BankAccounts包含涉及帐户名称,帐户类型(检查,保存,...的枚举)和其他数据的数据,但最重要的是帐户中Adjustment(存款或取款)的集合

For example, in a checkbook register program say I have a class of BankAccount. BankAccounts contain data involving the Name of the account, the Type of account (enum of Checking, Saving,...), and other data, but most importantly is a collection of the Adjustments (deposits or withdrawals) in the account.

在这里,我有两种选择来保存调整项:

Here, I have two options for keeping a collection of the Adjustments:

  1. 实例化一个集合,并将其保留为BankAccount类中的成员.这就像说"BankAccount 具有一组调整项."
  2. 从集合继承.这就像说"BankAccount 调整项的集合".
  1. Instantiate a collection and keep it as a member within the BankAccount class. This is like saying "BankAccount has a collection of Adjustments."
  2. Inherit from collection. This is like saying "BankAccount is a collection of Adjustments."

我认为这两种解决方案都是直观的,当然,它们都有一些优点和缺点.例如,实例化允许该类(在仅允许单个基类的语言中)从另一个类继承,而从collection继承可轻松控制Add,Remove和其他方法,而不必编写替代方法来包装"那些.

I think both solutions are intuitive, and, of course, both offer some advantages and disadvantages. For example, instantiating allows the class (in languages that allow only a single base class) to inherit from another class, while inheriting from collection makes it easy to control the Add, Remove, and other methods without having to write surrogate methods to 'wrap' those.

那么,在这种情况下,哪种方法更好?

So, in such situations, which is a better approach?

推荐答案

对我来说,银行帐户具有一系列调整.银行帐户不是调整集合,因为它不仅"具有以下功能:它还具有"名称和类型,等等.

To me, a bank account has a collection of adjustments. A bank account is not a collection of adjustments, because it "is" much more than that: it also "is" a name and a type, etc.

因此,就您的情况(和类似情况)而言,我建议您在班级内汇总一个集合.

So, in your case (and similar cases), I suggest you aggregate a collection inside your class.

如有疑问,请避免继承. :-)

In case of doubt, avoid inheritance. :-)

我可以进一步论证.为了正确使用继承,子类必须满足 Liskov的替代原则;这意味着,在您的情况下,BankAccount应该是有效的类型,而在任何需要Collection的地方.我认为情况并非如此,因为Collection可能会公开诸如Add()Remove()之类的方法,而您将希望对添加和删除银行帐户中的调整施加某种控制权,而不是让人们添加和删除免费删除它们.

I can argument this further. In order to use inheritance properly, the subclass must satisfy Liskov's substitution principle; this means that, in your case, BankAccount should be a valid type anywhere a Collection is expected. I don't think that's the case, because a Collection probably exposes methods such as Add() and Remove(), whereas you will want to exert some control over adding and removing adjustments from your bank account rather than letting people add and remove them freely.

这篇关于我应该实例化集合还是从集合继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆