单例设计模式是指:保证整个程序中只有一个实例。
确保某个类有且只要一个对象的场景,避免产生过多对象消耗过多资源,或者某个类型的对象有且只有一个。
实现形式有很多种,只要保证有单个实例就行,单例又分为懒汉式和饿汉式两种。
实现形式一:静态内部类
public class Single{
private Single() {
}
public static SinglegetInstance(){
return SingleHolder.mInstance;
}
public static class SingleHolder{
private static volatile Single mInstance = new Single();
}
}
实现形式二:容器管理
public class Single{
private static Map<String,Object> mSingleMap = new HashMap<>();
static {
mSingleMap.put("activity_manager",new Single());
}
private Singleton() {
}
public static Object getService(String serviceName){
return mSingleMap.get(serviceName);
}
}
实现形式三:饿汉式
public class Single{
private static Single mInstance = new Single();
private Single(){
}
public static Single getInstance(){
return mInstance;
}
}
实现形式四:饿汉式
public class Single{
private static Single mInstance;
private Single(){
}
public static synchronized Single getInstance(){
if(mInstance == null){
mInstance = new Single();
}
return mInstance;
}
}
实现方式四存在一些问题,使用同步锁(DCL)
public class Single3 {
private static Single3 mInstance;
private Single3() {
}
public static Single3 getInstance() {
if (mInstance == null) {
synchronized (Single3.class) {
if (mInstance == null) {
mInstance = new Single3();
}
}
}
return mInstance;
}
}
多线程并发的问题,如果多线程调用还是会存在多个实例
public class Single3 {
private static volatile Single3 mInstance;
private Single3() {
}
public static Single3 getInstance() {
if (mInstance == null) {
synchronized (Single3.class) {
if (mInstance == null) {
mInstance = new Single3();
}
}
}
return mInstance;
}
}
volatile 关键字的作用
防止重排序
线程可见性 - 某一个线程改了公用对象(变量),短时间内另一个线程可能是不可见的,因为每一个线程都有自己的缓存区(线程工作区)
Activity管理类实现
public class AppManager {
//Stack集合增加删除快
private static Stack<Activity> activityStack;
private static volatile AppManager instance;
private AppManager(){
if(activityStack==null){
activityStack=new Stack<Activity>();
}
}
/**
* 单一实例
*/
public static AppManager getAppManager(){
if (instance == null) {
synchronized (AppManager.class) {
if (instance == null) {
instance = new AppManager();
}
}
}
return instance;
}
/**
* 添加Activity到堆栈
*/
public void addActivity(Activity activity){
activityStack.add(activity);
}
/**
* 获取当前Activity(堆栈中最后一个压入的)
*/
public Activity currentActivity(){
Activity activity=activityStack.lastElement();
return activity;
}
/**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public void finishActivity(){
Activity activity=activityStack.lastElement();
finishActivity(activity);
}
/**
* 结束指定的Activity
*/
public void finishActivity(Activity finishActivity){
int size = activityStack.size();
for (int i = 0; i < size; i++) {
Activity activity = activityStack.get(i);
if (activity == finishActivity) {
activityStack.remove(i);
activity.finish();
i--;
size--;
}
}
}
/**
* 结束指定类名的Activity
*/
public void finishActivity(Class<?> cls){
int size = activityStack.size();
for (int i = 0; i < size; i++) {
Activity activity = activityStack.get(i);
if (activity.getClass().getCanonicalName().equals(cls.getCanonicalName())) {
activityStack.remove(i);
activity.finish();
i--;
size--;
}
}
}
/**
* 结束所有Activity
*/
public void finishAllActivity(){
int size = activityStack.size();
for (int i = 0; i < size; i++) {
if (null != activityStack.get(i)) {
activityStack.remove(i);
activityStack.get(i).finish();
i--;
size--;
}
}
activityStack.clear();
}
/**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.restartPackage(context.getPackageName());
System.exit(0);
} catch (Exception e) {
}
}
}