*** Welcome to piglix ***

Dispose pattern


In object-oriented programming, the dispose pattern is a design pattern for resource management. In this pattern, a resource is held by an object, and released by calling a method – usually called close, dispose, free, release, or similar – which releases any resources the object is holding onto. Many languages offer language constructs to avoid having to call the dispose method explicitly in common situations.

The dispose pattern is primarily used in languages whose runtime environment have automatic garbage collection (see motivation below), and thus may be styled as manual resource management in languages with automatic memory management.

Wrapping resources in objects is the object-oriented form of encapsulation, and underlies the dispose pattern.

Resources are typically represented by handles (abstract references), concretely usually integers, which are used to communicate with an external system that provides the resource. For example, files are provided by the operating system (specifically the file system), which in many systems represents files with a file descriptor (an integer representing the file).

These handles can be used directly, by storing the value in a variable and passing it as an argument to functions that use the resource. However, it is frequently useful to abstract from the handle itself (for example, if different operating systems represent files differently), and to store additional auxiliary data with the handle, so handles can be stored as a field in a record, along with other data; if this in an opaque data type, then this provides information hiding and the user is abstracted from the actual representation.


...
Wikipedia

...