Can this Java singleton get rebuilt repeatedly in WebSphere 6?

后端 未结 10 936
清酒与你
清酒与你 2021-02-13 13:26

I\'m trying to track down an issue in our system and the following code worries me. The following occurs in our doPost() method in the primary servlet (names have been changed

10条回答
  •  名媛妹妹
    2021-02-13 13:41

    It's not mandatory for the single instance to be final (it's not a good idea at all indeed, because this will avoid you to switch it's behaviour using other patterns).

    In the code below you can see how it gets instantiated only once (first time you call the constructor)

    package date;

    import java.util.Date;

    public class USDateFactory implements DateFactory { private static USDateFactory usdatefactory = null;

    private USDateFactory () { }
    
    public static USDateFactory getUsdatefactory() {
        if(usdatefactory==null) {
            usdatefactory = new USDateFactory();
        }
        return usdatefactory;
    }
    
    public String getTextDate (Date date) {
        return null;
    }
    
    public NumericalDate getNumericalDate (Date date) {
        return null;
    }
    

    }

提交回复
热议问题