|
| 1 | +/* |
| 2 | + The MIT License (MIT) |
| 3 | +
|
| 4 | + Copyright (c) 2015 Massimo Caliman |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + SOFTWARE. |
| 23 | + */ |
| 24 | +package io.github.mcaliman.patterns.singletion; |
| 25 | + |
| 26 | +/** |
| 27 | + * Se l'applicazione ha sempre necessità di una istanza, o se il costo di |
| 28 | + * creazione della stessa non è troppo grande in termini di tempo e risorse, si |
| 29 | + * può utilizzare questa modalità che crea sempre un istanza. |
| 30 | + * <p> |
| 31 | + * L'Eager (impaziente!) Initialization Singleton è un singleton impaziente di inizializzare |
| 32 | + * la sua unica istanza possibile. |
| 33 | + * |
| 34 | + * @author Massimo Caliman |
| 35 | + */ |
| 36 | +public class EagerInitializationSingleton { |
| 37 | + |
| 38 | + /** |
| 39 | + * Istanza creata all'atto di caricamento della classe in memoria |
| 40 | + * quindi è thread-safe! Le convenzioni sono importanti |
| 41 | + * INSTANCE è scritto in maiscolo in quanto final e static. |
| 42 | + */ |
| 43 | + private final static EagerInitializationSingleton INSTANCE = new EagerInitializationSingleton(); |
| 44 | + /** |
| 45 | + * Il singleton è utile se contiene o fa qualcosa, ecco quindi |
| 46 | + * un unica proprietà (dummy) accessive solo tramite setter e getter |
| 47 | + * appositi. |
| 48 | + */ |
| 49 | + private String property = "EagerInitializationSingleton property value."; |
| 50 | + |
| 51 | + /** |
| 52 | + * Il costruttore è privato, in quanto la creazione dell'istanza deve essere |
| 53 | + * controllata. Può avvenire solo all'interno della classe Singleton. |
| 54 | + */ |
| 55 | + private EagerInitializationSingleton() { |
| 56 | + System.out.println("Istanza di EagerInitializationSingleton creata! Questo messaggio non dovrà apparire più di una volta."); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Unico punto di accesso al Singleton tramite metodo pubblico e statico. |
| 61 | + * |
| 62 | + * @return il Singleton |
| 63 | + */ |
| 64 | + public static EagerInitializationSingleton getInstance() { |
| 65 | + return INSTANCE; |
| 66 | + } |
| 67 | + |
| 68 | + public String getProperty() { |
| 69 | + return property; |
| 70 | + } |
| 71 | + |
| 72 | + public void setProperty(String property) { |
| 73 | + this.property = property; |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments