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
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:53
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:56
Original comment by fry@google.com
on 26 Jan 2011 at 10:40
Original comment by kevinb@google.com
on 13 Jul 2011 at 6:18
Original comment by fry@google.com
on 10 Dec 2011 at 3:15
We haven't found enough evidence that this is worthwhile.
Original comment by kevinb@google.com
on 16 Feb 2012 at 6:31
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:16
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:10
Original issue reported on code.google.com by
TrashH...@gmail.com
on 28 Mar 2008 at 12:11Attachments: