当我们需要向 List<> 添加行为时,使用组合相对于继承有什么优势?

发布于 2024-08-28 09:13:43 字数 351 浏览 6 评论 0 原文

在此示例中,选项 2 相对于选项 1 的优点/缺点是什么?

选项 1(继承):

public class SalesList : List<Sales>
{
    //methods that add extra behavior List<Sales>
}

选项 2(组合):

public class SalesList 
{
    private List<Sales> _list;
    //methods that add extra behavior to List<Sales>
}

What are the advantages/disadvantages of Option 2 over 1 in this example?

Option 1 (Inheritance):

public class SalesList : List<Sales>
{
    //methods that add extra behavior List<Sales>
}

Option 2 (Composition):

public class SalesList 
{
    private List<Sales> _list;
    //methods that add extra behavior to List<Sales>
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

合约呢 2024-09-04 09:13:44

选项 2 的优点是敏捷性。使用组合,您可以选择公开列表界面的哪些部分。

想象一下,稍后您决定不再从另一个班级扩展。现在,您的库的一些用户已经使用了List提供的方法,因此您需要自己实现所有列表方法:add/addAll/contains/retainAll。对于一个简单的销售列表来说,有很多方法可以实现。

基本上,这可以归结为约书亚·布洛赫 (Joshua Bloch) 在 保险杠贴纸 API 设计。 API 的每个方面都应该尽可能小,但不能更小。您以后可以随时添加内容,但无法删除它们。

The advantage of option 2 is agility. Using composition, you can choose which parts of the list interface you expose.

Imagine that later on you decide no longer to extend from another class. By now, some of the users of your library will have used methods provided by List, so you need to implement all of the list methods yourself: add/addAll/contains/retainAll. That's a whole lot of methods to implement for a simple sales list.

Basically, this comes down to "when in doubt, leave it out" which Joshua Bloch talks about in Bumper-Sticker API Design. Every facet of an API should be as small as possible, but no smaller. You can always add things later, but you can't take them away.

清风疏影 2024-09-04 09:13:44

组合的主要缺点是,如果需要呈现相同的接口,则需要包装(复制)私有列表的所有公共方法,在继承中,所有这些方法都已经可用,但不能覆盖它们中的任何一个这是不可重写的(在 C# 中,这意味着该方法应标记为“虚拟”,在 Java 中,它不能标记为“最终”)。

在 C# 3 及更高版本中,您可以使用扩展方法,它提供了继承的外观,而不会真正弄乱层次结构树。

The main disadvantage of Composition is that you need to wrap (duplicate) all the public methods of the private List if you need to present the same interface, in Inheritance you have all of them already available, but you can't override any of them that was made not overridable (in C# it means the method should be marked 'virtual', in Java it can't be marked as 'final').

In C# 3 and up, you can use Extension Methods, which give the appearance of inheritance without truly messing up the hierarchy tree.

提笔书几行 2024-09-04 09:13:44

继承允许我们形成类的层次结构,这样父类是一个**集,其所有子类都是**集,这允许泛化、特化和应用替换原则

Inheritance allows us to form a hierarchy of classes,such that parent is a *super*set and all its child's are *sub*set,which allows generalization,Specialization and to apply substitution principle

风为裳 2024-09-04 09:13:44

选项 1
- 优点——继承&所有优点重复使用
- 缺点 - 代码现在向消费者发出信号,表明它是从 List 派生的。如果以后需要改变,那么所有相关消费者都将受到影响。

选项 2
- 优点 - 有关销售数据存储的实现细节从消费者那里抽象出来。因此,如果实现需要更改(例如字典),该类的使用者将不受这些更改的影响。
- 缺点 - SalesList 类现在需要公开其他方法来获取和/或设置内部 _list 对象。这是需要维护的额外代码。另外,如果内部实现发生变化,您需要注意仍然支持以前的行为......

只是想到的一些想法。

HTH。

Option 1
- Advantages - all advantages of inheritance & re-use
- Disadvantages - the code is now signalling to the consumers that it is dervied from List. If this needs to change at a later date, then all the related consumers will be affected.

Option 2
- Advantages - the implementation detail about the storage of the sales data is abstracted aways from the consumer. So, if the implementation needs to change (say a dictionary, for example), the consumer of the class will be immune from these changes.
- Disadvantages - the SalesList class will now need to expose additional methods to get and/or set to the inner _list object. this is additional code which need to be maintained. Also, if the inner implementation changes, you need to take care to still support the previous behaviour...

Just a few thoughts that come to mind.

HTH.

决绝 2024-09-04 09:13:44

在组合中,您可以在运行时动态更改超类实现。
但是使用继承你不能在运行时改变超类的实现。

在组合中,它取决于您传递给 setter 的对象类型,并且它的行为将基于此。这将为实施提供更大的灵活性

In composition you can change the super class implementation dynamically at run time.
But using the inheritance you can not change the super class implementation at run time.

In composition it depends on what type of object you are passing in to the setter and it will behave based on that. It will give more flexibility on implementation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文