The filter pattern enables you to define filters for your objects.
Suppose that you work with the same hierarchy of objects that we've seen in the previous tutorial,
a store with purses and bags.
If your goal is to filter out some objects based on their color or on any other available attribute,
then the filter pattern is the way to go.
The pattern is characterized by having a filter interface with classes that implement that interface,
which will allow us to do and or in various filters based on the attributes of the objects that are
filtered.
Let's have a look how this looks in the code.
We will be working with the same code as we did in the previous tutorials, so we have the same abstract
class here called Store, and for the sake of simplicity, I just deleted the back class and I left
only the purse.
So here we can see that the code is untouched.
We have the same constructor that accepts an object of type color and the same method that will return
the color to us.
Apart from this, we have the filter interface here that has only one method midfielder which will return
a list of persons to us and also it accepts as an argument on the software.
Of course, we should have had a store after class here, but for the sake of simplicity, I just left
it with letters and some classes also that are implementing this this interface.
So we have the glue filter that will filter all the purses that are blue.
So it will return a list to us that will contain only blue purses and an end filter that will perform
the logical end operation.
So basically it will apply to filters on and the final list will contain all the elements that were
filtered out by the two filters and another filter, which will perform as the name suggests, the ore
operation and this filter basically will return the elements that are in one list after filtering once
and not in the second list.
So this is actually the ore operation that is performed here.
So let's see this in action.
Let's and class.
So here we define three blue verses and one redBus.
So essentially, if we are and also we add them into one single list and essentially if we apply a blue
filter on them, then we should see only the blue passes and the red should be filtered out.
So let's see this in action.
So as you can see, instead of having all the four line zero, the four, of course, is printed here,
we have only three because we created only three blue versus the other one is red.
So, as you can see, this is working as expected.
So you can have plenty, plenty of criteria.
This is this as I said, this design pattern is also called criteria pattern.
So you can have as much criteria as you want and many, many classes that will implement the filter
interface here.
So but this is just an overly simplified example just to see how this should be implemented.
That's it for the filter pattern.
Join me in the next tutorial where we are going to discuss about a composite pattern.
So see you in the next story on.