码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式对比,运用,总结

时间:2014-12-26 14:44:40      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:java   单例模式   dcl   内部类单例模式   数据库单例模式   

相关优秀文章:http://blog.csdn.net/beyond0525/article/details/22794221

http://www.360doc.com/content/11/0810/12/1542811_139352888.shtml 

                         http://zz563143188.iteye.com/blog/1847029

  http://www.blogjava.net/ITdavid/archive/2008/01/22/176939.html

http://www.cnblogs.com/yidianfeng/archive/2011/11/07/2239246.html

http://spiritfrog.iteye.com/blog/214986


代码更改比较:

//单例模式
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;
	}

	// 连接数据库
	public SQLiteDatabase getConnection() {
		SQLiteDatabase sqLiteDatabase = null;
		try {
			sqLiteDatabase = new DatabaseHelper(context, DatabaseHelper.DBNAME)
					.getReadableDatabase();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sqLiteDatabase;
	}
	
	public static void createTable(SQLiteDatabase sqLiteDatabase){
		sqLiteDatabase.execSQL("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(SQLiteDatabase sqLiteDatabase){
		sqLiteDatabase.execSQL("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),shareurl varchar(100),title varchar(100),time varchar(100))");
	}

	/**
	 * 创建上传服务器表uploadTable
	 * mid-类型 stream-ftp服务器上文件地址  uid-上传用户id
	 * */
	public static void creatUploadTable(SQLiteDatabase sqLiteDatabase){
		sqLiteDatabase.execSQL("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.execSQL(sql, info);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != database) {
				database.close();
			}
		}
	}	
	

更改后代码:

//单例模式 5种模式,推荐使用静态内部内使用方式
public class Dao {
	//上下文用来连接数据库
	private static Context context;
	//数据库库管理器
	private static DatabaseHelper mdatebaseHelper;
	//数据库
	private static SQLiteDatabase mdatabase;
	/** 
	 * 内部类实现单例模式 
	 * 延迟加载,减少内存开销 
	 * 方法中Singleton 有一个静态内部类SingletonHolder,内部类在外部加载的时候并不会加载,在有在调用getInstance才回加载。
	 * 另外SingletonHolder类使用Private修饰以确保外部类不能访问。
	 * 优点:延迟加载,线程安全(java中class加载时互斥的),也减少了内存消耗
	 */  
	private static class DaoHolder{
		private static Dao dao = new Dao();
	}

	/** 
	 * 私有的构造函数  
	 * 延迟初始化
	 */   
	private Dao() {
		//延迟初始化全局上下文,初始化连接数据库 省去每次调用方法去连接一次服务器
		mdatebaseHelper = new DatabaseHelper(context, DatabaseHelper.DBNAME);
		mdatabase = mdatebaseHelper.getWritableDatabase();
	}

	public static  Dao getInstance(Context context) {
		Dao.context = context;
		return DaoHolder.dao;
	}

	// 连接,并打开数据库(以读写的方式打开数据库)
	public void getConnection() {
		SQLiteDatabase sqLiteDatabase = null;
		try {
			mdatebaseHelper = new DatabaseHelper(context, DatabaseHelper.DBNAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 连接,并打开数据库(以读写的方式打开数据库)
	public static void open(){
		mdatabase = mdatebaseHelper.getReadableDatabase();
	} 

	public static void createTable(){
		if(!mdatabase.isOpen()){
			open();
		}
		mdatabase.execSQL("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.execSQL("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),shareurl varchar(100),title varchar(100),time varchar(100))");
		mdatabase.close();
	}

	public synchronized int isHasInfors(String clickId, String clickType) {
		open();
		int id = -1;
		Cursor cursor = null;
		try {
			cursor = mdatabase.query("priaseTable", null,
					"clickId=? AND clickType=?", new String[] { clickId,
					clickType }, null, null, 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;
	}


单例模式对比,运用,总结

标签:java   单例模式   dcl   内部类单例模式   数据库单例模式   

原文地址:http://blog.csdn.net/zqs62761130/article/details/42170067

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!