• 【设计模式】单例模式


    使用频率:★★★★☆

    一、什么是单例模式

    一个类只有一个全局实例

    二、补充说明

    一般把其构造方法设为私有,另外提供一个可以获取该实例的静态方法;

    由于java存在反射机制,即使是私有构造方法,也能被外部创建,所以一般的写法严格来讲不属于单例模式;(ps:可以在构造方法内加个静态flag标志判断,保证其只能创建一次)

    违背了“单一职责原则”,该类既是工厂又是产品(自己创建了自己);

    单例模式可以改造成固定大小的多例模式;

    三、角色

    只有一个角色,就是单例;

    四、Java例子

    举几个常见的实用的例子

    1、在类加载的时候生成对象(如生成该单例对象不耗资源的情况,可以考虑使用)

    优点:线程安全;

    缺点:不能达到(单例实例在第一次被使用时构建)的效果;

    package com.pichen.dp.creationalpattern.singletonpattern.implement1;
    
    public class MyPrinter {
        private static  MyPrinter myPrinter = new MyPrinter();
        private MyPrinter(){
            System.out.println("implements1: created a MyPrint instance.");
        }
        public static MyPrinter getInsatnce(){
            return myPrinter;
        }
        public static void testPrint(){
            MyPrinter.getInsatnce().print("hello!");
        }
        public void print(String str){
            System.out.println(str);
        }
        
    }

    2、单例实例在第一次被使用时构建(单线程环境使用)

    优点:单例实例在第一次被使用时构建;

    缺点:不加锁的话,存在线程安全问题,即使加了锁,对性能也产生了影响;

    package com.pichen.dp.creationalpattern.singletonpattern.implement2;
    
    public class MyPrinter {
        private static  MyPrinter myPrinter = null;
        private MyPrinter(){
            System.out.println("implements2: created a MyPrint instance.");
        }
        public static synchronized MyPrinter getInsatnce(){
            if(null == myPrinter){
                myPrinter = new MyPrinter();
            }
            return myPrinter;
        }
        public static void testPrint(){
            System.out.println("hello!");
        }
        public void print(String str){
            System.out.println(str);
        }
    }

    3、静态内部类(推荐使用)

    优点:线程安全;单例实例在第一次被使用时构建;

    缺点:暂无发现

    package com.pichen.dp.creationalpattern.singletonpattern.implement3;
    
    public class MyPrinter {
    
        private static class MyPrinterHolder {
            private static MyPrinter instance = new MyPrinter();
        }
        private MyPrinter(){
            System.out.println("implements3: created a MyPrint instance.");
        }
        public static  MyPrinter getInsatnce(){
            return MyPrinterHolder.instance;
        } 
        
        public static void testPrint(){
            System.out.println("hello!");
        }
        
        public void print(String str){
            System.out.println(str);
        }
        
    }

    写个测试类如下,当我们没有显示的调用getInsatnce方法的时候,是不会生成单例对象的;

    package com.pichen.dp.creationalpattern.singletonpattern.implement3;
    
    import com.pichen.dp.creationalpattern.singletonpattern.implement3.MyPrinter;
    
    public class Main {
        public static void main(String[] args) {
    //        MyPrinter p = MyPrinter.getInsatnce();
    //        p.print("hello world.");
            MyPrinter.testPrint();
        }
    }

     打印结果:

    hello!
  • 相关阅读:
    JavaScript
    关于setInterval()你所不知道的地方
    JavaScript面向对象和原型函数
    GET和POST有什么区别?及为什么网上的多数答案都是错的
    10个最常见的 HTML5 面试题及答案
    Ajax我选择这样入门
    前端应当了解的Web缓存知识
    JavaScript
    Linux rhel7 下MySQL5.7.18详细安装文档
    思科交换机配置DHCP的四个方面
  • 原文地址:https://www.cnblogs.com/chenpi/p/5168886.html
Copyright © 2020-2023  润新知