Currently as an exercise to practicing SOLID principles and basic data structures, I am trying to implement linked list type structures with as much code reuse as possible. Currently, I have:
package structures.linked; public class SingleNode<T> { private T data; private SingleNode<T> next; public SingleNode(T data, SingleNode<T> next) { this.data = data; this.next = next; } public T getData() { return data; } public void setData(T data) { this.data = data; } public SingleNode<T> getNext() { return next; } public void setNext(SingleNode<T> next) { this.next = next; } }
and...
package structures.linked; public class DoubleNode<T> extends SingleNode<T> { private DoubleNode<T> prev; public DoubleNode(T data, DoubleNode<T> next, DoubleNode<T> prev) { super(data, next); this.prev = prev; } public DoubleNode<T> getPrev() { return prev; } public void setPrev(DoubleNode<T> prev) { this.prev = prev; } public DoubleNode<T> getNext() { return (DoubleNode<T>) super.getNext(); } public void setNext(DoubleNode<T> next) { super.setNext(next); } }
It seems to me that getNext()
inside of DoubleNode<T>
is a violation of Liskov's substitution principle. Is this the case? Is there a better way to implement this while still reusing the code in SingleNode<T>
and without breaking SOLID principles?