出典(authority):フリー百科事典『ウィキペディア(Wikipedia)』「2013/10/31 05:12:24」(JST)
This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (February 2013) |
In computer programming, the dispose pattern is a design pattern which is used to handle resource cleanup in runtime environments that use automatic garbage collection. The fundamental problem that the dispose pattern aims to solve is that, because objects in a garbage-collected environment have finalizers rather than destructors, there is no guarantee that an object will be destroyed at any deterministic point in time. The dispose pattern works around this by giving an object a method (usually called Dispose
or similar) which frees any resources the object is holding onto.
Many garbage-collected languages offer language constructs to avoid having to call the dispose method explicitly in many situations. These language constructs leads to results similar to what is obtained with the Resource Acquisition Is Initialization (RAII) idiom in languages with deterministic memory management (like e.g. C++).
It is very common to write code similar to the listing below when using resources that might throw exceptions in garbage-collected languages:
Resource resource = null; try { // Attempt to acquire the resource. resource = getResource(); // Perform actions with the resource. ... } finally { // Resource might not been acquired, or already freed if (resource != null) resource.Dispose(); }
The try...finally
construct is necessary for proper exception safety, since the finally
block enables execution of cleanup logic regardless of if an exception is thrown or not in the try
block.
One disadvantage of this approach is that it requires the programmer to explicitly add cleanup code in a finally
block. This leads to code size bloat, and failure to do so will lead to resource leakage in the program.
To make the dispose pattern less verbose, several languages have some kind of built-in support for it:
The C# language features the using
statement [1] that automatically calls the Dispose
method on an object that implements the IDisposable
interface:
using (Resource resource = GetResource()) { // Perform actions with the resource. ... }
which is equal to:
Resource resource = GetResource() try { // Perform actions with the resource. ... } finally { // Resource might not been acquired, or already freed if (resource != null) ((IDisposable)resource).Dispose(); }
Similarly, the Python language has a with
statement can be used to similar effect with a context manager object. The context manager protocol implies implementing __enter__
and __exit__
methods which get automatically called by the with
statement construct to facilitate not needing exception handling clauses such as finally
to cleanup resource allocations, etc. (see PEP 343):
with resource_context_manager() as resource: # Perform actions with the resource. ... # Perform other actions where the resource is guaranteed to be deallocated. ...
The Java language introduced a new syntax called try
-with-resources in Java version 7.[2] It can be used on objects that implement the AutoCloseable interface (that defines method close()):
try ( OutputStream x = new OutputStream(...) ){ //do something with x } catch(IOException ex){ //handle exception // The resource x is automatically closed } // try
全文を閲覧するには購読必要です。 To read the full text you will need to subscribe.
リンク元 | 「arrange」「arrangement」「disposition」「disposal」「整理」 |
拡張検索 | 「predispose」 |
.