in reply to Re: Re: change object class during runtime
in thread change object class during runtime

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
  • Comment on Re: Re: Re: change object class during runtime

Replies are listed 'Best First'.
Re: Re: Re: Re: change object class during runtime
by busunsl (Vicar) on Jun 19, 2001 at 16:07 UTC
    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.

      Ok, I see where you are heading with this problem.

      I'm going to assume that for every database type regardless, the check to see if the server is alive and responding, and the type of server can be done without knowing the database type. (This would be similar to polling port 80 and determining the web server information; it would be independant of what the web server is running).

      Create a class called ServerConnection; this doesn't inherit from anything, but give it all the functions that involve checking for the alive status and getting the database type. As a member variable, have a placeholder for your Server and related subclasses. When you actually do connect successfully to a server, determine the type and create a new Server class appropriate to that type and store it away. Provide a function in ServerConnection to return that reference such that functions on that reference can be used (*). When you need to change types later, you can delete the current Server object and recreate a new type that you want.

      (*) Not knowing how complete the functions would be in the Server type, another option would be to replicate the public functions of Server into ServerConnection, which would then simply pass the values on to it's Server object, for example:

      # in ServerConnection sub query { my $self = shift; $self->{ server }->query( @_ ); }
      This way, you need not pass the Server object around. This is probably only a good idea if you have 4 or 5 functions, anything larger than that and I would fall back onto a Server accessor function.

      In this fashion, Server only contains functions relating to querying or modifying the database, while ServerConnection contains the functions for connecting and disconnecting from the database.


      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        That would do, the only problem I see is that the object is probably known in several places.
        So a simple delete and recreate is not possible.
        One solution to that would be a reference to the object which is passed around.

        But I think I found an easy solution, see Re: change object class during runtime