 Hello, this is Christian. Welcome to another course with me. And this video will look at delegates or loose coupling We're gonna take a look at delegates in Java how you can use delegates, which is a program designed to Create a loose coupling in your program. So here is an example of what delegates mean in a real-world scenario Think about the CEO of a huge corporation like Jeff Bezos and Amazon. Okay, of course I'm no mean. This is him, but just a demonstration here. We have CL, you have your regional managers, the area managers down the chain, store managers all the way down to your Packagers and the drivers down here. So one day you have an angry customer, right? The customer's pretty angry. He said, Hey, where's my package? And was late. And so the CEO heard about that. So he would bark at the top Saying he wants all the products and the customer is handed within an hour. And then this is passed down to the regional managers and they would then tell the area managers and they have no choice but to tell the store managers and the store managers would, you know, yell at the packages and drivers to tell them to pack faster and drive faster and So finally the customer is happy because he got his package within the hour And then of course by the end of the day the CEO makes tons of money We can the richest man in the world in the story. Okay. So that's what it is, right? Delegates. So what happened here? You know, so in the outside world You only see the CEO as the face of the company. It does all this cool stuff. It's really smart really intelligent It does a lot of work But really the actual people what you make things happen are the delegates in this case the little green guys down here, right? The packages and your drivers Your store closed and all those people here make things happen and they are the delegates The one above your managers and you supervise the bosses those are more of a delegate towards right the delegate task to you Packager or the driver to make things happen. So in this scenario, it can also be used in programming You can write a program to delegate tasks to other objects those object watch you do the actual implementation of those things Okay, so let's go and see an example how this is done in code So this is the main program here. So let's say I have a class called HP printer and It wouldn't print something. I have another class here We'll call it the Epson Also print something This is how delegates work In order for these two classes to share some common features Usually you can create an abstract class so they can share the things right which it's okay, too But the most common one is using interface. Okay, so interface Forces these two classes or any other class That implements interface to be in the same group almost what will talk to each other in a way So let's say this interface is called task Okay interface as you as you know, it's just very very broad very abstract. It provides only very Just the basic function A definition. So let's say we have a very simple function called public void Just to print something. Okay, that's their task. So you're not allowed to implement If it's not static So I'm going to make the HP X implements the task Once you do that, you'll see that you must implement this function Or however many functions there are in here that is not implemented So you can just right click on it implement And just say yeah, that's the one I want and there you go. You see something similar right familiar override This because you're required to implement this dysfunction whether it is something or not. It doesn't care The apps will be the same thing implements the task And then I'm just going to copy this exactly the same before down here Okay, so now I'll let the HP Do its thing. So we just put a message here saying um HP printing It'll copy the same one Down here. We'll just put for the apps in here and then There is this really important class called printer Also implements that task Okay, so same thing. I need to implement that function So all they look kind of same But what this one does is the printer class would work as a controller as Like someone who would delegate a task So if the printer receives a request that is coming from somewhere else to print Use the HP printer the print function here is not going to do this Instead it's going to create an interface object in here called printer Right, but whatever this printer is we don't care And then I'm going to create a constructor called printer except the task called printer to a guest And then I'm going to assign this object to This local object being passed to it right here Okay, and then when I invoke the print function, I'm going to go and say Let this printer do the printing Okay So let this printer here this object do the printing. So what this one does is like this If this is the injection here if I inject the HP object to this function It's assigned to a variable inside call a printer. So this printer here is really equal to HP's object So when I do the print right here the print function It actually calls HP's actual print function and you're going to see this If I inject the Epson object to this function Then this printer now is Epson and when I call the print function it calls the Epson function down here Okay, you only use one class here to manage to delegate everything So this is a delgator I guess you can call it it delegates the task to the actual object to do their own task If your boss said to you hey You have this done So instead of you doing it you give that to your team and they do the actual work Okay, you're just managing that for you And then and here when you create a project A task you can create like the HP HP here is new HP I have the Epson here Okay in new Epson So I have those two objects created now. I will create the a printer And I'll pass to the printer What do I want so I want if I want HP to do it and pass HP to that And then the printer will print I had to put inside the main function forgot about So it goes in here. Okay, so printer will print If you just look at this it looks like the printer object actually does the printing right But it's not because this print function here down as you can see if it calls this function inside here it lets the This HP does the printing Okay, so if I run this now you're going to see that The result should say HP printing You can see down here in the terminal So if I pass in now the Epson you see Epson And this is a really really important concept For the delegates or injection here I'm injecting something to the printer function Print the class and it and this object Internally does all the work But outside You think your printer actually does the printing Okay, so whoever uses this Cast interface Can call this function here and can do the job when the printer delegates that to you So when you inject this object to this function here This printer here becomes that object so it will access all the function inside that particular class