Dispose pattern

Dispose pattern

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—a guarantee that is necessary for some other resource management patterns, such as Resource Acquisition Is Initialization. The dispose pattern works around this by giving an object a method (usually called Dispose() or something similar) which frees any resources the object is holding onto. Unless the language offers an alternative, this method must be manually invoked by clients that use the object once they are finished with it.

Examples

Fundamentally, the pattern looks like this from the caller's point-of-view:

// Acquire the resource.
Resource resource = null;
try {
    resource = getResource();
    // Perform actions with the resource.
    ...
} finally {
    if (resource != null) {
        // Free the resource.
        resource.Dispose();
    }
}

The try...finally construct is necessary for proper exception safety—if the code that uses the resource throws an exception, the resource will not be freed unless the Dispose() call is in a finally block. Also, the check against null is necessary if the object representing the resource is not directly instantiated, but rather is obtained from some other source that could return null.

To make this pattern less verbose, several languages have some kind of built-in support for it. For example, C# features the using statement, which automatically and safely calls the Dispose() method on an object that implements the IDisposable interface:

using (Resource resource = GetResource())
{
    // Perform actions with the resource.
    ...
}

Python's with statement can be used to similar effect:

with get_resource() as resource:
    # Perform actions with the resource.
    ...

See also


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Dispose — is design pattern which is used to handle resource clean up in systems which use garbage collection. See also * Finalizer * Object lifetime * Destructor (computer science) …   Wikipedia

  • Finalizer — In object oriented programming languages that use garbage collection, a finalizer is a special method that is executed when an object is garbage collected. It is similar in function to a destructor. In less technical terms, a finalizer is a piece …   Wikipedia

  • Visual Basic .NET — Paradigm(s) Structured, imperative, object oriented and declarative Appeared in 2001 Designed by Micro …   Wikipedia

  • Agriculture and Food Supplies — ▪ 2007 Introduction Bird flu reached Europe and Africa, and concerns over BSE continued to disrupt trade in beef. An international vault for seeds was under construction on an Arctic island. Stocks of important food fish species were reported… …   Universalium

  • property law — Introduction       principles, policies, and rules by which disputes over property are to be resolved and by which property transactions may be structured. What distinguishes property law from other kinds of law is that property law deals with… …   Universalium

  • ECONOMIC HISTORY — This article is arranged according to the following outline: first temple period exile and restoration second temple period talmudic era muslim middle ages medieval christendom economic doctrines early modern period sephardim and ashkenazim… …   Encyclopedia of Judaism

  • Germany — /jerr meuh nee/, n. a republic in central Europe: after World War II divided into four zones, British, French, U.S., and Soviet, and in 1949 into East Germany and West Germany; East and West Germany were reunited in 1990. 84,068,216; 137,852 sq.… …   Universalium

  • china — /chuy neuh/, n. 1. a translucent ceramic material, biscuit fired at a high temperature, its glaze fired at a low temperature. 2. any porcelain ware. 3. plates, cups, saucers, etc., collectively. 4. figurines made of porcelain or ceramic material …   Universalium

  • China — /chuy neuh/, n. 1. People s Republic of, a country in E Asia. 1,221,591,778; 3,691,502 sq. mi. (9,560,990 sq. km). Cap.: Beijing. 2. Republic of. Also called Nationalist China. a republic consisting mainly of the island of Taiwan off the SE coast …   Universalium

  • Business and Industry Review — ▪ 1999 Introduction Overview        Annual Average Rates of Growth of Manufacturing Output, 1980 97, Table Pattern of Output, 1994 97, Table Index Numbers of Production, Employment, and Productivity in Manufacturing Industries, Table (For Annual… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”