Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Unfortunately the `Consumer` functional interface doesn't throw an exception. So let's make our own!
The code to this project can be found on GitHub.
Changes are made to the live
branch.
Okay, so we have a common problem.
0:00
We have used the consumer
functional interface, and
0:02
it unfortunately doesn't
throw any exception.
0:07
And so therefore, we have to cover
that in our lambda ourselves.
0:09
So we can make our own
functional interfaces, and
0:13
this is one solution to the problem.
0:15
And I think you might like it.
0:18
So let's take a look at it.
0:19
So I'm gonna open up
the project window over here.
0:20
And let's go in under this database here,
and I'm gonna make a new Java class,
0:24
and we are going to call
this a ConnectionConsumer.
0:29
Right, so, the ConnectionConsumer
follows the naming path of,
0:33
sorry, we're gonna actually
make this an interface.
0:39
We're gonna say, public interface.
0:44
ConnectionConsumer, and we're gonna mark
this with a functional interface, and
0:47
it follows the same naming structure,
right?
0:50
So we have like a boolean consumer in
the Java util function package, so
0:53
you kind of say what
type it's going to do.
0:57
And we don't really want it
to accept anything else, so
0:59
we don't need to make it anything fancy
here, so we'll make a public void, right?
1:02
So the consumer doesn't return anything,
so
1:07
that means it's all full of
side effects and all that.
1:09
So we're gonna use the same word, right,
let's kind of follow the same pattern, and
1:12
it's gonna accept the connection,
and we'll call it conn.
1:16
And now we can say that
this throws an IOException.
1:19
All right, so now we've made our own
functional interface just like that.
1:24
And if we come back over to our method,
or withConnection here,
1:28
how about instead of this Consumer,
very specific Consumer,
1:33
let's just go ahead and
call this a ConnectionConsumer.
1:38
All right, so now we have the ability,
if we look in our application here, woah.
1:42
If we look in our application over here,
1:49
we have the ability to just
let this bubble up, right?
1:51
Since we were already catching that over
in the other ceremony from before, so
1:56
we can get rid of this try and catch.
2:01
And now if we go ahead and run this,
we see that stuff still works.
2:03
And one of the things that I think is
gonna come out of you switching to using
2:09
more functional interfaces are people
are gonna say you know what?
2:13
Actually, why don't we just use
results for each year, right?
2:18
And then we'll use System.out, and
then we'll just put it right to println.
2:22
I mean, obviously, this is probably not
what you're doing in your application, but
2:29
now you'll see that people are using
more functional things there.
2:32
And now we're running again,
doing the same thing.
2:35
But yeah, only two lines of code here,
it's really clear what's happening.
2:37
Now, the other beauty of this is
you can now have a single place
2:41
over here in your legacy database
that you can innovate, right?
2:48
I can make this a pool, I could make
this here, this connection here,
2:53
I could get this out of a pool, and
I could push a connection in there.
2:56
And I could keep them open or closed, and
I don't really need to worry about that,
2:59
I could change what happens in here.
3:02
And I could very easily add transactions,
right, like I could begin the transaction,
3:05
and when it's done, it's done, commit it.
3:08
So it's pretty powerful, and
we have followed the same structure,
3:10
it's really easy to make one of these.
3:14
And I think your team's gonna love you for
stopping that dance.
3:17
So please share your feedback in the forum
and also with me if you wanna review these
3:21
videos, let me know what you thought
about this style of content.
3:26
Looking forward to giving you some more,
thanks.
3:29
You need to sign up for Treehouse in order to download course files.
Sign up