Android设计模式之单例模式

一、实例:

public class Staff {


    public void work() {

        //干活

    }

}

public class CEO extends Staff {


    private static final CEO mCeo = new CEO();


    private CEO() {

    }


    public static CEO getCeo() {

        return mCeo;

    }


    @Override

    public void work() {

        //管理VP

    }

}

public class Company {


    private List<Staff> allStaffs = new ArrayList<>();


    public void addStaff(Staff per) {

        allStaffs.add(per);

    }


    public void showAllStaffs() {

        for (Staff per : allStaffs) {

            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Staff:" + per.toString());

        }

    }


}

测试:

public class TestSingleton extends InstrumentationTestCase {


    public void testRun() {

        Company cp = new Company();

        Staff ceo1 = CEO.getCeo();

        Staff ceo2 = CEO.getCeo();

        cp.addStaff(ceo1);

        cp.addStaff(ceo2);

        Staff vp1 = new VP();

        Staff vp2 = new VP();

        Staff staff1 = new Staff();

        Staff staff2 = new Staff();

        Staff staff3 = new Staff();


        cp.addStaff(vp1);

        cp.addStaff(vp2);

        cp.addStaff(staff1);

        cp.addStaff(staff2);

        cp.addStaff(staff3);


        cp.showAllStaffs();


    }


}

1.懒汉模式:

public class Singleton {


    private static Singleton instance;


    private Singleton() {

    }


    public static synchronized Singleton getInstance() {

        if (instance == null) {

            instance = new Singleton();

        }

        return instance;

    }


}

2.DCL模式:

public class Singleton2 {


    private static Singleton2 sInstance = null;


    private Singleton2() {

    }


    public void doSomething() {

        System.out.println("so  sth.");

    }


    public static Singleton2 getInstance() {

        if (sInstance == null) {

            synchronized (Singleton2.class) {

                if (sInstance == null) {

                    sInstance = new Singleton2();

                }

            }

        }

        return sInstance;

    }


}

3.静态内部类模式:

public class Singleton3 {

    private Singleton3() {

    }


    public static Singleton3 getInstance() {

        return SingletonHolder.sInstance;

    }


    private static class SingletonHolder {

        private static final Singleton3 sInstance = new Singleton3();

    }


    private Object readResolve() {

        return SingletonHolder.sInstance;

    }


}

4.枚举模式:

public enum SingletonEnum {

    INSTANCE;


    public void doSomething() {

        System.out.println(">>>>>>>>>>>>>>>>do.sth");

    }

}

5.容器实现单利模式:

public class SingletonManager {


    private static Map<String, Object> objMap = new HashMap<>();


    private SingletonManager() {

    }


    public static void registerService(String key, Object instance) {

        if (!objMap.containsKey(key)) {

            objMap.put(key, instance);

        }

    }


    public static Object getService(String key) {

        return objMap.get(key);

    }


}


你可能感兴趣的:(Android设计模式之单例模式)