0
object pool vs singleton vs virtual proxy
Hi In all three patterns, we have object which we reuse and I have following doubts: 1. Singleton has only one object across the application and same is used as and when required. So is the object pool. It has few objects and same is reused as and when required. What is the use of using random different object as and when required? If all are same or different object, what is the purpose of holding more objects? I understand thread pool, but do we have other scenarios where object pool is best? I am pretty much sure about singleton as it is used for preference settings, logger or database connection but what about use case of object pool? 2. virtual proxy and object pool is same only. Right? What else it do additional to one another?
2 Respostas
+ 5
Ketan Lalcheta the best way to answer this is to review what a singleton is
https://www.sololearn.com/learn/713/?ref=app
And what an object pool is
https://www.sololearn.com/learn/690/?ref=app
and virtual proxy
https://www.sololearn.com/learn/700/?ref=app
Key Difference:
Singleton: Only one instance is created and used across the entire application.
Object Pool: Multiple objects are created and managed, reused, and rotated as needed, often with a limit on how many instances are created.
Why use multiple objects in an object pool?
Performance: Some objects are costly to create (e.g., database connections, threads, etc.), and creating them on-demand can degrade performance. An object pool reduces this overhead by reusing objects.
Concurrency: In cases of high concurrency (multiple threads or requests), an object pool ensures that a limited number of objects are available, balancing performance and system load. If there are too many requests, some might have to wait for an object to become available.
+ 1
I will go through articles you shared
My initial understanding is still confusing. Why one can have object pool?
If all objects are same and does not hold any difference , what's the purpose of such class as objects are same. Why to use this pattern in practice ? Thread pool makes sense as thread is stl and do not have data members and just thread method changes.
Does this mean all object pool applications are stateless ? If it does have state, how to pull required stateful object ?