The singleton design pattern is one of the easier ones to un

The singleton design pattern is one of the easier ones to understand (at least initially) and implement in Java. One use of the singleton design pattern is to hold configuration parameters for an application. That is what we will be doing with this assignment. 1. Implement (in Java) a singleton class in accordance with the following class diagram:

Solution


public class ConfigParameters {

   String fileName = \"CFile.dat\";
   int numberOfUsers = 27;
   boolean soundOn = false;
   private static ConfigParameters instance = new ConfigParameters(); //this cannot be null in a sigleton as it is the only instance that is ever created of the class
  
   private ConfigParameters() {
   }

   public String getFileName() {
       return fileName;
   }

   public void setFileName(String fileName) {
       this.fileName = fileName;
   }

   public int getNumberOfUsers() {
       return numberOfUsers;
   }

   public void setNumberOfUsers(int numberOfUsers) {
       this.numberOfUsers = numberOfUsers;
   }

   public boolean SoundIsOn() {
       return soundOn;
   }

   public void setSoundOn() {
       this.soundOn = true;
   }
   public void setSoundOff() {
       this.soundOn = false;
   }
  
   public static ConfigParameters Instance(){
       return instance;
   }
  
  
}

 The singleton design pattern is one of the easier ones to understand (at least initially) and implement in Java. One use of the singleton design pattern is to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site