Google Guava供应商示例 [英] Google Guava Supplier Example

查看:92
本文介绍了Google Guava供应商示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请通过适当的示例说明界面Supplier(在Guava中)的使用.

Please explain the use of the interface Supplier(in Guava) with a suitable example .

推荐答案

Supplier接口只是对无参数函数的抽象,该函数返回一个值...这是获取某些实例的方法.或对象的实例.由于它是如此通用,因此它可以用于许多事物. Jared解释了Multimaps工厂如何将其用作工厂来为值创建某种类型的Collection的新实例.

The Supplier interface is simply an abstraction of a no-arg function that returns a value... it is a means of getting some instance or instances of an object. Since it is so general, it can be used as many things. Jared explained how the Multimaps factories utilize it as a factory for creating a new instance of a Collection of some type for values.

鉴于界面的简单性,它还通过将Supplier的行为包装在另一个以某种方式改变其行为的Supplier中,对行为进行了非常有力的修饰.记忆化就是一个例子.我自己使用Suppliers.memoizeWithExpiration方法作为一种简便的方法,因此在给定的时间段内,某些数据最多只能从服务器读取一次.

Given the simplicity of the interface, it also allows for some very powerful decoration of a Supplier's behavior by wrapping it in another Supplier that alters its behavior somehow. Memoization is one example of that. I've used the Suppliers.memoizeWithExpiration method myself as an easy way to make it so some data will only be read from a server at most once in a given period of time.

我还建议您看一下Guice以及在其中如何使用Provider界面. Provider完全等同于Supplier,并且对于Guice的工作方式至关重要.

I'd also recommend taking a look at Guice and how the Provider interface is used in it. Provider is exactly equivalent to Supplier and is central to how Guice works.

  • Provider允许用户定义一种创建给定类的新对象的自定义方式.用户可以编写get()方法,该方法可以执行创建新对象所需的任何代码,因此,他们不仅限于让Guice仅使用构造函数来创建对象.在这里,他们使用它为对象的新实例定义自定义工厂.
  • Guice允许注入任何依赖项的Provider.这可能会在每次调用get()时返回一个新实例,或者可能总是返回一个实例或两者之间的任何值,具体取决于Provider表示的绑定的范围.这也允许依赖项的延迟实例化" ... Provider为类提供了创建对象的手段,而无需提前实际创建对象.直到何时以及是否调用get()时,才需要创建该对象的实例.
  • 如上所述,Provider是Guice范围界定的基础.如果您查看范围接口,您会注意到它的单个方法Provider<T> scope(Key<T> key, Provider<T> unscoped)是根据Provider定义的.此方法采用可以创建对象新实例的东西(Provider<T> unscoped),并根据适用范围定义的任何策略的对象返回Provider<T>,并可能返回该对象的某些缓存实例而不是创建一个新的.默认的NO_SCOPE范围仅通过unscoped提供程序传递,这意味着每次都会创建一个新实例. SINGLETON范围将第一次调用unscoped.get()的结果缓存起来,然后返回该单个实例,确保依赖于单例作用域对象的所有内容都获得对该单个对象的引用.请注意,SINGLETON范围的scope方法返回的ProviderSuppliers.memoize返回的Supplier本质上是相同的(尽管有点复杂).
  • Provider allows users to define a custom way of creating new objects of a given class. Users can write a get() method which can execute whatever code is needed to create a new object, so they aren't limited to having Guice use constructors alone to create objects. Here, they are using it to define a custom factory for new instance of an object.
  • Guice allows injection of a Provider of any dependency. This may return a new instance every time get() is called or it may always return a single instance or anything in between, depending on how the binding the Provider represents is scoped. This also allows for "lazy instantiation" of dependencies... the Provider gives a class a means of creating an object without needing to actually create the object ahead of time. An instance of the object does not need to be created until when, and if, get() is called.
  • As indicated above, Providers form the basis of scoping in Guice. If you take a look at the Scope interface, you'll notice its single method Provider<T> scope(Key<T> key, Provider<T> unscoped) is defined in terms of Providers. This method takes something that creates a new instance of an object (the Provider<T> unscoped) and returns a Provider<T> based on that which applies whatever policy the scope defines, potentially returning some cached instance of the object rather than creating a new one. The default NO_SCOPE scope simply passes along the unscoped provider, meaning a new instance will be created each time. The SINGLETON scope caches the result of the first call to unscoped.get() and thereafter returns that single instance, ensuring that everything that depends on the singleton-scoped object gets a reference to that single object. Note that the Provider returned by the SINGLETON scope's scope method does essentially the same thing as the Supplier returned by Suppliers.memoize (though it's a bit more complicated).

这篇关于Google Guava供应商示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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