ObjectA calling ObjectB

Alan Gauld alan.gauld at btinternet.com
Sun Dec 28 10:06:52 CET 2003


On Sun, 28 Dec 2003 00:23:14 GMT, MidasOneTwo at hotmail.nospam.com
(Midas) wrote:
> Part 1)  Create 10 new assorted objects
> Part 2)  Link up the objects, somehow, so they can communicate with each other directly, 
> like parts of an electrical circuit.

Sounds like you need a list or dictionary full of objects.

> Part 3)  Call the first object, which calls another, etc. like an electrical circuit.

That's a pretty unusual electrical circuit, but I think I see
what you mean...

You want a calling sequence that can be changed. That could be
another list or dictionary mapping inputs to responses.

> To change the circuit, I would only change part 2 and perhaps part 3.

In which case to change the circuit you only need to change the
calling sequences.

> For example, ObjectX could have an input, for a string, and two outputs.
> If the input is "TurnOnS", it calls ObjectY.Input3
> If the input is "TurnOnT", it calls ObjectZ.Input7

So far it looks more like a list of functions than a list of
objects. 

> ObjectX would call either, ObjectY.Input3 or ObjectZ.Input7, 
> because it was connected that way in part 2.

You can either hard code Object X or have object X hold the tewo
lists I mentioned above. When you intialise ObjectX you need to
pass in the data to populate the lists.

The other, arguabluy more OO solution, would be to have the
inputs as message objects which know which objects to call. Thus
when you pass the "TurnOnS" message it has embedded within it a
reference to ObjectY.Input3 and a method called doit() or
somesuch. Then when object X receives the message it performs
some sanity/security checks then X tells the message to doit().
Probably a better solution would be to have the message objects
hold the sequence information but ObjectX hold the objects. X
then applies the sequence to its own objects.

That way when a message needs a new sequence you make the change
to the message that is affected. This encapsulates the change in
a much more logical way.

Lots of possibilities. Few of them actually requiring that you
pass around object and method names explicitly.

Alan g.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Python-list mailing list