Semi global variables

D

Don French

I have an application where multiple objects are created processing
different sets of data based upon a configuration specified. Each
object uses several other objects but those objects must use the
configuration initially set up in the first primary object. Each of
the primary objects maintain their own configuration.

How can you have variables defined in the primary object that are
visible to the other objects it creates?

Don French
 
I

Intransition

I have an application where multiple objects are created processing
different sets of data based upon a configuration specified. Each
object uses several other objects but those objects must use the
configuration initially set up in the first primary object. =A0Each of
the primary objects maintain their own configuration.

How can you have variables defined in the primary object that are
visible to the other objects it creates?

Well, we would need to know more to be sure. But one possibility is to
pass a reference to the primary object to all subsequent objects.
 
D

Don French

Well, we would need to know more to be sure. But one possibility is to
pass a reference to the primary object to all subsequent objects.

That is an option but I hate to pass around that to every object.

class Driver
def initialize
live_data_handler = Handler.new('live')
test_data_handler = Handler.new('test')
end

def handle_command
do
command, data = queuemanager.wait
case command
when 'processlive'
live_data_handler.handle(data)
when 'processtest'
live_data_handler.handle(data)
end
until command == 'quit'
end
end

class Handler
def initialize(config)
@config = getconfig(config)
@otherconfiginfo = other
end

def handle(data)
lots_of_different_objects_created(data)
end
end


this is an example of the structure. same handler code but config
determines what to do and how.

Don French
 
R

Robert Klemme

2010/4/26 Don French said:
That is an option but I hate to pass around that to every object.

You might hate it initially but you'll be glad when coming back to the
code months later for maintenance. Using global variables of any form
makes code a) harder to read and understand and b) less flexible
(because you are bound to that exact global variable). Explicit
passing of data is the better alternative most of the time. You can
still organize it in a way that it's not too much typing overhead.
class Driver
=A0def initialize
=A0 =A0live_data_handler =3D Handler.new('live')
=A0 =A0test_data_handler =3D Handler.new('test')
=A0end

=A0def handle_command
=A0 =A0 do
=A0 =A0 =A0 command, data =3D queuemanager.wait
=A0 =A0 =A0 case command
=A0 =A0 =A0 =A0 when 'processlive'
=A0 =A0 =A0 =A0 =A0 live_data_handler.handle(data)
=A0 =A0 =A0 =A0 when 'processtest'
=A0 =A0 =A0 =A0 =A0 live_data_handler.handle(data)

Shouldn't that read

test_data_handler.handle(data)

?
=A0 =A0 =A0 end
=A0 =A0until command =3D=3D 'quit'
=A0end
end

class Handler
=A0def initialize(config)
=A0 @config =3D getconfig(config)
=A0 @otherconfiginfo =3D other

Where do you get that "other" from?
=A0end

=A0def handle(data)
=A0 =A0lots_of_different_objects_created(data)
=A0end
end


this is an example of the structure. =A0same handler code but config
determines what to do and how.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
D

Don French

2010/4/26 Don French <[email protected]>:





You might hate it initially but you'll be glad when coming back to the
code months later for maintenance.  Using global variables of any form
makes code a) harder to read and understand and b) less flexible
(because you are bound to that exact global variable).  Explicit
passing of data is the better alternative most of the time.  You can
still organize it in a way that it's not too much typing overhead.



Shouldn't that read

test_data_handler.handle(data)

?



Where do you get that "other" from?




Kind regards

robert

Thanks. Looks like I will just create a configuration object and pass
it around.

Other is just a place holder instead of showing more of the code.
There are lots of config things that are initialized.

Don Frenc
 

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

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top