Singleton Mode Comparison, application, and summary

Source: Internet
Author: User

Singleton Mode Comparison, application, and summary

 

 

Comparison of code changes:

 

// Singleton mode public class Dao {private static Dao dao = null; private Context context; private Dao (Context contex) {this. context = contex;} public static Dao getInstance (Context context) {if (dao = null) {dao = new Dao (context. getApplicationContext ();} return dao;} // connect to the public SQLiteDatabase getConnection () {SQLiteDatabase sqLiteDatabase = null; try {sqLiteDatabase = new DatabaseHelper (context, DatabaseHel Per. DBNAME ). getReadableDatabase ();} catch (Exception e) {e. printStackTrace ();} return sqLiteDatabase;} public static void createTable (SQLiteDatabase export cSQL (create table if not exists priaseTable (id integer primary key autoincrement, clickId varchar (100 ), + clickType varchar (100), clickCount INTEGER, nativeClick INTEGER);} public static void createRecordTable (SQLiteDatabas E sqlitedatabase‑sqlitedatabase.exe cSQL (create table if not exists playRecordTable (id integer primary key autoincrement, playid varchar (100), + playType varchar (100), path varchar (100 ), adpath varchar (100), apic varchar (100), URL varchar (100), title varchar (100), time varchar (100 )));} /*** create an upload server table uploadTable * mid-type stream-ftp Server File address uid-upload user ID **/public static void creatUploadTable (SQLiteDataba Se sqlitedatabase1_sqlitedatabase.exe cSQL (create table if not exists uploadTable (id integer primary key autoincrement +, uid varchar (100), mid varchar (100), title varchar (100 ), description varchar (255) +, stream varchar (100), time varchar (100);} @ SuppressLint (SimpleDateFormat) public synchronized void insertUploadTable (FtploadInfo ftploadInfo) {SQLiteDatabase database = getConnection (); try {String SQL = Insert into uploadTable (+ uid, mid, title, description, stream, time) values (?,?,?,?,?,?); Date date = new Date (); SimpleDateFormat simpleDateFormat = new SimpleDateFormat (); String time = simpleDateFormat. format (date); Object [] info = {ftploadInfo.getUid(),ftploadInfo.getMid(),ftploadInfo.getName(),ftploadInfo.getDesc(),ftploadInfo.getStream(),time};database.exe cSQL (SQL, info);} catch (Exception e) {e. printStackTrace ();} finally {if (null! = Database) {database. close ();}}}

Code after modification:

 

 

// Singleton mode: 5 modes. It is recommended to use the public class Dao in static mode {// Context to connect to the database private static context Context; // database manager private static DatabaseHelper mdatebaseHelper; // database private static SQLiteDatabase mdatabase;/*** internal class implementation Singleton mode * Delayed Loading, in the reduce memory overhead * method, Singleton has a static internal class SingletonHolder. The internal class is not loaded when it is loaded externally. It is reloaded only when getInstance is called. * In addition, The SingletonHolder class uses Private modification to ensure that the external class cannot be accessed. * Advantages: delayed loading and thread security (mutually exclusive during class loading in java ), it also reduces memory consumption */private static class DaoHolder {private static Dao dao = new Dao ();}/*** private constructor * Delayed initialization */private Dao () {// delays initializing the global context. initializing the connected database saves the trouble of connecting to the server mdatebaseHelper = new DatabaseHelper (context, DatabaseHelper. DBNAME); mdatabase = mdatebaseHelper. getWritableDatabase ();} public static Dao getInstance (Context context) {Dao. context = context; return DaoHolder. dao ;} // Connection, and open the database (open the database in read/write mode) public void getConnection () {SQLiteDatabase sqLiteDatabase = null; try {mdatebaseHelper = new DatabaseHelper (context, DatabaseHelper. DBNAME);} catch (Exception e) {e. printStackTrace () ;}// connection, and open the database (open the database in read/write mode) public static void open () {mdatabase = mdatebaseHelper. getReadableDatabase ();} public static void createTable () {if (! Export cSQL (create table if not exists priaseTable (id integer primary key autoincrement, clickId varchar (100), + clickType varchar (100), clickCount INTEGER, nativeClick INTEGER); mdatabase. close ();} public static void createRecordTable () {if (! Mdatabase.isOpen()){open();}mdatabase.exe cSQL (create table if not exists playRecordTable (id integer primary key autoincrement, playid varchar (100), + playType varchar (100), path varchar (100 ), adpath varchar (100), apic varchar (100), URL varchar (100), title varchar (100), time varchar (100); mdatabase. close ();} public synchronized int isHasInfors (String clickId, String clickType) {open (); int id =-1; Curso R cursor = null; try {cursor = mdatabase. query (priaseTable, null, clickId =? AND clickType = ?, New String [] {clickId, clickType}, null); if (cursor. moveToFirst () {id = cursor. getInt (0) ;}} catch (Exception e) {e. printStackTrace ();} finally {if (null! = Mdatabase) {mdatabase. close () ;}if (null! = Cursor) {cursor. close () ;}} return id ;}


 

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.