Android open-source project-Research and optimization of jamendo music player-Database Operations Based on builder Mode

Source: Internet
Author: User

[The value of a note is subdivided into even values based on the principle of second-class. This is called the basic division of the value of a note]

--- Basic five-line spectrum tutorial


For a complete application, database operations are often unavoidable. jamendo builds the entire framework of database operations based on the builder pattern. First, let's take a look at the definition of the generator mode: separates the construction of a complex object from its representation, so that different representations can be created during the same build process.

The typical structure is as follows:

It mainly involves four roles:

1) Builder: builder interface, which defines the operations of each part required to create a product object;

2) concretebuilder: a specific builder is used to create parts, assemble parts of the product object, and provide a method for obtaining the assembled product objects;

3) Director: the mentor, mainly used to call the builder interface and build the desired product object in a unified process;

4) Product: a product that represents a complex object created by the generator and contains various components.

Next, let's take a look at how to use the builder mode in jamendo to perform database operations:


1. Builder role

In jamendo, the builder role is played by databasebuilder. Because there are many product object types, generic representation is used. As shown below, two interfaces are defined in this abstract class, which is the opposite process, the product object is created by the database cursor object, and the contentvalues object suitable for database operations is constructed from the product object.

Public abstract class databasebuilder <t> {// construct the product object public abstract t build (cursor c) based on the passed cursor object; // opposite to the build method, obtain the contentvalues object public abstract contentvalues deconstruct (t) suitable for database operations from the product object );}

2. concretebuilder and product roles

In the type hierarchy view of Eclipse, we can see that there are four concretebuilder roles ,:

The following focuses on the business logic of music playback:


1) trackdatabasebuilder class and track product class

Track maintains the track information of an album, including the ID, name, duration, lyrics, data stream, score, and serial number, as shown in:

The trackdatabasebuilder class is responsible for associating the information of the track product class with the database, as shown in the Code:

/*** Audio track information database generation class **/public class trackdatabasebuilder extends databasebuilder <track> {Private Static final string track_id = "track_id "; // idprivate static final string track_name = "track_name"; // name Private Static final string track_duration = "track_duration"; // duration Private Static final string track_url = "track_url "; // Private Static final string track_stream = "track_stream"; // Private Static final string track_rating = "track_rating "; // rating Private Static final string album_track_num = "album_track_num"; // No. @ overridepublic track build (cursor query) {int columnname = query. getcolumnindex (track_name); int columnstream = query. getcolumnindex (track_stream); int columnurl = query. getcolumnindex (track_url); int columnduration = query. getcolumnindex (track_duration); int columnid = query. getcolumnindex (track_id); int columnrating = query. getcolumnindex (track_rating); int columnalbumtracknum = query. getcolumnindex (album_track_num); track = new track (); track. setduration (query. getint (columnduration); track. setid (query. getint (columnid); track. setname (query. getstring (columnname); track. setrating (query. getdouble (columnrating); track. setstream (query. getstring (columnstream); track. seturl (query. getstring (columnurl); track. setnumalbum (query. getint (columnalbumtracknum); Return track;} @ overridepublic contentvalues deconstruct (track) {contentvalues values = new contentvalues (); values. put (track_name, track. getname (); values. put (track_stream, track. getstream (); values. put (track_url, track. geturl (); values. put (track_duration, track. getduration (); values. put (track_id, track. GETID (); values. put (track_rating, track. getrating (); values. put (album_track_num, track. getnumalbum (); return values ;}}

2) albumdatabasebuilder class and album product class

The album class indicates the album object, which includes the album ID, album name, album cover, album rating, artist name, and album tracks. The class definition is as follows:

The role of the albumdatabasebuilder class is to inherit the databasebuilder class and store the main information of the above album class to the database for database operations, as shown in the following code:

/*** Album database builder class */public class albumdatabasebuilder extends databasebuilder <album> {Private Static final string album_id = "album_id "; // album idprivate static final string album_name = "album_name"; // album name Private Static final string album_image = "album_image "; // album cover Private Static final string album_rating = "album_rating"; // The album's star Private Static final string artist_name = "artist_name "; // artist name @ overridepublic album build (cursor query) {int columnartistname = query. getcolumnindex (artist_name); int columnname = query. getcolumnindex (album_name); int columnimage = query. getcolumnindex (album_image); int columnid = query. getcolumnindex (album_id); int columnrating = query. getcolumnindex (album_rating); album = new album (); album. setid (query. getint (columnid); album. setartistname (query. getstring (columnartistname); album. setname (query. getstring (columnname); album. setrating (query. getdouble (columnrating); album. setimage (query. getstring (columnimage); Return album ;}@ overridepublic contentvalues deconstruct (album) {contentvalues values = new contentvalues (); values. put (artist_name, album. getartistname (); values. put (album_image, album. getimage (); values. put (album_name, album. getname (); values. put (album_id, album. GETID (); values. put (album_rating, album. getrating (); return values ;}}

3) radiodatabasebuilder and Radio Product

The radio class indicates information related to the broadcast radio. It mainly includes the ID, name, and corresponding image ID. The Code is as follows:

The implementation of the radiodatabasebuilder class is similar to the preceding ones. You can directly view the Code:

/*** Broadcast information database generation class **/public class radiodatabasebuilder extends databasebuilder <radio> {Private Static final string radio_id = "radio_id "; // broadcast idprivate static final string radio_idstr = "radio_idstr"; // The string format of the broadcast ID is Private Static final string radio_name = "radio_name "; // broadcast name Private Static final string radio_image = "radio_image"; // broadcast image ID Private Static final string radio_date = "radio_date "; // The time when the broadcast information is saved @ overridepublic radio build (cursor query) {int columnid = query. getcolumnindex (radio_id); int columnidstr = query. getcolumnindex (radio_idstr); int columnname = query. getcolumnindex (radio_name); int columnimage = query. getcolumnindex (radio_image); Radio radio = new radio (); Radio. setid (query. getint (columnid); Radio. setidstr (query. getstring (columnidstr); Radio. setimage (query. getstring (columnimage); Radio. setname (query. getstring (columnname); return radio;} @ overridepublic contentvalues deconstruct (Radio radio) {contentvalues values = new contentvalues (); values. put (radio_id, radio. GETID (); values. put (radio_idstr, radio. getidstr (); values. put (radio_name, radio. getname (); values. put (radio_image, radio. getimage (); values. put (radio_date, system. currenttimemillis (); return values ;}}

4) downloadjobbuilder class and downloadjob product class

The downloadjob product category is not the same as the previous three product categories. It does not only contain database information, but also maintains download-related information. This article only analyzes the database-related sections, the downloaded part is analyzed in a later article. As shown in, only playlistentry in the downloadjob class is related to the database:

The playlistentry class is a simple encapsulation of the album class and the track class. It is embodied in the playlist Information Class and is defined as follows:

The downloadjobbuilder class is naturally responsible for building and anti-building playlistentruy information in downloadjob. The Code is as follows:

/*** Database generation class for download information **/public class downloadjobbuilder extends databasebuilder <downloadjob> {Private Static final string downloaded = "downloaded "; // indicates whether the download is complete (download is complete-1, download is incomplete-0) @ overridepublic downloadjob build (cursor query) {track = new trackdatabasebuilder (). build (query); album = new albumdatabasebuilder (). build (query); // encapsulated into a playlist class playlistentry pentry = new playlistentry (); pentry. setalbum (Album); pentry. settrack (track); downloadjob djob = new downloadjob (pentry, downloadhelper. getdownloadpath (), 0, jamendoapplication. getinstance (). getdownloadformat (); // query the download progress int progress = query. getint (query. getcolumnindex (downloaded); If (Progress = 1) {djob. setprogress (100);} return djob;} @ overridepublic contentvalues deconstruct (downloadjob t) {contentvalues values = new contentvalues (); values. P Utall (New trackdatabasebuilder (). deconstruct (T. getplaylistentry (). gettrack (); values. putall (New albumdatabasebuilder (). deconstruct (T. getplaylistentry (). getalbum (); values. put (downloaded, (T. getprogress () = 100 )? 1: 0); return values ;}}

Role 3 Director

The Director role calls the interfaces provided by the builder role to build the desired product role in a unified manner. In jamendo, there are two director roles that operate the database builder role, the classes are databaseimpl and downloaddatabaseimpl respectively. The class diagram is as follows:

Among them, the databaseimpl class is the mentor of the albumdatabasebuilder, trackdatabasebuilder, and radiodatabasebuilder classes;

The downloaddatabaseimpl class is the mentor of the albumdatabasebuilder, trackdatabasebuilder, and downloadjobbuilder classes.

Below is a brief introduction to some code that calls the builder corner class in databaseimpl:

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.