language-icon Old Web
English
Sign In

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. 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. For example, in C file input/output, files are represented by objects of the FILE type (confusingly called 'file handles': these are a language-level abstraction), which stores an (operating system) handle to the file (such as a file descriptor), together with auxiliary information like I/O mode (reading, writing) and position in the stream. These objects are created by calling fopen (in object-oriented terms, a factory), which acquires the resource and returns a pointer to it; the resource is released by calling fclose on a pointer to the FILE object. In code: Note that fclose is a function with a FILE * parameter. In object-oriented programming, this is instead an instance method on a file object, as in Python: This is precisely the dispose pattern, and only differs in syntax and code structure from traditional file opening and closing. Other resources can be managed in exactly the same way: being acquired in a constructor or factory, and released by an explicit close or dispose method.

[ "Humanities", "Forestry", "Programming language", "Waste management" ]
Parent Topic
Child Topic
    No Parent Topic