How to remove objects created by factory template

I use the Factory pattern to create .NET class objects. I also need to make sure that all such objects must be deleted before the application terminates.

Where and how can I get rid of objects created by the Factory template? Should I post in the class in which I get the objects created by the factory?

+4
source share
2 answers

Why do you want to remove them before completing the application? Is it because they contain unmanaged resources?

If so, just implement IDisposable and do the cleanup in the Dispose method and let .Net take care of the rest.

+4
source

When your factory creates new IDisposable objects, the caller should typically dispose of such an object. A suitable template is the following:

 using (var instance = Factory.CreateInstance(someArg)) { // use the instance } 

If your factory uses some internal pool, then it is still advisable that the caller delete the object, but in this case, as soon as the instance is deleted, it should be returned to the pool. However, this design is much more complicated.

+6
source

Source: https://habr.com/ru/post/1307328/


All Articles