Closed GoogleCodeExporter closed 9 years ago
What's the use case?
Original comment by limpbizkit
on 28 Mar 2008 at 1:23
This class would allow for a supplier that would be a composition of smaller
more
specific suppliers. Lets say we have
ChipSupplier
PretzelSupplier
CheesePoofSupplier
CandySupplier
Then we could create a SnackSupplier with the list order being PretzelSupplier,
CheesePoofSupplier, ChipSupplier and the default supplier being CandySupplier
because
there is always candy laying around. The predicate we would pass in checks to
see if
there is enough of that snack available to eat.
The list of suppliers provides a preference order. Try to find pretzels, if
there are
no pretzels try and find cheese poofs if there are no cheese poofs try and find
chips.
The SnackSupplier can be written without a ChainSupplier by placing all the
logic
from the different suppliers into a large if else if else if else etc block.
But if
you are using a declarative programming framework with google collections such
as
Spring then creating the SnackSupplier via a ChainSupplier wouldn't even
require a
single line of code.
<bean id="snackSupplier" class="com.google.common.base.Suppliers"
factory-method="chain">
<constructor-arg>
<ref bean="availablePredicate" />
</constructor-arg>
<constructor-arg>
<list>
<ref bean="pretzelSupplier" />
<ref bean="cheesePoofSupplier" />
<ref bean="chipSupplier" />
</list>
</constructor-arg>
<constructor-arg>
<ref bean="candySupplier" />
</constructor-arg>
</bean>
Original comment by TrashH...@gmail.com
on 28 Mar 2008 at 6:14
Do you have a more concrete example? I haven't seen this come up before.
For what it's worth, I'd take the single line of code over 15 lines of XML.
Original comment by limpbizkit
on 28 Mar 2008 at 6:30
Any distributed business object would use a similar concept. If server A fails
to
give me a result go to server B and so on and so on.
Another example is when you have a base interface with multiple implementing
classes
and you wish to implement a supplier to get a object out of that base interface.
Original comment by TrashH...@gmail.com
on 28 Mar 2008 at 6:42
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 5:57
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 6:02
This issue has been moved to the Guava project (keeping the same id number).
Simply replace 'google-collections' with 'guava-libraries' in your address
bar and it should take you there.
Original comment by kevinb@google.com
on 5 Jan 2010 at 11:09
Original issue reported on code.google.com by
TrashH...@gmail.com
on 28 Mar 2008 at 12:11Attachments: