in reply to change object class during runtime

You probably want to readjust this object pattern to a Factory method; that is, besides your base Server object and the various items that inherit from it, you want to create a class that could be called ServerFactory which you cannot make an instance of, but has 'static' functions that create Server objects for you (that is, the first argument to these functions is NOT the blessed ref). You can connect to the database and create the appropriate server object from these functions, and not have to change class type during run time.

One possible modification is that once you've determined the connection type, you can further call static functions within the individual database types as to keep the class specific code within the class itself.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: change object class during runtime
by busunsl (Vicar) on Jun 19, 2001 at 15:31 UTC
    This would be similar to connecting to the real server and determine the type in the main program and create the objects from the correct class.

    The problem here is that I need a working object even when it doesn't know it's type, due to the server beeing down, and report the failure to connect.

      Well, one thing you could do is have a subclass of Server called "InvalidServer" (or some better name), that would be returned by the Factory object in case the server is unreachable which you can fill with whatever error information that you need; optionally, you can stick connection error info into field in the ServerFactory; if there is a problem creating the connection, you can return undef, and then use the error fields to determine why and respond appropriately.

      Note that the Factory method is quite common as an object pattern, and while it's the same as doing stuff in main, it's considered to be much safer (namespace collisions), so there's nothing wrong with using such a method.


      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        The usage of an 'InvalidServer' just postpones the problem.
        Eventually the server object can connect to the real server and has to change it's class.

        I know that the Factory method has it's advantages.
        In fact I don't want to open the connection in the main program or in a factory but in the object itself.
        This way it's easier to add new types.