Limit instances creation of a class?

前端 未结 6 1585
暖寄归人
暖寄归人 2021-01-11 16:15

I am using C#. I have created a class which can be included in any c#.net project (desktop or web based), but I want that only 10 objects will be created in that application

6条回答
  •  离开以前
    2021-01-11 16:47

    I believe that you want some form of the multiton pattern.

    The multiton pattern is a variation on the singleton pattern, but that allows for n instances of an object. Much like how the singleton class has a static variable to hold the single instance, the multiton is often implemented with a static array or map of instances, depending on how you want to access the instances - arrays only allow for numerical access, but by using a map, you could provide String keys to your instances, making them named.

提交回复
热议问题