import java.util.HashMap; import java.util.Map; /** * This class represents a coin. Only a single Coin * with a particular value can be created. This class * implements the multiton design pattern. * * @author Franck van Breugel */ public class Coin { private int value; /** * The unique instances. */ private static Map instance = new HashMap(); /** * Initializes the coin. */ private Coin(int value) { this.setValue(value); } /** * Returns the value of this coin. * * @return the value of this coin. */ public int getValue() { return this.value; } /** * Sets the value of this coin to the given value. * * @param value the new value of this coin. */ private void setValue(int value) { this.value = value; } /** * Returns the coin with the given value. * * @return the coin with the given value. */ public static Coin getInstance(int value) { if (!Coin.instance.containsKey(value)) { Coin.instance.put(value, new Coin(value)); } return Coin.instance.get(value); } }