Content deleted Content added
→Wrapping resources in objects: fopen() resembles more a constructor than a factory |
Fgnievinski (talk | contribs) |
||
(7 intermediate revisions by 7 users not shown) | |||
Line 1:
{{short description|Software design pattern in which resources held by objects can be explicitly released}}
{{redirect|Dispose|the music album
{{Refimprove|date=February 2013}}
In [[object-oriented programming]], the '''dispose pattern''' is a [[design pattern (computer science)|design pattern]] for [[resource management (computing)|resource management]]. In this pattern, a [[system resource|resource]] is held by an [[object (computing)|object]], and released by calling a conventional [[method (computer science)|method]] – usually called <code>close</code>, <code>dispose</code>, <code>free</code>, <code>release</code>
The dispose pattern is primarily used in languages whose [[runtime environment]] have [[automatic garbage collection]] (see motivation below).
Line 13 ⟶ 15:
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 [[Constructor_(object-oriented_programming)|constructor]]), 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>
<syntaxhighlight lang="c">
Line 32 ⟶ 34:
=== Prompt release ===
The fundamental problem that
If a resource is unlimited or effectively unlimited, and no explicit finalization is necessary, it is not important to release it, and in fact short-lived programs often do not explicitly release resources: due to short run time, they are unlikely to exhaust resources, and they rely on the [[runtime system]] or [[operating system]] to do any finalization.
Line 95 ⟶ 97:
To make the safe use of the dispose pattern less verbose, several languages have some kind of built-in support for resources held and released in the same [[Block (programming)|block of code]].
The [[C Sharp (programming language)|C#]] language features the <code>using</code> statement
<syntaxhighlight lang="csharp">
Line 120 ⟶ 122:
</syntaxhighlight>
Similarly, the [[Python (programming language)|Python]] language has a <code>with</code> statement that can be used to similar effect with a ''context manager'' object. The ''context manager protocol'' requires implementing <code>__enter__</code> and <code>__exit__</code> methods which get automatically called by the <code>with</code> statement construct, to prevent duplication of code that would otherwise occur with the <code>try</code>/<code>finally</code> pattern.<ref>{{cite web |author=[[Guido van Rossum]], Nick Coghlan |date=
<syntaxhighlight lang="python">
with resource_context_manager() as resource:
Line 149 ⟶ 151:
Disposal in the presence of inheritance and composition of objects that hold resources have analogous problems to destruction/finalization (via destructors or finalizers). Further, since the dispose pattern usually does not have language support for this, [[boilerplate code]] is necessary. Firstly, if a derived class overrides a <code>dispose</code> method in the base class, the overriding method in the derived class generally needs to call the <code>dispose</code> method in the base class, in order to properly release resources held in the base. Secondly, if an object has a "has a" relationship with another object that holds a resource (i.e., if an object indirectly uses a resource through another object that directly uses a resource), should the indirectly using object be disposable? This corresponds to whether the relationship is ''owning'' ([[object composition]]) or ''viewing'' ([[object aggregation]]), or even just ''communicating'' ([[association (object-oriented programming)|association]]), and both conventions are found (indirect user is responsible for the resource or is not responsible). If the indirect use is responsible for the resource, it must be disposable, and dispose the owned objects when it is disposed (analogous to destroying or finalizing owned objects).
Composition (owning) provides [[Encapsulation (computer programming)|encapsulation]] (only the object that is used needs to be tracked), but at the cost of considerable complexity when there are further relationships between objects, while aggregation (viewing) is considerably simpler, at the cost of lacking encapsulation. In [[.NET Framework|.NET]], convention is to only have direct user of resources be responsible: "You should implement IDisposable only if your type uses unmanaged resources directly."<ref name="idisposable">{{cite web |url=https://
== See also ==
|