I'm building an object pool and am find it really tedious to regularly reference a factory or and object pools to handle objects. Instead I'm considering building the pool directly into the object's class.
Abstract Object Class:
class Object(ABC): _pool = [] _max_pool_size = 10 def __new__(cls, *args, **kwargs): if cls._pool: instance = cls._pool.pop() instance.__init__(*args, **kwargs) return instance else: return super().__new__(cls) @classmethod def release(cls, instance): if len(cls._pool) < cls._max_pool_size: cls._pool.append(instance) Implementation:
@dataclass class FooObject(Object): n: int foo_1 = FooObject(1) FooObject.release(foo_1) But when researching the ObjectPool design pattern it seems more common to separate the Pool, Objects, and use a Factory. Why is that and what problem would I run into with this method?