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

In reply to Re: Re: Re: Re: Re: change object class during runtime by Masem
in thread change object class during runtime by busunsl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.