1 00:00:00,590 --> 00:00:07,910 As usual, let's start by looking at abstractions first so we have this iterator interface, which contains 2 00:00:08,240 --> 00:00:15,440 the prototypes for methods that will contain the algorithm for traversing our container and of course, 3 00:00:15,440 --> 00:00:19,290 the container interface here, which has a method that returns and iterator. 4 00:00:19,670 --> 00:00:25,130 So let's see the classes that implement those those interfaces. 5 00:00:25,140 --> 00:00:28,100 So the employee directory will implement the container. 6 00:00:28,340 --> 00:00:34,940 The names of the employees will be stored in a list which will be given in the constructor of the employee 7 00:00:34,940 --> 00:00:36,130 directory object. 8 00:00:36,140 --> 00:00:43,010 And as you may have guessed, get iterator will return a new name iterator to us. 9 00:00:43,670 --> 00:00:51,710 The name Iterator will use the composition technique to store the employee directory to have a reference 10 00:00:51,710 --> 00:00:52,970 to the employee directory. 11 00:00:53,720 --> 00:01:02,280 And these two methods will create the means for traversing and retrieving objects from inside the container. 12 00:01:02,300 --> 00:01:06,140 So the first one, the next method will return. 13 00:01:06,140 --> 00:01:14,420 True, as long as the employee directory is is bigger than the index, otherwise it will return false 14 00:01:14,430 --> 00:01:18,500 and the next, as long as the house next will return. 15 00:01:18,590 --> 00:01:27,200 True, then they are the object that is stored at that specific index will be returned to to us, will 16 00:01:27,200 --> 00:01:28,530 be returned by this method. 17 00:01:29,060 --> 00:01:30,890 Let's see how the client looks like. 18 00:01:30,920 --> 00:01:37,880 So basically here we define a new list of employees with names or employees and we create an employee 19 00:01:37,880 --> 00:01:40,730 directory based on this list, as you can see here. 20 00:01:41,000 --> 00:01:44,290 And this is how the for loop looks like. 21 00:01:44,300 --> 00:01:51,260 So we basically create an iterator based on the get the iterator method from the employee directory. 22 00:01:51,560 --> 00:01:57,030 And we use the next method here to to traverse the container. 23 00:01:58,010 --> 00:02:06,560 This this out you should print all the names of the employees using the next method, which like we've 24 00:02:06,560 --> 00:02:12,080 seen, it's returning the objects that sit inside the container. 25 00:02:12,080 --> 00:02:13,570 So let's see this in action. 26 00:02:14,270 --> 00:02:15,890 Let's run this quickly. 27 00:02:15,890 --> 00:02:23,270 And as you can see, we get the names here for all of the employees that are stored inside the employee 28 00:02:23,270 --> 00:02:23,890 directory. 29 00:02:25,160 --> 00:02:25,850 That's it. 30 00:02:25,850 --> 00:02:34,370 For for this matter, you can download the code that we've used in this example from from the video 31 00:02:34,370 --> 00:02:37,150 itself, from the video attachments section. 32 00:02:38,060 --> 00:02:42,620 You can start playing with it so that you will understand it better. 33 00:02:42,620 --> 00:02:47,720 And like I said, this is not a very complicated design pattern. 34 00:02:48,170 --> 00:02:53,540 It's it is something that is offered by the Java API through the Iterator interface. 35 00:02:53,840 --> 00:03:01,520 So it's nothing new, but it's a very, very good pattern to implement whenever you want to, to traverse 36 00:03:01,520 --> 00:03:02,970 a custom data structure. 37 00:03:03,230 --> 00:03:06,430 So this is the best use case for this design pattern. 38 00:03:07,280 --> 00:03:08,470 So that's it. 39 00:03:08,480 --> 00:03:09,770 Join me in the next video. 40 00:03:09,790 --> 00:03:13,220 We are going to discuss about the mediator, Patrick.