views:

109

answers:

4

I'm working in Delphi (and Delphi terminology), but my question is language neutral.

Suppose you have a class, TClient, which sends messages to another class, TFacade. I assume TClient instances would have a private reference variable of type TFacade. A method somewhere in TClient would create an instance of TFacade and assign it to this private variable. The TClient client class would need to use the TFacade class.

Meanwhile, TFacade works with other classes (call them WorkerClass1, WorkerClass2, and WorkerClass3). Presumably TFacade instances do the same thing--they have private WorkerClass variables, and create instances of the WorkerClasses (perhaps in the TFacade constructor). TFacade classes need to use the WorkerClass units.

So how does information get from the WorkerClasses back to TClient? If TClient is a form, for example, it might receive user input which is used by TFacade--and, in turn, used by the WorkerClasses. After WorkerClasses have manipulated or used information, however, they need to communicate information back to TClient (i.e., output information will be displayed in the TClient form).

I hope this is clear. Basically I'm just asking if Class1 calls a method in Class2, and Class2 calls a method in Class3, how does Class3 send information back to Class1? Since I am a newbie, don't be afraid of being too simple or too specific in your response. I can handle it :-)

Thanks, as always -- Al C.

A: 

One possible way is to use callbacks, see http://en.wikipedia.org/wiki/Callback_(computer_science).

lothar
A: 

Information can be passed in function parameters, and it can be returned via the return value of the function.

John Saunders
+2  A: 

I hope this is clear. Basically I'm just asking if Class1 calls a method in Class2, and Class2 calls a method in Class3, how does Class3 send information back to Class1?

It doesn't, as that too tightly couples the claases in your design.

Instead, Class3 communicates only with Class2. This way, Class1 needs to know nothing about Class3, and vice versa. This way, you can swap out Class1 and Class3 without having to change anything except Class2.

There are other, more advanced idioms, for further reducing coupling, but this is enough for you to start with.

tpdi
+1  A: 

You have many options to choose from, with trade-offs. Some options may not be "good" depending on what you want to achieve:

  • Use function parameters and return values to move data bits around.
  • Use reference/out parameters to collect data.
  • Use callback functions/delegates.
  • Use events (if supported by language).
  • Use one common instance where all interesting information is gathered.
  • Let class1 implement stable interface, which can be used by class3.
  • Use central storage like common memory, file, database etc.
  • Check design patterns.
  • Check idioms in platform.
isntn