-
Programming bind
Yo, I'm in a bind and I'm not sure how to resolve this. I'm having an issue with message passing in OOP.
There is an object foo, and it has an object bar. When I instance foo, I also need to pass information to the bar object within foo. Since it's not a good idea to do message passing like this due to very high coupling problems, it's better to create the bar instance and pass that instance into foo's constructor.
But that doesn't solve the coupling problem. Now there's no message passing, but bar is highly coupled with foo. Any change in methods of bar will require a change in foo and any other object which needs a bar object.
How do you solve this problem? It's driving me nuts.
-
I'm not sure I understand the problem.
If foo has an object bar, then naturally whenever the public interface of bar changes, foo will need to be recompiled or possibly recoded.
To decrease the coupling you can use a virtual class vbar, which has whatever features of bar foo uses, and from which bar inherits. The public interface of vbar will hopefully not change anywhere near as often as bar's will.
-
One way to do it is to make another object baz which implements the functionality of both bar and foo. That way, you have no coupling, but a new object which contains all the data it needs in order to run "alone".
-
Have foo's constructor take whatever parameters are appropriate for its level of abstraction. Should callers of foo's constructor know about foo having a bar? If yes, go ahead and pass the bar object. You may want to have it take an abstract class or interface (or the equivalent in your language) instead.