in reply to Re: Re: Re: Re: change object class during runtime
in thread change object class during runtime
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:
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 ServerConnection sub query { my $self = shift; $self->{ server }->query( @_ ); }
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re(6): change object class during runtime
by busunsl (Vicar) on Jun 19, 2001 at 17:06 UTC |