How do I restrict object creation not more than 3 in Java class?

后端 未结 8 1722
礼貌的吻别
礼貌的吻别 2020-12-10 18:25

How do I restrict object creation not more than 3 in Java class?

Can you give me an idea of how I can to do it?

8条回答
  •  轮回少年
    2020-12-10 19:25

    Factory pattern is the way to go. Or a static counter can be used. Need to careful about thread safety.

提交回复
热议问题