Hi, in this post we are supposing, we have a mobile application project, in which user can create there account,upload there personal info, images and videos on a server. First we divide the requirement into modules and the most important module which we get is network communication. So we decided we make a separate module or lib for network communication.
Now there is a possibility some people think, why we need this module? because we can use any third party Rest API client and use that for our requirement or some built-in threads for Network calls …
For your questions I will say one thing, In the end of this blog I will write a benefits section, which will give you answer of your all questions.
OK Its time to start our Network Module.
1. Create a new Android application.
Now create a new Module in Android Application with name Network Module or REST API Module.
Now here I am showing you very easy class diagram. Which will give you some information about our Module.
The above image is basically our Network Module. Now I will explain that one by one. I am also trying to use some design patterns, so in this way you will know how,where and when use design patterns 🙂 .
Here we are using two simple Interfaces. Code shown below.
Both are simple. Response Interface I will use as a Callback for Web Requests and Executor provide Information hiding.
Next we have an abstract class of WebRequest as shown below.
Here NetworkCommunicator is basically a main class which we use for Communication on Network. At this time I am using thread which will work in background and call a abstract method startRequest(). Its mean we are using polymorphic behavior of OOP.
Now I am going to show code of GetRequest and PostRequest classes.
Very simple. Both classes extending from WebRequest and both are concrete classes so we implement the startRequest(). But one thing which is very important, both classes are final and Package level. So its mean no one can access my these classes other then this package. It may confuse you, if no one can access these classes then why we are creating these 🙂 . I need to call Get or Post Request in my project for network communication.
I will remove your ambiguity as I complete this discussion. OK in the same manner our Put and DeleteRequest are defined.
Now I am starting first Interesting thing . As we know in GetRequest, I need one Url which is compulsory field, header may or may not require, its mean header is an optional field also in PostRequest I need Params. So as we know we want to make our module clean and good. I choose here one design pattern. Its name is BuilderPattern. This pattern basically a creational pattern. Which create a Object for us. One more thing if you have some class with 5 or more params in constructors and you know some are optional then BuilderPattern is a very good choice for you. OK How I use this pattern here in our module. Basically I created a Request Class which we use in creating of WebRequest.
Now its time to see benefit of this class. So I am showing you some code how I use this class to create a WebRequest as shown below.
Again I am repeating only we created a Request not executed in above Image. Now next part how execute this request in our application.
As you see in above image GetRequest is Red its mean compiler giving me error. So I am not able to create the direct object of this class. Question is why I am not able to create this class object? Answer is because as we discuss earlier I created this class package level. So new question arises, how I will use these requests? So for that I am using here one more very famous Design Pattern and its name is FactoryPattern. That is also a creational pattern. So its time to see the code of that factory class.
Now this is the public class and it has a one static method which will return the WebRequest. Now there are two questions.
1. Why we created a Factory class when we have option to public all our WebRequest extending classes?
2. How we use this class?
Very simple and clean code. As you see. I created a get and post requests and after that I execute both by using same interface. So we achieve the information hiding and polymorphism.
Now its time to learn what are the benefits of this module.
1. My networking class code is decouple. For example 100 projects are using this module and we want to change our Network Communication because we feel thread are not efficient so we want to use AsyncTask class. Now if we use direct Threads in our classes then we need to open 100 projects and all there classes and change all code. Its mean we need to waste lot of time also we are breaking Open Closed Principle.
But in module wise coding case I only need to open my Network class and change there code only.
🙂 Magic happens. We only change in a one class and all our 100 project shifted to AsyncTask efficient communication.
2. Factory Class:
Its time to enjoy the Factory Class. As we know any one who are using our module always create a WebRequest using Factory. Assume this module used in 100 projects and I also give direct creation. Its mean I will use like
GetRequest request = new GetRequest() on many places. Same for post requests. Basically I expose many classes to our clients which is bad. Now company decided to change all GetRequests to PostRequests due to some reason. Now can you imagine 100 projects and change all classes. But due to giving one interface to clients for making WebRequest, I save a lot of time. Now see what change require to convert all GetRequest to PostRequest.
Now I think this also give you some image how things are deprecated and backward compatible. I know that is a poor example but gives you a clear image.
I think that is enough. Your questions are welcome.