I am trying to Implement a doubly linked with null objects at the beginning and end of the list using null object design pattern. So an empty list will contain two null objects. So I wrote this code Does this follow null object design pattern? If not how can I achieve that. ANy suggestions will be appreciated.
Updated Code-
// Creating a doubly linked list.
doubleLinkedList = new DoubleLinkedList();
class DoubleLinkedList {
private NewLink firstNode;
private NewLink lastNode;
private NewLink rootNode;
public DoubleLinkedList() {
//So this satisfies my question that I have asked meaning null objects at beginning and last node or something else I have to do.
firstNode = NewLink.NULL_NODE;
lastNode = NewLink.NULL_NODE;
}
}
class NewLink {
public String data;
public NewLink nextPointer;
public NewLink previousPointer;
public static final NewLink NULL_NODE = new NewLink();
public NewLink(String id) {
data = id;
}
public NewLink() {
}
// Overriding toString method to return the actual data of the node
public String toString() {
return "{" + data + "} ";
}
}
must be in
NewLinkclassso
also you can make all methods from
NewLink– abstractand make two nested classes: for NULL objects and for not NULL object.
It’s can be very helpful in difficult situations