Hi folks, Co-Tutor here and my name is Anil Deshpande. Hi folks, welcome to the second video of Solid with Android series that we have just started in the previous video. In the previous video, we have gone through how to basically refactor DAO.
We have refactored model and the app database with the database provider. If you want to understand why we did it, how we did it, you can go back to the previous video. Now it is time to go beyond that and start refactoring code beyond the model.
And what is beyond the model? We were about to start refactoring the repository. So let me open the repository that is here, country repository. This country repository now implements iCountry repository.
You can see that I had commented out filter countries. But before we go there, let me check whether anything else can be refactored. If you see here, all of our functions use with context dispatch IO. These are basically nothing but the dispatchers.
So dispatcher basically allow you to control where you want to execute this particular suspend function. Suspend function basically gets executed as a coroutine. So here in this case, we are trying to run it on a IO dispatcher.
But in case if you want to avoid using IO dispatcher and want to use something else, you will have to come here in this particular class and start changing everywhere these things. And I don't want to do that. That is basically a violation of OCP.
That is open end. closed principle. So instead of directly explicitly using the dispatcher.io here, it would be better if I start using dispatcher that I have received as a argument to this particular class. So what I can do is I can send the dispatcher as one of the argument to the country repository. I will create another argument variable called as dispatcher which would be of type coroutine dispatcher and then I will use this particular dispatcher wherever I need it instead of explicitly initializing coroutine dispatcher here.
So that will basically solve one set of problem. Now it is time to implement this filter criteria. So before I show you what is the filter criteria let's understand how it is currently implemented.
Now let me go to the view model country view model you can see here we have these functions that is filter country by continent and then you have function filter country by drive side now the problem with this particular mechanism is later point in time your team lead or your project manager will come and say that i don't want to filter only by these two categories i want to even filter by say for example language that is spoken in that particular country in that case you will have to come and start modifying this particular code again. That means you have to start fiddling with the contribu model that you have already implemented. You want to avoid that because that is basically a violation of open and closed principle.
You should be able to modify code without breaking any existing code. When I say breaking, I'm not saying making it fail. I am basically saying touching the existing code.
You should be able to basically introduce new changes without modifying the existing code. Now how do we go about this? The way we can go about it is by using a pattern called as strategy pattern.
Now what is a strategy pattern? It might sound very fancy but it's not that complicated. Let me explain it to you through a simple analogy. Let's assume that cutting is a task in cooking and if I say cut a vegetable, cut a bread or cut a...
pizza even though the task is cutting but the way you cut a vegetable the way you cut a bread or the way you cut a pizza will be different the strategy will be different but at the end of the day it is cutting depending upon what material is handed to you you will basically change your strategy of cutting so your senior chef will just hand you a material and say cut this if it is a bread you will cut in different way if it is a vegetable you will cut it in a different way the same concept is applicable here as well in this case i want to filter either by continent or by drive side depending upon upon what kind of filter that i want to apply which is the actual task i should be able to either choose the continent or the drive side so let's understand how do we go about this what i will do is i will create a interface somewhere here yeah let me create interface and that particular interface will have a suspend function that is suspend filter and it basically takes a list of countries and returns another list of countries. What it returns is basically a filtered list of countries from this list of countries that it might have received as an argument. How you want to actually filter will be determined by class that will implement this particular filter criteria interface.
So what I will do is I will create another class which is filter by continent. And this filter by continent will actually implement the filter by continent functionality so it basically implements the filter criteria takes the string by which it needs to filter and it implements filter functionality so it basically applies the filter on a country's list by continent string that you would be passing to it and later point in time let's assume that you want to add another functionality all that you will have to do is create another class that implements this filter criteria. So let's assume that I want to filter by drive side.
So I will create another class which is filter by drive side and this filter by drive side will also implement filter criteria but it will use the drive side as the parameter or a criteria to basically do the actual filtering. So later point in time if somebody says that I want to filter by language. all you will have to do is create another class that implements the filter criteria and you don't have to worry at all about fiddling the existing code this is how you make your code ocp compliant so now that we have implemented these filter criteria implementations time to use them in our repository so let me go back to the icountry repository and enable this code now let me go to the functionality here country repository and actually implement the filter functionality and how it would be as simple as this this filter countries will basically receive the filter criteria what filter criteria whether by continent by drive side doesn't really matter as long as it implements the filter criteria that's the power of abstraction i am just delegating it to the filter criteria dot filter and i pass all countries all countries is basically defined here in the repository So now that we have refactored the repository also, it is time to refactor the next part of our application which comes even above the repository.
And what is that? That is the view model. And here is our country view model.
Let me stop now. We shall see refactorization of the model and remaining parts as per the solid principles in the That brings us to the end of this particular video. Don't forget to like, comment, share the video and subscribe to the channel.
Take care. Bye.