Dispose pattern: Difference between revisions

Content deleted Content added
Minor typo fix
Correct C file handling description and code, add reference.
Line 13:
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 (computer science)|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 <code>FILE</code> type (confusingly called "[[file handle]]s": 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 <code>[[openC (systemfile call)input/output#fopen|openfopen]]</code> (in object-oriented terms, a [[factory method|factory]]), which acquires the resource, and returns a pointer to it; the resource is released by calling <code>[[closeC (systemfile call)input/output#fclose|closefclose]]</code> on a pointer to the <code>FILE</code> object<ref>{{man|bd|stdio.h|SUS}}</ref>. In code:
 
<source lang="c">
FILE *f = openfopen(filename, mode);
// Do something with f.
closefclose(f);
</source>
 
Note that <code>closefclose</code> is a function with a <code>FILE *</code> parameter. In object-oriented programming, this is instead an [[instance method]] on a file object, as in Python:
 
<source lang="python">