Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello Monks,

I would love to hear -yet again- your opinions. I have now decided to use SQLite for my program and want to use this step to also clean up my currently slightly messy data structure.

I have decided to create database source classes for every table, that do the database communication part, as well as object classes, that actually hold the data. All Object classes and source classes have a base class.
Finally there is a source access class, which returns arrays containing the objects, or can match them by my keys.

What I am looking for is a good idea on how to realize the singleton.
As demonstrated in http://c2.com/cgi/wiki?PerlSingleton, there are a couple of ways to create singleton by basically setting the "bless" function to a scalar and just returning this whenever it is required again. Having done a lot of work on updating all these data structures whenever some icky bits in the database changed on C++, I wanted to use the power of Perl to try and get the singleton definition out of the subclasses or any access classes and right into the base database source class.
I am now thinking of a good way to do this. The basic scheme I use for my objects is based on Damian Conway's OO Perl. I plan to use the the singleton technique
package Singleton; my $singleton; sub new { my $class = shift; $singleton ||= bless {}, $class; }
within a subclass. Do I miss out any problems when I just replace $singleton by a hash which includes the classname:
package SingletonBase; my $singleton; sub new { my $class = shift; $singleton{ $class } ||= bless {}, $class; }
The actual base class would look something like this:
# encapsulated class data { my %_singleton; my %_attr_data = ( _dbd => [ undef, 'r/w' ], _dbname => [ undef, 'r/w' ], _dbpass => [ undef, 'r/w' ], _dbserver => [ undef, 'r/w' ] ); my $_count = 0; sub _get { my ( $class ) = @_; return $_singleton{ $class } if ( $defined( _singleton{ $class } ) + ); return 0; } sub _create { my ( $self ) = @_; my $_singleton{ ref($self) } = $self; } sub _accessible { my ( $self, $attr, $mode ) = @_; $_attr_data{$attr}[1] =~ /$mode/; } sub _default_for { my ( $self, $attr ) = @_; $_attr_data{$attr}[0]; } sub _standard_keys { keys %_attr_data } sub _count { my ( $self ) = @_; ++$_count; $self->{ "_id" } = $_count; } } sub new { my ( $caller, %arg ) = @_; my $caller_is_obj = ref( $caller ); my $class = $caller_is_obj || $caller; return _get( $class ) if ( _get( $class ) ); my $self = bless {}, $class; $self->_create(); foreach my $attrname ( $self->_standard_keys() ) { my ( $argname ) = ( $attrname =~ /^_(.*)/ ); if ( exists $arg{ $argname } ) { $self->{ $attrname } = $arg{ $argname } } elsif ( $caller_is_obj ) { $self->{ $attrname } = $caller->{ $attrname } } else { $self->{ $attrname } = $self->_default_for( $attrname ) } } $self->_count(); return $self; }
I have added the functions _get, _create and %_singleton in the closure as well as changed the lines above and below the bless statement. Do I miss anything out? Are there any big setbacks? It seems too easy to me. Would it for some reason be a lot better to go via getInstance methods? Thank you for all help. PerlingTheUK

In reply to Singletons and Inheritance by PerlingTheUK

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2024-04-18 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found