Android design casually speaking simple practice (module Division)

Source: Internet
Author: User

The previous essay with (Android design casually say) then said what is the design and design principles, here to give a simple example to further say Android design. We use the App Store design for example.

Before the design, need to grasp two parts of the content, in order to make the design more reasonable, appropriate.

The first part is the business that the application itself contains. The store's business is roughly a few:

1 Show Users The APK information

2 Provide user download apk

Of course, you can continue to subdivide. But it's enough for our example.

The second part of the local SDK, that is, which interfaces the phone can provide. We need to look at the API for the above analysis.

First, the interface shows the API can support.

Second, whether the network supports sockets,

Let's say we're already familiar with the basic business and API needs of the store, and start our design journey now.

In the previous article I mentioned two more critical start points. One is the module division, the other is a reasonable combination.

Say before the module division needs to explain what is the dependency, what is the interface.

The so-called dependency is the method that the module calls to the outside class. The so-called dependency inversion is the original module of the external dependence is realized, and now the external dependence of the module is a virtual interface. If an external entity can implement a virtual interface, it can be dependent on the module implementation.

The so-called interface is the method that the module calls to the outside world. There are two kinds, one is a direct public method. One is to tell the outside world in the form of an interface.

Let's start with the module partitioning of the App Store.

First, what is the UI part, and what does the UI do? UI has two functions, one is to show the user information, and the other is to provide users with a platform to operate. So the UI part needs data, and the source of the data requires support from other modules, unless it's a HelloWorld program. Of course, there is no time-consuming operation in the UI process, otherwise ANR occurs. Therefore, network requests are not allowed in the UI.

Example of a page recommendactivity,apk recommended page.

Recommendactivity (entity class, User user operation) the external interface is

    • APK Details view gotoappdetailsinfo (ID); Call this method to implement the APK details display.
    • APK Download request Gotodownloadapp (ID); Call this method to implement the APK download.

and interface class Recommendinterface (Interface, the underlying data changes update to the interface)

    • Onpageviewdatacomplate () page callback of your data
    • Onappinfodatacomplate () callback for appinfo data
    • Onimagecomplate (UIL) page picture data callback

..............

Recommendactivity dependency is recommendbase (interface or virtual base class for implementing class inheritance)

    • Getpageviewdata () Gets the template information for the page to display.
    • Getappinfosdata () Gets the app information to display the page.
    • GetImages (AppID) Get page picture information according to AppID
    • Setcommondinterface (Recommendinterface) Set callback

..............

The second part, the network request module. Responsible for retrieving APK data from the server.

Nettaskmanager (Entity Class) network task management, the interface class of this module. It relies on the httpget,httpconnect and other network interfaces to realize the business, is already the bottom, relies on the API. So what are the interfaces he provides?

    • Downloadimagebyurl () Picture loading interface
    • Downloadtemplatesbyid (ID) Request template based on page number
    • Downloadappsbytemplates () Requests app data based on the template number.
    • Downloadappsinfo (ID) Get app details by ID

................

The above is a forward interface, but it also needs to escalate the request result because the implementation of the module is done asynchronously, so you also need to define a callback Nettaskresultlistener (interface) to provide the following interfaces

    • Nettaskimagedownloadcommplate (URL), the external return image has been downloaded to complete.
    • Nettasktemplatesdownloadcommplate (ID), external return template data has been downloaded to complete
    • Nettaskappsdownloadcomplate (ID) external return app data has been downloaded.
    • Nettaskappsinfocomplate (ID) external return app details data

............

The above general said about the interface provided. Of course, there are some details that are not cared for here.

The third part, the Data parsing module, the data stream from the network can not be used directly, need a conversion, this module is responsible for this task.

Example Dataworkmanager (entity Class) is responsible for parsing and assembling data.

It relies on the interface, in fact he needs to parse the data, either JSON or XML is dependent on the API, so its dependency is the API.

It provides the following interfaces:

    • SetData (temp, apps) is responsible for parsing and consolidating the temp data and the apps data. (It looks so complicated, but it's usually a template and apps data, so it can't be split)
    • Setiamge (URL) is responsible for associating the image with the interface when the picture is downloaded.
    • Setappinfo (Appinfos) is responsible for appinfo data parsing and template integration.

......

In addition, the processing of these data depends on whether the amount of data is large and whether the time is long. If the amount of data is small and takes a long time, synchronization is possible without the need to provide callbacks.

    • Getviewdatabytempid (); Gets the ViewData to be displayed on the interface.
    • Getiamgebyurl (URL); Gets the graph according to the URL of the diagram.
    • Getappsinfodata (ID) Get app details page data based on AppID

.......

If the amount of computation is large and takes a long time, asynchronous processing is required and a callback interface is provided. No more detail here.

Part IV, download module, responsible for the application download. One would ask that the download also requests data from the network and why it is not in the network request module. The reason is that the two modules function completely different, the responsibility of the network request module is responsible for the page data display, as the page slide down, it needs to keep the request, if the page jumps, you need to pause the original request, make a new request. The uncertainty is relatively large. But the download module, responsibility is responsible for the APK download. He needs to differentiate between networks that are that kind of network, and that downloads are in order, unless they are artificially altered.

An example of Downloadappmanager (Entity interface Class) dependency on the outside world is actually the API of the network interface.

For external interfaces:

    • Startdownload ();
    • Setdownloadapps (appinfo);

............

There is also an external callback interface class Downloadlistener

    • Downlaodprogress (ID, progress);

.............

The fifth part, the dispatch module, in order to the above four modules to realize the business methodically, need this module to dispatch. This module is responsible for user interaction and business implementation of the intermediate module, on the upper level, responsible for receiving the user's response and provide UI interface data. On the lower level, responsible for dispatching all business units to achieve the overall business .

Name the module with a controller.

The interface it needs to provide is as follows:

    • Getviewbytemp (ID) users to different pages to launch data requests on different pages.
    • DownLoad (ID), stopdownload (ID) ..... User-Downloaded instruction interface
    • Getappinfo () The user views the app details screen and launches the app details data request.

It also needs to provide the callback interface as follows:

Controlleruiinterface

    • Onviewcomplate (viewData); page data combination complete callback
    • Ondownloadprogress (id,progress) Download Progress callback
    • Onappinfos (viewData) app details page data preparation callback

Controllerdownloadinterface

    • Downloadiamgecomplate (URL) picture Download complete callback
    • Doanloadtemplatecomplate (id) template Download completion callback
    • Downloadappsbytempcomplate (ID) template data download completion callback

Controllerdownloadappinfo

    • Downloadappstart (ID) Start Download app callback
    • Downloadappprogress (ID, progress) app download Progress callback

It needs to depend on the interface Controllerdependinterface (virtual class, or interface).

1 for network requests

    • Downloadimage (URL) add download picture
    • Downloadtemplatebyid (id) Download template
    • Downloadappsbytemp (ID) Download the app information for the template

2 data processing for network requests

    • SetData (temp, apps) for templates and app data combinations
    • Setappinfo (Appinfos) for a combination of templates and app details
    • Getviewdatabytempid () Gets the combined ViewData
    • Getiamgebyurl (URL) Get pictures
    • Getappsinfodata (ID) Get the combined app details data

3 for app Downloads

    • Startdownload () Start download
    • Setdownloadapps (appinfo); Add Download item

We analyze the dispatch module, it has two functions, the first by dispatching the various business units, so as to achieve the entire business. After receiving the interface instruction, the instruction is split, the results are synthesized and reported. The advantage of doing this is that the business is handled uniformly. Second, it splits the UI tightly with each business unit, adding a layer to isolate the UI and each business, leaving the UI layer and the individual business units in a separate and disparate relationship. The advantage of this is that the business unit does not directly affect the UI, the structure is flat and adds stability.

But this is not good, if the business is huge, such a dispatch module is bloated. Of course, the other method is that the data processing module relies on the network module, and the dispatch module no longer relies on the network module. This is a flat vertical design, no matter what, as long as the module division is similar, it is easy to go on.

The partitioning of the modules is explained here. Tomorrow say a reasonable combination of parts.

Android design casually speaking simple practice (module Division)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.