Monday, September 16, 2013

Singleton Design Pattern


Singleton Design Pattern

There are some instances in the application where we have to use just one instance of a particular class.

Singleton pattern is one of the simplest design patterns in Java, which comes under creational pattern.

Important :
* Ensure unique instance by defining class final to prevent cloning.
* May be extensible by the subclass by defining subclass final.
* Make a method or a variable public or/and static.
* Access to the instance by the way you provided.
* Well control the instantiation of a class.
* Define one value shared by all instances by making it static.



Example :

1. DisplayCurrencyFormat.java

package DesignPatterns.Singleton;

import java.text.DecimalFormat;

public class DisplayCurrencyFormat {

     private static DisplayCurrencyFormat currencyFormatuniqueInstance;

     private boolean ENABLE_ROUNDING = false;
     private boolean ENABLE_DECIMAL = false;
     private boolean ENABLE_GROUPING = false;
     private String ROUNDING_TYPE = "abs";

     public static synchronized DisplayCurrencyFormat getInstance(){

           if(null == currencyFormatuniqueInstance){
                 return currencyFormatuniqueInstance = new DisplayCurrencyFormat();
           }
           return null;
    }

    public String setDisplayFormat(double value){

           if(currencyFormatuniqueInstance.isENABLE_ROUNDING()){

                 if("ceil".equalsIgnoreCase(currencyFormatuniqueInstance.
getROUNDING_TYPE())){
                        value = Math.ceil(value);
                 }
                 if("floor".equalsIgnoreCase(currencyFormatuniqueInstance.
getROUNDING_TYPE())){
                       value = Math.floor(value);
                 }
                 if("abs".equalsIgnoreCase(currencyFormatuniqueInstance.
getROUNDING_TYPE())){
                       value = Math.abs(value);
                }
          }

          String retValue;
          if(currencyFormatuniqueInstance.isENABLE_DECIMAL() && currencyFormatuniqueInstance.isENABLE_GROUPING()){
                 DecimalFormat df1 = new DecimalFormat( "#,###,###,##0.00" );
                 retValue = df1.format(value);
          }else{
                 if(currencyFormatuniqueInstance.isENABLE_DECIMAL()){
                        DecimalFormat df2 = new DecimalFormat( "#####0.00" );
                        retValue = df2.format(value);
                }else
                if(currencyFormatuniqueInstance.isENABLE_GROUPING()){
                        DecimalFormat df3 = new DecimalFormat( "#,###,###,##0" );
                        retValue = df3.format(value);
                }else{
                        retValue = String.valueOf(value);
                }
         }
         System.out.println("value | retValue --> " + value + " | " + retValue);
         return retValue;
   }

   public boolean isENABLE_ROUNDING() {
         return ENABLE_ROUNDING;
   }

   public void setENABLE_ROUNDING(boolean eNABLE_ROUNDING) {
         ENABLE_ROUNDING = eNABLE_ROUNDING;
   }

   public boolean isENABLE_DECIMAL() {
         return ENABLE_DECIMAL;
   }

   public void setENABLE_DECIMAL(boolean eNABLE_DECIMAL) {
         ENABLE_DECIMAL = eNABLE_DECIMAL;
   }

   public boolean isENABLE_GROUPING() {
         return ENABLE_GROUPING;
   }

   public void setENABLE_GROUPING(boolean eNABLE_GROUPING) {
          ENABLE_GROUPING = eNABLE_GROUPING;
   }

   public String getROUNDING_TYPE() {
          return ROUNDING_TYPE;
   }

   public void setROUNDING_TYPE(String rOUNDING_TYPE) {
          ROUNDING_TYPE = rOUNDING_TYPE;
   }
}


2. SingletonDemo.java

package DesignPatterns.Singleton;

public class SingletonDemo {

      /**
      * @param args
      */
      public static void main(String[] args) {
              SingletonDemo demo = new SingletonDemo();
              demo.execute();
      }

      private void execute(){

              DisplayCurrencyFormat currencyFormatObj = DisplayCurrencyFormat.getInstance();
              currencyFormatObj.setENABLE_ROUNDING(true);
              currencyFormatObj.setENABLE_DECIMAL(true);
              currencyFormatObj.setENABLE_GROUPING(true);
              currencyFormatObj.setROUNDING_TYPE("abs");
              currencyFormatObj.setDisplayFormat(10189.4);
      }
}


Output :

value | retValue [R-t|D-t|G-t]--> 10189.4 | 10,189.40
value | retValue [R-t|D-t|G-f]--> 10189.4 | 10189.40
value | retValue [R-t|D-f|G-t]--> 10189.4 | 10,189
value | retValue [R-t|D-f|G-f]--> 10189.4 | 10189.4
value | retValue [R-f|D-t|G-t]--> 10189.4 | 10,189.40
value | retValue [R-f|D-t|G-f]--> 10189.4 | 10189.40
value | retValue [R-f|D-f|G-t]--> 10189.4 | 10,189
value | retValue [R-f|D-f|G-f]--> 1189.4 | 10189.4

No comments:

Post a Comment