Various implementations of the singleton pattern


The main advantages of the singleton model are as follows. (1) 单例 mode提供了对唯一实例的受控访问。因为单例类封装了它的唯一实例,所以它可以严 The grid controls how and when clients access it. (2) 由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销 The ruined object singleton pattern can certainly improve the performance of the system. (3) 允许可变数目的实例。基于单例 mode我们可以进行扩展,使用与单例控制相似的方法来获 This saves system resources and solves the problem of sharing too many single instances of a single instance object, which can be detrimental to performance. Title. 2.Major disadvantages The main disadvantages of the singleton model are as follows. (1) 由于单例 mode中没有抽象层,因此单例类的扩展有很大的困难。 (2) 单例类的职责过重,在一定程度上违背了“单一职责原则”。因为单例类既充当了工厂角 color that provides factory methods while acting as a product role, containing a number of business methods that combine the creation of products and product The features themselves are integrated into one. (3) 现在很多面向对象语言(如Java、C#)的运行环境都提供了自动垃圾回收的技术,因此,如 If the instantiated shared object is not utilized for a long time, the system will consider it garbage and will automatically destroy and reclaim the resource, the next time will be re-instantiated again when utilized, which will result in the loss of shared single instance object state. 3.Applicable scenarios The singleton pattern may be considered in the following cases. (1) 系统只需要一个实例对象,如系统要求提供一个唯一的序列号生成器或资源管理器,或者 Need to consider that the resource consumption is too large and only one object is allowed to be created. (2) 客户调用类的单个实例只允许使用一个公共访问点,除了该公共访问点,不能通过其他途 path to access the instance. reflect on How can the singleton pattern be adapted so that a finite number of objects of a particular class can exist in the system, such as two or three instances? [Note: The transformed class can be called a multi-instance class. ]

Hungry man mode.

public class Esingleton {

    private Esingleton() {

    }

    private static Esingleton ins = new Esingleton();

    public static Esingleton Instance() {
        return ins;
    }


    public static void main(String[] args) {
        Esingleton esingleton = Esingleton.Instance();
        Esingleton esingleton1 = Esingleton.Instance();
        System.out.println(esingleton == esingleton1);
    }
}

Slacker mode.

public class Lsingleton {

    private Lsingleton() {

    }

    private static Lsingleton instance = null;

    public static Lsingleton Instance() {
        if (instance == null)
            instance = new Lsingleton();
        return instance;
    }


    public static void main(String[] args) {
        Lsingleton lsingleton = Lsingleton.Instance();
        Lsingleton lsingleton1 = Lsingleton.Instance();
        System.out.println(lsingleton == lsingleton1);
    }
}

IoDH(Initialization Demand Holder ) mode:

public class Singleton {

    private Singleton() {
    }

    private static class InnerClass {
        private final static Singleton instance = new Singleton();
    }

    public static Singleton Instance() {
        return InnerClass.instance;
    }

    public static void main(String[] args) {
        Singleton singleton = Singleton.Instance();
        Singleton singleton1 = Singleton.Instance();

        System.out.println(singleton == singleton1);
    }
}

Retrofitting the lazy man model under multithreading.

public class Lsingleton {

    private Lsingleton() {

    }

    /**
     *  Variables are shared for all threads
     */
    private volatile static Lsingleton instance = null;

    public static Lsingleton Instance() {
        // Double lock verification
        if (instance == null)
            synchronized (Lsingleton.class) {
                if (instance == null)
                    instance = new Lsingleton();
            }
        return instance;
    }


    public static void main(String[] args) {
        Lsingleton lsingleton = Lsingleton.Instance();
        Lsingleton lsingleton1 = Lsingleton.Instance();
        System.out.println(lsingleton == lsingleton1);

    Queue<Test> queue = new ArrayBlockingQueue<Test>(100);
        List<Thread> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(new Thread(() -> {                 
                queue.add(Test.getInstance());
            }));
        }
        for (Thread thread : list) {
            thread.start();
        }

        while (queue.size() != 100);
            System.out.println(""+JSON.toJSONString(queue));
    }
}

Recommended>>
1、Springexplanatory noteComponentRepositoryServiceControlle
2、RecommendedThe Five Commandments for Data Scientists and Engineers
3、Java Dynamic Proxy InDepth Analysis
4、Silicon Valley Watch on Big Data full version
5、Some useful websites that programmers must know

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号