zoukankan      html  css  js  c++  java
  • The Java Enum: A Singleton Pattern [reproduced]

    The singleton pattern restricts the instantiation of a class to one object. In Java, to enforce this, the best approach is to use an enum. This great idea comes straight from the book Effective Java by Joshua Bloch. If you don't have it in your library, get it. One of the best Java books to date.

    There are a few reasons why one would use an enum as a singleton in Java:

        -Serialization for free.
        -Guaranteed one instance (Cannot instantiate more then one enum even through reflection.)
        -Thread safe

    /**
     * Example of a Java Singleton.
     * It is suggested to use an enum as a singleton. The Class
     * cannot be instantiated more then once, specifically when
     * using reflection.
     *
     * @author keaplogik
     */
    public enum AnimalHelperSingleton {

        INSTANCE;

        private AnimalHelperSingleton(){

        }

        public Animal[] buildAnimalList(){
            final Animal[] animals = new Animal[10];

            animals[0] = new SimpleAnimal(Animal.AnimalClass.MAMMAL,
                    "Dog", true, Color.GRAY);
            animals[1] = new SimpleAnimal(Animal.AnimalClass.MAMMAL,
                    "Cat", true, Color.YELLOW);
            animals[2] = new SimpleAnimal(Animal.AnimalClass.AMPHIBIAN,
                    "Frog", true, Color.GREEN);
            animals[3] = new SimpleAnimal(Animal.AnimalClass.BIRD,
                    "Crow", true, Color.BLACK);
            animals[4] = new SimpleAnimal(Animal.AnimalClass.BIRD,
                    "Cardinal", true, Color.RED);
            animals[5] = new SimpleAnimal(Animal.AnimalClass.ARTHROPOD,
                    "Mantis", false, Color.GREEN);
            animals[6] = new SimpleAnimal(Animal.AnimalClass.ARTHROPOD,
                    "Spider", false, Color.ORANGE);
            animals[7] = new SimpleAnimal(Animal.AnimalClass.MAMMAL,
                    "Tiger", true, Color.ORANGE);
            animals[8] = new SimpleAnimal(Animal.AnimalClass.MAMMAL,
                    "Bear", true, Color.BLACK);
            animals[9] = new SimpleAnimal(Animal.AnimalClass.BIRD,
                    "Owl", true, Color.BLACK);

            return animals;
        }

    }

    And in use:

    //Call singleton to build the animal list.
    Animal[] animals = AnimalHelperSingleton.INSTANCE.buildAnimalList();

    From:

    http://keaplogik.blogspot.sg/2013/12/the-java-enum-singleton-pattern.html

  • 相关阅读:
    Python自动化运维之20、HTML
    Python自动化运维之18、Python操作 MySQL、pymysql、SQLAchemy
    Python自动化运维之17、Python操作 Memcache、Redis、RabbitMQ
    Python自动化运维之16、线程、进程、协程、queue队列
    Python自动化运维之15、网络编程之socket、socketserver、select、twisted
    Python自动化运维之14、设计模式
    Python自动化运维之13、异常处理及反射(__import__,getattr,hasattr,setattr)
    浏览器学习笔记-11 settimeout
    浏览器学习笔记-10 页面中的一些优化
    浏览器学习笔记--09 事件循环
  • 原文地址:https://www.cnblogs.com/pugang/p/5780504.html
Copyright © 2011-2022 走看看