Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just as <code>enum</code> is recommended by <em>Effective Java 2nd Edition</em> to implement singleton, this solution also uses <code>enum</code> to implement... quadrupleton?</p> <pre><code>import java.util.*; public enum RoundRobin { EENIE, MEENIE, MINY, MO; private final static List&lt;RoundRobin&gt; values = Collections.unmodifiableList(Arrays.asList(values())); // cache it instead of creating new array every time with values() private final static int N = values.size(); private static int counter = -1; public static RoundRobin nextInstance() { counter = (counter + 1) % N; // % is the remainder operator return values.get(counter); } public static void main(String[] args) { for (int i = 0; i &lt; 10; i++) { System.out.println(RoundRobin.nextInstance()); } // EENIE, MEENIE, MINY, MO, EENIE, MEENIE, MINY, MO, ... } } </code></pre> <p>Extending this to quintupleton is self-explanatory.</p> <h3>See also</h3> <ul> <li><em>Effective Java 2nd Edition, Enforce singleton property with a private constructor or an enum type</em> <blockquote> <p>As of release 1.5., there is a third approach to implementing singletons. Simply make an enum type with one element. This approach is functionally equivalent to the public field approach, except that it is more concise, provides serialization mechanism for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, <strong>a single-element enum type is the best way to implement a singleton</strong>.</p> </blockquote></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/70689/efficient-way-to-implement-singleton-pattern-in-java">Efficient way to implement singleton pattern in Java</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload