OOP: Share objects between classes

H

hobosalesman

I've divided my code into classes, and I use objects as much as
possible to make the code maintainable and so I can reuse code. I have
modules and objects that need to be used throughout my relatively large
program, from several different packages/classes. FOr instance I have a
handful of fully encapsulated classes with get/set methods to access
instance variables and all. Each of these objects needs to interact
with my database, and obviously I can't have 100 different database
connections for every object. So I want to use inheritance so that all
classes that need to use the database inherit methods for doing so from
a database parent.

But then I also have a class for logging actions that every object
needs to use. And each object needs to use a configuration class that
reads from the config file. And on, and on. I don't know how to set
this up, multiple inheritance? I want to keep it simple, maybe I can
have one parent class that creates instances of the log/db/conf classes
and allows every other class to inherit functions that allow access to
these log/db/conf objects.

I don't want some hack with global variables, I want namespacews as
clear as possible. Maintainability, code re-use, clarity are all more
important than speed for this program. Any advice is welcome.
 
X

xhoster

I've divided my code into classes, and I use objects as much as
possible to make the code maintainable and so I can reuse code. I have
modules and objects that need to be used throughout my relatively large
program, from several different packages/classes. FOr instance I have a
handful of fully encapsulated classes with get/set methods to access
instance variables and all. Each of these objects needs to interact
with my database,

Is there only one database? If not, how does each object (or class?) know
which database it is supposed to use?
and obviously I can't have 100 different database
connections for every object. So I want to use inheritance so that all
classes that need to use the database inherit methods for doing so from
a database parent.

How about inheriting methods from a noninstantiable project parent class
instead?
But then I also have a class for logging actions that every object
needs to use. And each object needs to use a configuration class that
reads from the config file. And on, and on. I don't know how to set
this up, multiple inheritance? I want to keep it simple, maybe I can
have one parent class that creates instances of the log/db/conf classes
and allows every other class to inherit functions that allow access to
these log/db/conf objects.

Rather than having the parent class create the objects, how about just
having the parent code store the objects it is given by some init code?

package Parent;
my $dbh;
sub set_dbh {
die "Can't change once set!" if defined $dbh;
$dbh=$_[1];
die "Not a valid database handler" unless $dbh->ping();
};
sub get_dbh { $dbh};

my $logger;
sub set_logger {
#....


I don't see how this fundamentally different from just using
"some hack with global variables", but then again I haven't drunk the
cool-aid.
I don't want some hack with global variables, I want namespacews as
clear as possible. Maintainability, code re-use, clarity are all more
important than speed for this program. Any advice is welcome.

Xho
 
M

Mark Clements

I've divided my code into classes, and I use objects as much as
possible to make the code maintainable and so I can reuse code. I have
modules and objects that need to be used throughout my relatively large
program, from several different packages/classes. FOr instance I have a
handful of fully encapsulated classes with get/set methods to access
instance variables and all. Each of these objects needs to interact
with my database, and obviously I can't have 100 different database
connections for every object. So I want to use inheritance so that all
classes that need to use the database inherit methods for doing so from
a database parent.

But then I also have a class for logging actions that every object
needs to use. And each object needs to use a configuration class that
reads from the config file. And on, and on. I don't know how to set
this up, multiple inheritance? I want to keep it simple, maybe I can
have one parent class that creates instances of the log/db/conf classes
and allows every other class to inherit functions that allow access to
these log/db/conf objects.

I don't want some hack with global variables, I want namespacews as
clear as possible. Maintainability, code re-use, clarity are all more
important than speed for this program. Any advice is welcome.

Check out the singleton design pattern. Perl examples at

http://perldesignpatterns.com/?SingletonPattern

but there is plenty more literature available.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top