使用设计模式实现双链表 [英] implement doubly linked list using design patterns

查看:103
本文介绍了使用设计模式实现双链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在双向链接列表中实现空对象模式,以使列表中的第一个元素和最后一个元素始终为空?

how to implement null object pattern in doubly linked list such that the 1st and last element in the list will always be null?

推荐答案

我不确定对象设计模式是否为空( http://en.wikipedia. org/wiki/Design_pattern [ ^ ])是链表的完美解决方案.对我来说,这似乎太过隆重的典礼"了.常规Java null非常适合用来表示列表的第一个和最后一个元素的概念.

另请参阅以下有趣的讨论: http://stackoverflow.com/questions/2707322/what-is- null-in-java [^ ].

另请参见著名作者 M的类似模式.福勒 :
http://martinfowler.com/eaaCatalog/specialCase.html [ http://en.wikipedia.org/wiki/Null_Object_pattern#C.23 [ ^ ].

请参阅上面引用的文章中引用的其他文章;他们很有趣.

模式非常冗余的原因是,它既没有提供额外的功能,也没有为链表提供额外的安全性.您有列表的最后一个元素和第一个元素引用null(在传统方法中)或一个特殊类型的特殊非元素"对象(在使用null对象设计模式的情况下).

有更好的吗?我认为对于此应用程序来说,这不会更好.模式的好处是基于公共接口(基类的接口类型)的特殊对象的特殊(琐碎)行为.最后/第一个元素有什么特别之处?

您可以使用静态对象(将提供线程相关的额外问题)或更好的单例模式(http://en.wikipedia.org/wiki/Singleton_pattern [
现在,如果使用这种模式的原因是某种学校的任务,我必须注意:您作为软件开发人员的主要技能也是最困难的技能之一是抵制各种白痴提出的要求",只是因为有人认为通过某种开发方法权威"使用某种设计模式技术是酷"还是时髦"或鼓励".别开玩笑了,我们面临到处都是这样的情况.因此,开发人员需要一项艰难的技能来反对它,以采用周全的解决方案.想一想.

—SA
I''m not sure that a null object design pattern (http://en.wikipedia.org/wiki/Design_pattern[^]) is the perfect solution for a linked list. To me, it looks like too much of "high ceremony"; regular Java null is perfectly good to be used to express the notion of first and last elements of the list.

See also this interesting discussion: http://stackoverflow.com/questions/2707322/what-is-null-in-java[^].

See also a similar pattern by the famous author M. Fowler:
http://martinfowler.com/eaaCatalog/specialCase.html[^].

If you need to understand how the pattern is supposed to work, you can use this description written for C#, as for Java it would look almost identical: http://en.wikipedia.org/wiki/Null_Object_pattern#C.23[^].

Please see the other articles referenced from the article referenced above; they are pretty interesting.

The reason for the pattern to be pretty redundant is that is does not provide neither extra functionality not additional safety to the linked list. You have last and first elements of the list referencing null (in traditional approach) or a special "not an element" object of a special type (in the approach of the null object design pattern).

Is it any better? I don''t think it can be better for this very application. The benefit of the pattern is special (trivial) behavior of the special object based on the common interface (interface type of base class). And what is special about last/first elements?

You can have the only one instance of this type, using a static object (which would provide and extra issue related to threading) or, better, the singleton pattern (http://en.wikipedia.org/wiki/Singleton_pattern[^]). What should be the behavior of this special object? If should have "next" and "previous" functionality anyway, and it cannot reference the "real" null, because it would defeat the purpose of the pattern. So, the instance of this type could reference itself. This is highly redundant and would not help the user, say, perform the iterations in a safety manner: the user, instead of tradition check for the null, will need to catch the reference to "the same thing". The failure to do the proper check is more dangerous than a failure to check for null. The failure to check the null would cause dereferencing of null and immediate exception which is very easy to debug. In case of self-referencing object the danger is getting into an infinite loop, which is much harder to detect.

Now, if the reason of using such pattern is some kind of school assignment, I must note: one of your major and most difficult skills as a software developer is resisting the "requirements" created by all kinds of idiots, just because someone think that using some technique of design pattern is "cool" or "trendy" or "encouraged" by some kind of development method "authorities". No kidding, we face such situations happen here and there; so developers need a difficult skill to oppose it in favor of well-though solutions. Just think about it.

—SA


这篇关于使用设计模式实现双链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆