我应避免暴露懒< T>类公共API? [英] Should I avoid exposing the Lazy<T> class in public API?

查看:146
本文介绍了我应避免暴露懒< T>类公共API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在库的公共接口的设计,它是合法的,返回的 懒< T> ,如果​​我想延迟初始化?或者是更好的隐藏始终懒&LT的使用; T> 由封装或其他技术手段

In a design of the public interface of a library, is it legitimate to return an instance of Lazy<T>, if I want lazy initialization? Or is it better to always hide the usage of Lazy<T> by means of encapsulation or other techniques?

推荐答案

有关以下我assumim你的意思是一个懒惰的属性。

For the following I'm assumim you mean a Lazy Property.

这要看你的界面的目的。

It depends on the purpose of your interface.

它是一个重要的细节,消费者知道这是懒惰?或者是它只是一个技术细节,不应该改变的行为,为消费者。

Is it an important detail that the consumer knows that it is lazy ? Or is it just a technical detail which should not change the behavior for the consumer.

它,你只需要一个短暂的延迟,绝不能由消费者来处理然后我会倾向于害得懒惰和只公开牛逼直接。

It you only have a short delay which must not be handled by the consumer then i would tend to haide the Lazy and only expose T directly.

如果消费者应该意识到,可能适应这种行为,然后我会暴露懒惰。

If the consumer should be aware and may adapt this behavior then i would expose the lazy .

但thiking这个我会在大多数情况下,而暴露出这表明code可能有副作用或可能需要一段时间的方法。

But thiking on this I would in most cases rather expose a method which indicates that the code may have side effects or may take a while.

这篇关于我应避免暴露懒&LT; T&GT;类公共API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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