How to use multiple instance mode multiton
This article mainly explains "how to use multi-case mode multiton". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian to study and learn "how to use multi-case mode multiton" together.
In the singleton pattern, we can only get one instance of each class. And in many cases, we can get a limited number of instances of this class, which are creative design patterns,
public enum FruitType { APPLE, BANANA;}public class Fruit { private String name; private Fruit(String name) { this.name = name; } private static ConcurrentHashMap fruits; static { fruits.put(APPLE,new Fruit("pinguo")); fruits.put(BANANA,new Fruit("xiangjiao")); } public static Fruit getFruit(FruitType fruitType){ return fruits.get(fruitType); }}public static void main(String[] args) { Fruit apple = Fruit.getFruit(FruitType.APPLE); Fruit banana = Fruit.getFruit(FruitType.BANANA); } Thank you for reading, the above is the content of "how to use multi-case mode multiton", after learning this article, I believe that everyone has a deeper understanding of how to use multi-case mode multiton, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!