是否有一个识别的模式为无用的对象实现一个接口? [英] Is there a recognized pattern for useless objects that implement an interface?

查看:109
本文介绍了是否有一个识别的模式为无用的对象实现一个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,(虽然它不是接口).NET中的Stream类具有Stream.Null提供的实现,它只是丢弃数据。 PowerShell具有Out-Null。



在我开发的应用程序中,我经常发现使用默认实现NullFoo或类似的实现接口IFoo最好有一个无用的实现,而不是根本不传递一个对象。



我的问题是在文档化或解释架构时应该如何参考这种做法?是否有公认的名称或GoF / Fowler设计模式?

解决方案

此模式通常被称为NullObject:< a href =http://en.wikipedia.org/wiki/Null_Object_pattern =noreferrer> http://en.wikipedia.org/wiki/Null_Object_pattern


For example, (although it's not an interface) the Stream class in .NET has an implementation provided by Stream.Null which simply discards the data. PowerShell has Out-Null.

In applications I've developed, I've often found it useful to implement an interface IFoo with a default implementation NullFoo or similar when it is preferable to have a useless implementation rather than not passing an object at all.

My question is how should I refer to this practice in documenting or explaining an architecture? Is there a recognized name or GoF/Fowler design pattern for this?

解决方案

This pattern is often refered as "NullObject" : http://en.wikipedia.org/wiki/Null_Object_pattern

这篇关于是否有一个识别的模式为无用的对象实现一个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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