Dispose pattern: Difference between revisions

Content deleted Content added
Libnoon (talk | contribs)
Wrapping resources in objects: fopen() resembles more a constructor than a factory
Libnoon (talk | contribs)
The idea of freeing resources is not specific to the Dispose pattern
Line 1:
{{redirect|Dispose|the album by The Plot In You|Dispose (album)|other uses|Disposal (disambiguation)}}
{{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>, ordepending on the similarlanguage – 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).
Line 32:
 
=== Prompt release ===
The fundamental problem that thefreeing dispose patternresources aims to solve is that resources are expensive (for example, there may be a limit on the number of open files), and thus should be released promptly. Further, some finalization work is oftensometimes needed, particularly for I/O, such as flushing buffers to ensure that all data is actually written.
 
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.