简体   繁体   English

LinkedList中的空对象设计模式

[英]Null Object Design Pattern in LinkedList

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 + "} ";

        }
    }

No, your code doesn't implement Null Object Design Pattern . 不,您的代码没有实现Null Object Design Pattern Its essence is not to use null but to create an object which will represent the null . 它的本质不是使用null而是创建一个表示null的对象。

For example: 例如:

public static final NewLink NULL_NODE = new NewLink();

And then: 接着:

firstNode = NULL_NODE;
lastNode  = NULL_NODE;

We use Null object design pattern if we want to assign some default behaviors (or prevent some behaviors to happen as a default behavior) For example using Null object pattern we can replace this code : 如果要分配一些默认行为(或防止某些行为作为默认行为),则使用Null对象设计模式 。例如,使用Null对象模式,我们可以替换以下代码:

if(myObj!=null) 
myObj.DoSomething();
else
DoSomethingElse();

wtih this one: 与此:

myObj.DoSomething() //assuming that myObj can be a Null object (not a null reference) that has implemented DoSomethingElse when you ask it to DoSomething()

Thus a Null Object design pattern actually uses a default object reference (not a null reference ) 因此,Null Object设计模式实际上使用默认的对象引用(不是null引用)

public static final NewLink NULL_NODE = new NewLink(); 

must be in NewLink class 必须在NewLink类中

so 所以

firstNode = NewLink.NULL_NODE;
secondNode = NewLink.NULL_NODE;

also you can make all methods from NewLink - abstract 也可以从NewLink制作所有方法NewLink
and make two nested classes: for NULL objects and for not NULL object. 并创建两个嵌套类:NULL对象和非NULL对象。
It's can be very helpful in difficult situations 在困难的情况下可能会很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM