Dispose pattern: Difference between revisions

Content deleted Content added
Libnoon (talk | contribs)
Only open files have a file descriptor.
Libnoon (talk | contribs)
Wrapping resources in objects: fopen() resembles more a constructor than a factory
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>[[C file input/output#fopen|fopen]]</code> (in object-oriented terms, a [[factory methodConstructor_(object-oriented_programming)|factoryconstructor]]), which acquires the resource and returns a pointer to it; the resource is released by calling <code>[[C file input/output#fclose|fclose]]</code> on a pointer to the <code>FILE</code> object<ref>{{man|bd|stdio.h|SUS}}</ref>. In code:
 
<syntaxhighlight lang="c">