现在网站做多宽,网络推广服务费会计账务处理,郑州搜狗网站建设,做电影网站违法么设计模式: 对问题行之有效的解决方式, 其实它是一种思想.单例设计模式解决的问题:就是可以保证一个类在内存中的对象唯一性. 即单个实例.比如对于A 和 B 两个程序使用同一个配置信息对象时, A 对配置信息作出修改, B 也与之对应的更新配置信息, 即需要保证该对象的唯一性.如何保…设计模式: 对问题行之有效的解决方式, 其实它是一种思想.单例设计模式解决的问题:就是可以保证一个类在内存中的对象唯一性. 即单个实例.比如对于A 和 B 两个程序使用同一个配置信息对象时, A 对配置信息作出修改, B 也与之对应的更新配置信息, 即需要保证该对象的唯一性.如何保证对象唯一性呢?不允许其他程序用 new 创建该类对象在该类中创建一个本类实例对外提供一个方法让其他程序可以获取该对象.步骤:私有化该类构造函数通过 new 在本类中创建一个本类对象定义一个公有方法, 将创建的对象返回两种方式饿汉式: 类一加载, 对象就存在了.懒汉式: 类加载进来, 没有对象, 只有调用 getInstance 方法时, 才会创建对象.也称为 单例设计模式的延迟加载模式. 但是, 懒汉式在多线程访问时,存在安全隐患.// 第一种方式: 饿汉式class Single{Single s new Single(); // 在本类中创建一个本类对象private Single(){} // 私有化该类构造函数// 定义一个公有方法, 将创建的对象返回. 用于返回对象 s, 所以返回类型 Singlepublic Single getInstance(){return s;}}class SingleDemo{public static void main(String[] args){Single ss Single.getInstance();}}/*分析一: main 函数中, getInstance 方法调用不能使用对象调用, 只能使用类名调用.所以 Single 类中该方法需要使用 static 修饰.分析二: getInstance 方法为静态方法, 它访问的内容必须是静态的,所以对象 s 也需要静态修饰.*/// 改进class Single{private static Single s new Single();private Single(){}public static Single getInstance() // 提供该方法访问实例对象 s, 是为了对象的可控{return s;}}class SingleDemo{public static void main(String[] args){Single ss Single.getInstance();}}// 第二种方式: 懒汉式// 类加载进来, 没有对象, 只有调用 getInstance 方法时, 才会创建对象.class Single2{private static Single2 s null;private Single(){}public static Single2 getInstance(){if(snull)s new Single2();return s;}}// 示例:下列代码的输出结果class SingleDemo{public static void main(String[] args){Test t1 Test.getInstance();Test t2 Test.getInstance();t1.setNum(10);t2.setNum(20);System.out.println(t1.getNum()); // 输出 20System.out.println(t2.getNum()); // 输出 20}}class Test{private int num;private static Test t new Test();private Test(){}public static Test getInstance(){return t;}public void setNum(int num){this.num num;}public int getNum(){return num;}}// 懒汉式二:避免多线程同时调用getInstance()方法, 可以使用关键字synchronizedclass LazySingleton {private static LazySingleton instance null;private LazySingleton(){}public synchronized static LazySingleton getInstance() {if(instance null){instance new LazySingleton();}return instance;}}// 懒汉式三:为提高系统性能,对instance new LazySingleton()进行锁定class LazySingleton{private static LazySingleton instance null;private LazySingleton(){}public static LazySingleton getInstance(){if(instance null){ // 此处,有可能造成单例对象不唯一synchronized(LazySingleton.class){instance new LazySingleton();}}return instance;}}// 懒汉式四:双重检查锁定(Double-Check Locking)class LazySingleton{// 注意,此处增加修饰符 volatileprivate volatile static LazySingleton instance null;private LazySingleton(){}public static LazySingleton getInstance(){// 第一重判断if(instance null){// 锁定代码块synchronized(LazySingleton.class){// 第二重判断if(instance null){instance new LazySingleton();}}}return instance;}}//单例第三种方式: Initialization Demand Holder(IoDH)技术// 在单例类中增加一个静态(static)内部类class Singleton{private Singleton(){}// 静态类private static class HolderClass {private final static Singleton instance new Singleton();}public static Singleton getInstance() {return HolderClass.instance;}public static void main(String args[]){Singleton s1, s2;s1 Singleton.getInstance();s2 Singleton.getInstance();System.out.println(s1 s2);}}_参考资料_- [JavaSE 基础视频(毕向东)](https://www.bilibili.com/video/av3092292/#page4)- [单例模式中的双重检查](http://blog.csdn.net/chenchaofuck1/article/details/51702129)