Java Google Guava 供应商示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4014589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Guava Supplier Example
提问by Emil
Please explain the use of the interface Supplier(in Guava) with a suitable example .
请用一个合适的例子解释接口供应商(在番石榴中)的使用。
采纳答案by ColinD
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
接口只是一个无参数函数的抽象,它返回一个值......它是一种获取某个或多个对象实例的方法。由于它是如此通用,因此它可以用作很多东西。Jared 解释了Multimaps
工厂如何利用它作为工厂来Collection
为值创建某种类型的a 的新实例。
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.
鉴于界面的简单性,它还允许Supplier
通过将a的行为包装在Supplier
以某种方式改变其行为的另一个中来对 a的行为进行一些非常强大的修饰。记忆化就是一个例子。我自己使用该Suppliers.memoizeWithExpiration
方法作为一种简单的方法来实现,因此某些数据在给定的时间段内最多只能从服务器读取一次。
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.
我还建议您查看 Guice 以及如何在其中使用Provider
界面。Provider
完全等同于Supplier
Guice 的工作方式,并且是其工作方式的核心。
Provider
allows users to define a custom way of creating new objects of a given class. Users can write aget()
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 factoryfor new instance of an object.- Guice allows injection of a
Provider
of any dependency. This may return a new instance every timeget()
is called or it may always return a single instance or anything in between, depending on how the binding theProvider
represents is scoped. This also allows for "lazy instantiation" of dependencies... theProvider
gives a class a means of creating an objectwithout 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,
Provider
s form the basis of scoping in Guice. If you take a look at the Scopeinterface, you'll notice its single methodProvider<T> scope(Key<T> key, Provider<T> unscoped)
is defined in terms ofProvider
s. This method takes something that creates a new instance of an object(theProvider<T> unscoped
) and returns aProvider<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 defaultNO_SCOPE
scope simply passes along theunscoped
provider, meaning a new instance will be created each time. TheSINGLETON
scope caches the result of the first call tounscoped.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 theProvider
returned by theSINGLETON
scope'sscope
method does essentially the same thingas theSupplier
returned bySuppliers.memoize
(though it's a bit more complicated).
Provider
允许用户定义创建给定类的新对象的自定义方式。用户可以编写一个get()
方法,该方法可以执行创建新对象所需的任何代码,因此他们不仅限于让 Guice 单独使用构造函数来创建对象。在这里,他们使用它来为对象的新实例定义自定义工厂。- Guice 允许注入
Provider
任何依赖项。这可能会在每次get()
调用时返回一个新实例,也可能总是返回单个实例或介于两者之间的任何实例,具体取决于Provider
表示的绑定的范围。这也允许依赖项的“延迟实例化”......这Provider
为类提供了一种创建对象的方法,而无需提前实际创建对象。对象的实例不需要在get()
调用when 和 if 之前创建。 - 如上所述,
Provider
s 构成了 Guice 中作用域的基础。如果您查看Scope接口,您会注意到它的单个方法Provider<T> scope(Key<T> key, Provider<T> unscoped)
是根据Provider
s定义的。此方法采用创建对象(theProvider<T> unscoped
)新实例的内容,并Provider<T>
基于应用范围定义的任何策略的内容返回 a ,可能返回对象的某些缓存实例,而不是创建新实例。默认NO_SCOPE
范围只是传递unscoped
提供者,这意味着每次都会创建一个新实例。该SINGLETON
范围缓存第一次调用的结果unscoped.get()
然后返回该单个实例,确保依赖于单例范围对象的所有内容都获得对该单个对象的引用。注意,Provider
通过返回的SINGLETON
范围的scope
方法做本质上是一回事作为Supplier
由归国Suppliers.memoize
(虽然这是一个比较复杂一点)。
回答by Christoffer Hammarstr?m
It's a way to provide an indirect object. You may want to provide another object each time Supplier.get() is
called.
这是一种提供间接对象的方法。您可能希望每次Supplier.get() is
调用时都提供另一个对象。
For example, i have a singleton class called SmtpMailSender
, which takes a hostname for the smtp server. However, the hostname can change at runtime, so instead of taking a String hostname
, it takes a Supplier<String> hostname
.
例如,我有一个名为 的单例类SmtpMailSender
,它采用 smtp 服务器的主机名。但是,主机名可以在运行时更改,因此不是采用String hostname
,而是采用Supplier<String> hostname
.
回答by Gareth Davis
回答by nanda
回答by amertum
Another example use of Supplier:
供应商的另一个使用示例:
http://javawayoflife.blogspot.com/2010/06/unit-test-and-new-date.html
http://javawayoflife.blogspot.com/2010/06/unit-test-and-new-date.html
回答by Jared Levy
The main reason we included Supplier in Guava was to support the Multimapsmethods that generate arbitrary Multimaps, such as
我们在 Guava 中包含 Supplier 的主要原因是支持生成任意 Multimaps的Multimaps方法,例如
public static <K,V> Multimap<K,V> newMultimap(Map<K,Collection<V>> map,
Supplier<? extends Collection<V>> factory)
The Supplier creates a Collection that holds all of the values for a given key. The Multimap uses the Supplier whenever you store a key-value pair with a key that's not already in the Multimap.
供应商创建一个集合,其中包含给定键的所有值。每当您使用不在 Multimap 中的键存储键值对时,Multimap 就会使用供应商。
回答by thSoft
Another great use of the class is decoupling - if a component only uses another to obtain a value, do not depend on the concrete implementation, but on this interface.
该类的另一个重要用途是解耦——如果一个组件只使用另一个来获取值,则不依赖于具体的实现,而依赖于这个接口。
Anyway, there is some example code here: http://www.slideshare.net/tfnico/google-guava
无论如何,这里有一些示例代码:http: //www.slideshare.net/tfnico/google-guava