Complete Android MVP Development tour _android

Source: Internet
Author: User

Development background
Recently, in a fitness-related app, which has a Trainer Module basic function is to follow the characteristics of the action of the demo and description to guide users to complete the training. In the first version, it took weeks of time for a few 1500 lines of code to complete the function without touching some of the class items and functions,
While I was trying to make the code clear, I could imagine what it would be like to include so much code in an activity. It's hard to maintain yourself.

First talk about design
With a previous design experience the development of the use of MVP, modular, interface-oriented concepts, the entire trainer is divided into controllers, data models, audio, views, training objects five modules are represented by the following interface respectively:

    • Itrainercontroller
    • Itrainermodel
    • Iaudioflow
    • Itrainerview
    • Itrainable

Remove some abstract classes after the interface diagram is as follows:

Design the above interface after introducing the MVP concept using Itrainercontroller as Presenter,itrainermodel do model,itrainerview do view the following main modules.

Controller
Can be used in MVC and MVP depending on which development pattern is used, the project controller I developed is used to control the trainer's life cycle, such as training, pausing, resting, completing, coordinated Itrainermodel, Itrainerview, Iaudioflow and other modules.
More than one controller is used in the process this is also the reason for abstracting an interface, the Itrainercontroller interface inherits the Ipresenter interface so that it can be used as presenter.

Data model
The data model contains a large number of Itrainable objects and provides data support to the external organization data. There are two main ways to organize data:

    • From the local database
    • Getting from the network

In the trainer may be normal training or a training test and training data and test data have some differences, but their data are treated as itrainable, test data is not needed to be saved only from the server pull after the required completion on the line and training will produce local records.
It is necessary to provide different data models for different data organization methods.

Audio
Audio comparison variety like the training process contains the action name, time, unit words, reminders and other audio these audio are separate different audio files. There are two main ways to implement Android:

    • Soundpool
    • MediaPlayer

First of all, the advantage of Soundpool is that it eliminates loading, manage audio and other processes but it does not fit in with our trainers, mainly because of the lack of preparation, some callbacks after completion, and the fact that these processes are necessary during the trainer's operation, such as when the audio is ready to begin, and then we can have formal training.
Finally, the adoption of MediaPlayer, but also in the use of the process of audio management and the release of resources to avoid the packaging of more than one time. I put all the audio logic in the Android service at design time. The activity uses the audio through bind Audioservice, putting the audio logic into the audioservice so that the audio can finish
Fully independent so that it can play in the background and can also improve the process priority.

In the design Audioservice only playing and managing audio and resources does not have the logical function of audio playback. Because of the different training ways audio playback logic is also different so design the Iaudioflow interface to be responsible for the audio logic.

Training view
Android common activity as a view, through the implementation of the Itrainerview interface to complete the training view display. The view does not contain any business logic code.

Another talk about realization
When it comes to implementation, this is not the most important thing to focus on, because it provides a comprehensive interface and our modules are the interfaces that are used, so no matter how those functions are implemented, they do not have a big impact on each module unless there is too much difference between the functional implementation and the actual requirements. Here I'll only elaborate on the implementation of the audio module.

Audio implementation
Audio module can be divided into audio management and audio business logic. Audio management is loading, playback, recycling resources and other functions, the audio business logic mainly in the correct state should play what kind of audio. Place the entire audio management module in the Android service completely separate from the business logic. The audio module involves the following classes and interfaces:

    • Audioservice: Audio server inherits Android Service
    • Iaudioservice: Audio Abstraction interface contains events such as playback, pauses, etc.
    • Mediaplayerholder: Holding MediaPlayer management MediaPlayer life Period
    • Iaudioflow: Provides audio logic for different training content
    • Audioserviceimpl: Realizing Iaudioservice

The basic use process is to first return the Iaudioservice implementation class to Iaudioflow using the binding Audioservice Onbind method, Iaudioflow holds the Iaudioservice implementation and loads the training audio then for Itrainercontroller use. An audio tuning level queue is maintained in the Audioserviceimpl.
The above mentioned that because audio is not in a file all need to connect them when used to form a piece of audio. With priority queues combined with MediaPlayer playback complete, the callback can combine multiple audio combinations to form the desired audio. As audio plays more and more MediaPlayer recycling is particularly heavy
In the Audioserviceimpl also have the MediaPlayer recovery and utilization function. The implementation of this function is handled through Mediaplayerholder, Obtaining Mediaplayerholder by Mediaplayerholder static Get method if there are free mediaplayerholder in the Recycle pool
Create a new one, also call Mediaplayerholder recycle to recycle after an audio playback is complete.

Module integration
To reduce the need for consolidation between dependent modules, provide a management or help class, create a new trainerhelper that creates a module implementation class that contains a mode enumeration to enumerate the training patterns.

public class Trainerhelper {public

  enum mode{test, training, EXAM}

  private static mode mode;

  public static void SetMode (mode m) {
    mode = m;
  }

  public static Itrainercontroller Createpresenter (Itrainerview view, Bundle Createargs) {return
    new Trainerpresenter (View,createargs);
  }

  public static Itrainermodel Createtrainermodel (Itrainercontroller Controller) {return
    = new Defaulttrainermodel ( bundle);;
  }

  public static Iaudioflow Createtraineraudioflow (Itrainercontroller Controller) {return
    new Defaultaudioflow ( Controller);
  }



Summarize
Successful design and architecture can reduce the amount of work time, and the use of interfaces allows developers to focus more on functional implementations while isolating dependencies between modules. The next time the product manager changes the demand or the whole training mode, I can handle it calmly.

Because of my limited level, if there is a mistake, please understand.

Related Article

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.