'What is a "free object"?
Source: https://refactoring.guru/design-patterns/factory-method
I was wondering what the exact definition of a "free object" was in below context, and what free objects in general meant.
Context
Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.
You often experience this need when dealing with large, resource-intensive objects such as database connections, file systems, and network resources.
Let’s think about what has to be done to reuse an existing object:
- First, you need to create some storage to keep track of all of the created objects. When someone requests an object, the program should look for a free object inside that pool. … and then return it to the client code. If there are no free objects, the program should create a new one (and add it to the pool). That’s a lot of code! And it must all be put into a single place so that you don’t pollute the program with duplicate code.
Solution 1:[1]
Free objects are objects of pool that was returned by user into pool or objects located inside pool.
What is pool?
As wiki says about pool:
In computer science, a pool is a collection of resources that are kept[clarification needed] ready to use, rather than acquired on use and released[clarification needed] afterwards. In this context, resources can refer to system resources such as file handles, which are external to a process, or internal resources such as objects. A pool client requests a resource from the pool and performs desired operations on the returned resource. When the client finishes its use of the resource, it is returned to the pool rather than released and lost.
You can see an example of source code of ObjectPool<T> of .NET Core here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | StepUp |
