preface

The web is an important part of our development process, so we started with a web framework or web module. In the framework development process, I will sort out the development ideas, as well as some design problems will have what to consider, the solution, of course, this is just my personal point of view, you can also have their own implementation. In addition to the web framework, I would like to update the ImageLoader framework, ORM framework, and animation framework and Microblog development series if I have time. Of course, these frameworks are just some simple framework foundations, my level and time are limited, and there are already many ready-made and mature frameworks. Here we just learn the wheel construction process by repeating the attitude of building wheels, so as to reach the point of building wheels. As for many details, we will not discuss more here, if you are interested, you can do your own research.

Finally, we’ll call this framework SimpleNet for now, and let’s get to the point.

The basic structure



Figure 1 (Basic structure of SimpleNet)

The SimpleNet framework has a basic structure similar to Volley, including some naming similarities. It is mainly divided into four parts, the top part is the Request, that is, the various Request types. For example, if the data type returned is JSON, it corresponds to JsonRequest, and if the data string returned is StringRequest. If you need to upload files, you need to use MultipartRequest. This request only supports uploading small files.

The second part is the message queue, which maintains the list of requests submitted to the network framework and sorts them according to the corresponding rules. This queue uses a thread-safe PriorityBlockingQueue because our queue can be accessed concurrently and therefore requires atomicity of access.

The third part is Executor, or network Executor. This Executor inherits from Thread, loops through the second part of the request queue in the run method, and delivers the result to the UI Thread when the request completes. In order to better control the request queue, such as request ordering, cancellation, etc., we do not use the Thread pool to operate, but manage the queue and Thread form, which makes the whole structure more flexible.

The ResponseDelivery class executes the network request in the Executor of section 3, which is the Thread, but we can’t update the UI in the main Thread, so we use ResponseDelivery to encapsulate the delivery of the Response. Ensure that the Response executes in the UI thread. Each part has a relatively single responsibility, which facilitates future upgrades and maintenance.

Framework for analysis

Figure 1 looks a bit like a layered architecture, but it isn’t. This diagram expresses its logical order more than its structure. In our application development, layered architecture is an important tool, as shown in Figure 2.



Figure 2

But during development, we tend to couple the UI and business layers because they are too closely related to be easily disintegrated. Experts can simplify complex things, and decomposition is an important means of simplification, decomposition process in the development process we call refactoring.

So we have introduced a concept of layering, and you can also follow the structure shown in Figure 1 to make it easier to understand. So what are the pros and cons of layering?

Advantages:

  1. Complex problems are decomposed and simplified, and each layer is responsible for its own implementation and provides services to the outside;
  2. Responsibility separation, complex systems are developed by many personnel, the management and integration of these function development is a very serious problem, after the implementation of layered design, each layer only needs to define its own external interface, other dependent layer services can be developed;
  3. Each layer is independent of the other layers and hides implementation details from the outside. The upper layer does not need to know the details of the lower layer, but only needs to call the interface.
  4. Good for standardization.

Disadvantages:

  1. Changes to the domain business after layering may require changes to many layers;
  2. Too many layers affect performance.

As mentioned above, our SimpleNet is not layered, it’s simply modular, but the rationale is similar, relying on abstraction over implementation, a single responsibility… Here we introduce the concept of layering, which is easy to understand, but also hope that we can try to ensure the cohesion and coupling of modules in the development process. If you look at SimpleNet, Request is an abstract generic class, and the generic type is the Response type that’s returned, so StringRequest, for example, inherits from Request. The second part of the RequestQueue relies on Request. Request is abstract, so any subclass of Request can be passed to the Request queue. It relies on the abstract Request rather than a concrete implementation, thus ensuring extensibility. You can implement your own requests, such as large file upload requests. In the same way, NetworkExecutor in Part 3 relies only on the Request abstraction, but here we introduce another type, HttpStack. The real executor of the network Request is HttpClientStack and HttpUrlConnStack. HttpURLConnection (Apache HttpClient) and HttpURLConnection (Java HttpClient). For details about the differences between the two types of HttpURLConnection and HttpClient, see: HttpURLConnection or HttpClient. . HttpStack is also an abstraction. Whether HttpClient or HttpURLConnection is used depends on the operating system version. HttpStackFactory returns the corresponding HttpStack to the framework based on the operating system version. ResponseDelivery (RequestListener onComplete) {ResponseDelivery (RequestListener onComplete) {ResponseDelivery (RequestListener onComplete) {ResponseDelivery (RequestListener onComplete) {ResponseDelivery (RequestListener onComplete) {ResponseDelivery (RequestListener onComplete);}}

Let’s take a look at SimpleNet’s engineering structure, as shown in Figure 3.



Figure 3

That’s the basic structure of the SimpleNet framework, if you’re looking forward to the next blog post.

SimpleNet Github address.