Thread: Dispose pattern in class derivation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Dispose pattern in class derivation

    Hello everyone,


    For the Dispose pattern, here is what MSDN mentioned,

    http://msdn2.microsoft.com/en-us/lib...5e(VS.80).aspx

    --------------------
    Implement the dispose design pattern on a base type that commonly has derived types that hold onto resources, even if the base type does not.
    --------------------

    Could anyone let me know what means "on a base type that commonly has derived types that hold onto resources" and "the base type does not"? Could anyone show some pseudo code please?


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Roughly something like this: base has no resources attached to it, but derived has, so we implement a "dispose" function (this is just to show the concept, not compilable C++ or C# code).

    Code:
    class base
    {
       ... 
    public:
       virtual dispose();
    };
    
    
    class derived: public base
    {
    private:
        someresource res;
    
    public:
       virtual dispose()  { relase(res); }
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Well, here's a quick example:

    Say you have an AbstractDataSource class that's an abstract base class whose child classes handle reading data from specific sources (local file, database, network, etc.) Those child classes may have resources such as file handles and TCP connections that we'd like to have a Dispose() method to clean up. So we put it in the base class, even though the base class itself doesn't have any resources, so that our children can override it and clean themselves up.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats and Cat,


    Question answered.

    Quote Originally Posted by Cat View Post
    Well, here's a quick example:

    Say you have an AbstractDataSource class that's an abstract base class whose child classes handle reading data from specific sources (local file, database, network, etc.) Those child classes may have resources such as file handles and TCP connections that we'd like to have a Dispose() method to clean up. So we put it in the base class, even though the base class itself doesn't have any resources, so that our children can override it and clean themselves up.

    regards,
    George

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    One question concerning Dispose:

    Is it ok the resuse "Dispose" just for Raising an Disposed-Event, so listeners can assume the object is dead and react to that - although the object never held any unmanaged resources and may still be referenced?

    Currently in my game (which is seperated into Model, View, Control) whenever something gets destroyed* (e.g. a tree or monster) i call the Dispose method, that raises an event, which causes the model to unregister that object, and usually the view also listens to that event and also removes the visual representation (if any).

    Fortunately even if what i did is a no-go, its easy to refactor


    *by destroyed i really mean that the object can be destroyed.
    When a tree first gets "destroyed" it remains in "dead" ("destroyed") state for a few seconds, before it is really removed from the model (so Disposed is called).
    signature under construction

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for sharing experience, Raven!


    Quote Originally Posted by Raven Arkadon View Post
    One question concerning Dispose:

    Is it ok the resuse "Dispose" just for Raising an Disposed-Event, so listeners can assume the object is dead and react to that - although the object never held any unmanaged resources and may still be referenced?

    Currently in my game (which is seperated into Model, View, Control) whenever something gets destroyed* (e.g. a tree or monster) i call the Dispose method, that raises an event, which causes the model to unregister that object, and usually the view also listens to that event and also removes the visual representation (if any).

    Fortunately even if what i did is a no-go, its easy to refactor


    *by destroyed i really mean that the object can be destroyed.
    When a tree first gets "destroyed" it remains in "dead" ("destroyed") state for a few seconds, before it is really removed from the model (so Disposed is called).

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM