|
| 1 | +/* |
| 2 | + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). |
| 3 | + * |
| 4 | + * The MIT License |
| 5 | + * Copyright © 2014-2022 Ilkka Seppälä |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package com.iluwatar.singleton; |
| 26 | + |
| 27 | +/** |
| 28 | + * <p>Bill Pugh Singleton Implementation.</p> |
| 29 | + * |
| 30 | + * <p>This implementation of the singleton design pattern takes advantage of the |
| 31 | + * Java memory model's guarantees about class initialization. Each class is |
| 32 | + * initialized only once, when it is first used. If the class hasn't been used |
| 33 | + * yet, it won't be loaded into memory, and no memory will be allocated for |
| 34 | + * a static instance. This makes the singleton instance lazy-loaded and thread-safe.</p> |
| 35 | + * |
| 36 | + |
| 37 | + */ |
| 38 | +public final class BillPughImplementation { |
| 39 | + |
| 40 | + /** |
| 41 | + * Private constructor to prevent instantiation from outside the class. |
| 42 | + */ |
| 43 | + private BillPughImplementation() { |
| 44 | + // private constructor |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * The InstanceHolder is a static inner class and it holds the Singleton instance. |
| 49 | + * It is not loaded into memory until the getInstance() method is called. |
| 50 | + */ |
| 51 | + private static class InstanceHolder { |
| 52 | + /** |
| 53 | + * Singleton instance of the class. |
| 54 | + */ |
| 55 | + private static BillPughImplementation instance = new BillPughImplementation(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Public accessor for the singleton instance. |
| 60 | + * |
| 61 | + * <p> |
| 62 | + * When this method is called, the InstanceHolder is loaded into memory |
| 63 | + * and creates the Singleton instance. This method provides a global access point |
| 64 | + * for the singleton instance. |
| 65 | + * </p> |
| 66 | + * |
| 67 | + * @return an instance of the class. |
| 68 | + */ |
| 69 | + // global access point |
| 70 | + public static BillPughImplementation getInstance() { |
| 71 | + return InstanceHolder.instance; |
| 72 | + } |
| 73 | +} |
0 commit comments