Simple Singleton

xiaoxiao2021-03-06  21

When doing Java's Singleton, don't forget to handle Clone methods!

Example (taken from a unknown English document)

Public Class SingletonObject

{

Private singletonobject ()

{

// no code req'd

}

Public static singletonobject () {if (ref == null) // it's ok, we can call this constructor ref = new singleletonobject (); returnif;}

Public Object Clone () throws clonenotsupportedException {throw new clonenotsupportedException (); // That'll Teach 'em} private static singletonobject ref;

}

转载请注明原文地址:https://www.9cbs.com/read-40154.html

New Post(0)