1 00:00:01,000 --> 00:00:04,730 The filter pattern enables you to define filters for your objects. 2 00:00:05,560 --> 00:00:10,570 Suppose that you work with the same hierarchy of objects that we've seen in the previous tutorial, 3 00:00:10,900 --> 00:00:13,060 a store with purses and bags. 4 00:00:13,840 --> 00:00:20,020 If your goal is to filter out some objects based on their color or on any other available attribute, 5 00:00:20,560 --> 00:00:22,480 then the filter pattern is the way to go. 6 00:00:23,700 --> 00:00:29,130 The pattern is characterized by having a filter interface with classes that implement that interface, 7 00:00:29,130 --> 00:00:36,390 which will allow us to do and or in various filters based on the attributes of the objects that are 8 00:00:36,390 --> 00:00:36,900 filtered. 9 00:00:37,230 --> 00:00:39,090 Let's have a look how this looks in the code. 10 00:00:41,620 --> 00:00:47,830 We will be working with the same code as we did in the previous tutorials, so we have the same abstract 11 00:00:47,830 --> 00:00:53,980 class here called Store, and for the sake of simplicity, I just deleted the back class and I left 12 00:00:53,980 --> 00:00:55,170 only the purse. 13 00:00:55,420 --> 00:00:58,690 So here we can see that the code is untouched. 14 00:00:58,960 --> 00:01:05,620 We have the same constructor that accepts an object of type color and the same method that will return 15 00:01:05,620 --> 00:01:06,600 the color to us. 16 00:01:07,180 --> 00:01:14,350 Apart from this, we have the filter interface here that has only one method midfielder which will return 17 00:01:14,350 --> 00:01:18,730 a list of persons to us and also it accepts as an argument on the software. 18 00:01:19,300 --> 00:01:27,040 Of course, we should have had a store after class here, but for the sake of simplicity, I just left 19 00:01:27,040 --> 00:01:33,530 it with letters and some classes also that are implementing this this interface. 20 00:01:33,530 --> 00:01:38,700 So we have the glue filter that will filter all the purses that are blue. 21 00:01:39,250 --> 00:01:47,320 So it will return a list to us that will contain only blue purses and an end filter that will perform 22 00:01:47,320 --> 00:01:48,800 the logical end operation. 23 00:01:49,030 --> 00:01:58,270 So basically it will apply to filters on and the final list will contain all the elements that were 24 00:01:58,480 --> 00:02:05,210 filtered out by the two filters and another filter, which will perform as the name suggests, the ore 25 00:02:05,230 --> 00:02:16,110 operation and this filter basically will return the elements that are in one list after filtering once 26 00:02:16,150 --> 00:02:18,480 and not in the second list. 27 00:02:18,520 --> 00:02:21,900 So this is actually the ore operation that is performed here. 28 00:02:22,240 --> 00:02:24,190 So let's see this in action. 29 00:02:24,190 --> 00:02:25,440 Let's and class. 30 00:02:25,810 --> 00:02:32,220 So here we define three blue verses and one redBus. 31 00:02:32,250 --> 00:02:41,650 So essentially, if we are and also we add them into one single list and essentially if we apply a blue 32 00:02:41,650 --> 00:02:48,540 filter on them, then we should see only the blue passes and the red should be filtered out. 33 00:02:48,670 --> 00:02:50,380 So let's see this in action. 34 00:02:53,680 --> 00:02:59,610 So as you can see, instead of having all the four line zero, the four, of course, is printed here, 35 00:02:59,830 --> 00:03:05,240 we have only three because we created only three blue versus the other one is red. 36 00:03:05,500 --> 00:03:07,890 So, as you can see, this is working as expected. 37 00:03:07,900 --> 00:03:11,060 So you can have plenty, plenty of criteria. 38 00:03:11,530 --> 00:03:16,770 This is this as I said, this design pattern is also called criteria pattern. 39 00:03:16,960 --> 00:03:24,220 So you can have as much criteria as you want and many, many classes that will implement the filter 40 00:03:25,120 --> 00:03:25,960 interface here. 41 00:03:26,200 --> 00:03:34,030 So but this is just an overly simplified example just to see how this should be implemented. 42 00:03:34,720 --> 00:03:36,470 That's it for the filter pattern. 43 00:03:36,490 --> 00:03:40,830 Join me in the next tutorial where we are going to discuss about a composite pattern. 44 00:03:41,230 --> 00:03:42,940 So see you in the next story on.