Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet94/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   90   91   92   93   94   95   96   97   ...   245
Bog'liq
Java 17 Recipes

 Solution 2
First, create an enum and declare a single element named INSTANCE within it. Next, 
declare other fields within the enum that you can use to store the values that are required 
for use by your application. The following enum represents a singleton with the same 
abilities as solution 1.
import java.util.ArrayList;
import java.util.List;
public enum StatisticsSingleton {
INSTANCE;
private List teams = new ArrayList ();
/**
* @return the teams
*/
public List getTeams() {
return teams;
}
Chapter 5 ObjeCt-Oriented java


157
/**
* @param teams the teams to set
*/
public void setTeams(List teams) {
this.teams = teams;
}
}
The main class is:
public static void main(String[] args){
StatisticsSingleton stats = StatisticsSingleton.INSTANCE;
System.out.println("Adding objects to the list using stats object");
List mylist = stats.getTeams();
mylist.add("One");
mylist.add("Two");
System.out.println("Reading objects from the list using stats2 
object");
StatisticsSingleton stats2 = StatisticsSingleton.INSTANCE;
List mylist2 = stats2.getTeams();
for(Object name : mylist2){
System.out.println(name.toString());
}
The output is:
Adding objects to the list using stats object
Reading objects from the list using stats2 object
One
Two
Note there is a test class within the recipe05_03 package that you can use to 
work with the enum singleton solution.
Chapter 5 ObjeCt-Oriented java


158
 How It Works
The singleton pattern creates classes that cannot be instantiated by any other class. 
This can be useful when you only want one instance of a class to be used for the entire 
application. The singleton pattern can be applied to a class by following three steps. 
First, make the constructor of the class private so that no outside class can instantiate it. 
Next, define a private static volatile field representing an instance of the class. The 
volatile keyword guarantees each thread uses the same instance. Create an instance 
of the class and assign it to the field. In the solution to this recipe, the class name is 
Statistics, and the field definition is as follows.
private static volatile Statistics instance = new Statistics();
Lastly, implement an accessor method called getInstance() that simply returns the 
instance field. The following code demonstrates such an accessor method.
public static Statistics getInstance(){
return instance;
}
To use the singleton from another class, call the singleton’s getInstance() method. 
This returns an instance of the class. The following code shows an example of another 
class obtaining an instance to the Statistics singleton defined in solution 1 to 
this recipe.
Statistics statistics = Statistics.getInstance();
List teams = statistics.getTeams();
Any class that calls the getInstance() method obtains the same instance. 
Therefore, the fields contained within the singleton have the same value for every call to 
getInstance() within the entire application.
What happens if the singleton is serialized and then deserialized? This situation may 
cause another object instance to be returned on deserialization. To prevent this issue 
from occurring, be sure to implement the readResolve() method, as demonstrated in 
solution 1. This method is called when the object is deserialized, and simply returning 
the instance ensures that another instance is not generated.
Solution 2 demonstrates a different way to create a singleton, using a Java enum 
rather than a class. Using this approach can be beneficial because an enum provides 
serialization, prohibits multiple instantiations, and allows you to work with code more 
Chapter 5 ObjeCt-Oriented java


159
concisely to implement the enum singleton, create an enum and declare an INSTANCE 
element. This is a static constant that returns an instance of the enum to classes that 
reference it. You can then add elements to the enum that can be used by other classes 
within the application to store values.
As with any programming solution, there is more than one way to do things. Some 
believe that the standard singleton pattern demonstrated in solution 1 is not the most 
desirable solution. Others do not like the enum solution for different reasons. Although 
you may find that one works better than the other in certain circumstances, both work.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   90   91   92   93   94   95   96   97   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling