从基本链表创建双向链表(DLL)-组合还是继承?

huangapple go评论70阅读模式
英文:

Creating doubly linked list(DLL) from a basic linked list-Composition or inheritance?

问题

以下是翻译好的内容:

假设我的基本链表(BLL)具有以下基本方法:

  • 在头部、尾部、任意位置插入
  • 在头部、尾部、任意位置删除

我想知道我应该使用组合还是继承来从BLL创建我的DLL。

我的思考过程

根据**《Effective Java 第3版》第18条:宁可使用组合,不要使用继承**的阅读,我猜想应该使用组合。

但是在这里,组合似乎不太合适,因为我不知道如何通过委托来结合指向前一个节点的指针。

然后我阅读了里氏替换原则(LSP),似乎继承是正确的选择?但我不确定。


问题(总结):
我应该使用组合还是继承来从BLL创建我的DLL?为什么?

英文:

Say my BasicLinkedList(BLL) has these following basic methods:

-insert at the head, tail, anywhere
-delete at the head, tail, anywhere

I want to know if I should use composition or inheritance to create my DLL from BLL.

My thought process:

My guess was composition from the read up of Effective Java Item #18(3rd Edition): Favour composition over inheritance.

But composition didn't feel right here as I didn't know how to incorporate the pointer to previous node with delegation.

Then I read up on Liskov substitution Principle(LSP) and seems that inheritance is the right choice? But I'm not sure.


Question(summarised):
Should I use composition or inheritance to create my DLL from BLL? Why?

答案1

得分: 1

"优先选择组合而非继承"并不意味着您永远不应该使用继承,以及继承总是不好的。在这种特定情况下,您最好使用继承来获取对必要的类变量和方法的访问。

英文:

"Favor composition over inheritance" does not mean you should never use Inheritance and that Inheritance is always bad. In this particular case, you are better off using inheritance to gain access to the necessary class variables and methods.

huangapple
  • 本文由 发表于 2020年8月15日 09:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63421783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定