Welcome back.
In this video, we're going to look at two of the major reasons why interfaces are used.
And they are decoupling code and enhancing testability.
So let's look at where this would be useful.
It is useful in large applications where components need to interact with each other in a loosely coupled
manner.
Why does that matter?
Well, interfaces help decouple code by reducing dependencies between classes.
This makes the code easier to test and to maintain.
And when would you use it?
Well, you use interfaces when you want to decouple the implementation details from the interface contract,
which is especially useful in large and complex systems.
So we're going to build a real world example.
But it's going to obviously be a very simple small application.
This would really not make sense if you build an app by yourself, like a small application with maybe
a dozen classes or less than that.
But once you have more than that, it really starts to make a lot more sense to use interfaces.
Okay?
And this will make a lot more sense, especially if you want to work as a developer in a corporation
on a big existing software where there are already hundreds and hundreds of classes with tens of thousands
of lines of code.
Okay, let's look at the real world example where we're going to log in an application.
So consider an application where you need to log messages to different destinations such as a file,
as we've seen already, a database or a remote server.
So all of those would have to be separate classes and all of the implementations.
But actually it's a lot easier when we are using decoupling and interfaces.
So let's get started.
Therefore I'm going to keep this code.
I'm going to copy it over in a second.
But first we're going to create an interface public interface.
And I'm going to call this one Ilogger.
All right.
And this logger will have a log method.
So void log string message okay.
Very simple.
And then I'm going to have a public class which will be called file logger which will implement the
I logger interface.
So now inside of this I'm going to have a public void log method.
I need to follow the rules of the contract.
And the contract says you need to have a log method that takes a string as a parameter and returns nothing.
All right.
So my implementation of the Ilogger interface for the log method will be our code from the last video.
Okay.
So basically I'm going to say this is how I'm going to log it.
And I'm not going to create a new message, but I'm going to override or actually I don't need to overwrite
the message.
I'll just take the message that is sent to my log method, and then I'm going to I'll write it into
this file.
Okay.
So that's the implementation of the file logger.
And now let's look at the database logger.
So I'm going to have another class public class database logger which will also implement the interface
Ilogger.
And you see I need to obviously now also have this method void log which will take in a message string.
And now I need to do the implementation of how it would be done for a database logger.
Okay.
Now I'm going to keep it simple.
I'm just going to say logging to database.
And I'm just going to log the message here like so.
Now obviously here I would implement the logic to log a message to a database.
But as we haven't covered databases yet, we're just going to keep it very simple and just write onto
the console.
So now comes the interesting part okay.
So now what we're going to do is we're going to have a class that will use the database logger.
So our application class public class application which will have a well it will have a constructor
and it needs to use the logger okay.
So I'm going to have a private read only I a logger which I'm going to score a call underscore logger.
And I'm going to have an I logger that needs to be passed on to the application constructor, where
I'm just going to set the read only I logger private field.
So this application now is ready to use the logger.
And this application doesn't care who's.
Going to do the logging?
Is it going to be the database logger or is it going to be the file logger, or is it going to be a
stream logger?
Whatever kind of logger you are passing on to me, I don't care.
Just do the work.
And what is the work that I want to do?
Well, I'm just going to say public void work or do work.
And this method will just use the logger to log.
And what should it log?
It should log a message.
So I'm going to say work started.
Then do all the work, whatever the work is that the application needs to do.
And then I'm going to log again to the logger, say work done, good job or something like that.
Okay.
So now what I can do is in my main class, I can go ahead and create a logger, which I'm going to call
file logger, which will be a new file logger.
And I'm going to create an application which will be a new application using that file logger.
And then I'm going to do the work.
And then I'm going to do the same thing with the other logger as well.
So I logger and this one will be a DB logger.
So a database logger which will be a new database logger.
And I'm going to overwrite the app.
So the app will be a new application.
But this time it's going to use the DB logger.
And it will do the work on that as well.
So we have seen this in action before okay.
We've used a very similar example.
Last time.
I just wanted to really bring down the point of what's going on.
So this here what's happening is decoupling.
Let's look at at what it does.
Exactly.
So first of all it sees log.
We see logging to database work started.
And then we see log in to database work done.
Now let's look into our file as well which we created.
So we should see it inside of our logs folder.
Log dot txt.
And you see work started work done.
Good job.
So there we are.
Now our logger.
It doesn't care if it's a file logger.
So here or if it's a database logger the application doesn't care.
What it does is it just does the work and it allows it to actually execute the work with whatever eye
logger we are passing to the applications database.
So which part of it is decoupling?
Well the decoupling part is that the application class depends on the eye logger interface rather than
specific implementations like the file logger or database logger, or stream logger or whatever logger
you come up with.
Right.
It's only important that this file logger and database logger implement the eye logger interface, which
then in turn obviously implements the logging file method.
This log method here and then it doesn't care.
So this means you can easily switch the logging mechanism without changing the application class.
So we don't need to implement how the logging works and to do work class.
Or we don't need to think about like using different classes and which class or which object are we
using from which class and so forth.
It doesn't matter.
It doesn't care.
And that's the decoupling aspect of it.
Really, really cool stuff.
And at the same time this enhances testability.
So you can test your application.
This one here, our app so to speak with different logging implementations or mock objects.
But more on testing later on in the course with Yannick.
Okay, so this was just really to bring home the point of what is decoupling.
We've seen decoupling in action before.
We've seen it now once again, but with focus on the decoupling aspect.
All right.
And this makes obviously a lot more sense if you have tons of classes and tons of code and really big
projects where one person is defining what a logger should generally do, and then other people can
create their own classes that implement AI logger, and then they can pass on their own classes.
So their own way of implementing how an AI logger works, or the log method of an AI logger works too.
The application itself right here.
So now you could go ahead and you could create your own logger that just needs to follow the AI logger
interface or implement the AI logger interface.
And you could just say whatever you want to do with your log method and just go totally ham.
And you could use your own class object and pass it on to the application constructor when creating
a new application.
Okay, so that's it for this video.
See you in the next one.