Question: Can you give an example in code to show the difference between weak and strong dependency?
Answer: First, let's review the definitions of weak and strong dependence. From slide 5 of the lecture,
Now, recall that in slide 9, Folder has no dependence on StandardOutReporter. Compactor calls a static report method in the StandardOutReporter class directly, so there is no need for Folder to refer to StandardOutReporter in any way. In slide 12, the situation is different. Here, instances of two possible classes (StandardOutReporter, and JTextComponentReporter) may be passed from Session, through Folder, and on to Compactor where the instances are ultimately used. From slide 11, the code in Main that starts this is
s.download(new StandardOutReporter(), ...);
The download method of Session would be defined as follows.
public void download (Reporter r1, ...) {
Folder f1;
// There would be some code here to create or access a folder, f1
// Then, the download method for the folder would be called with r1.
f1.download(r1, ...);
}
Similarly, the download method of Folder would be defined as follows.
public void download (Reporter r1, ...) {
Compactor c1;
// There would be some code here to create or access a compactor, c1
// Then, the download method for the compactor would be called with r1.
c1.download(r1, ...);
}
Note that the download method in Folder does not actually access any fields in r1, it just passes it through to the download method for c1. Therefore, the dependence on Reporter is weak.
Suppose now that there were situations where Folder had to report errors. For example, suppose that the download method for Folder is as follows.
public void download (Reporter r1, ...) {
Compactor c1;
// There would be some code here to create or access a compactor, c1
// Suppose no compactor can't be found or created. A check here for this
// would use r1 to report an error.
if (c1 == null)
r1.report("No compactor found for download operation.");
else
c1.download(r1, ...);
}
Now, the dependence of Folder on Reporter is strong because Folder uses the report
method of r1.
Question: Can you show, in code form, the difference between slide 15 and slide 17?
Answer: In slide 15, the previously described dependence of Folder on Reporter is avoided by
using a static field. The code for this (from slide 14) is
public class StaticReporter {
static Reporter r;
static void setReporter(Reporter r) {
this.r = r; }
static void report(String msg) {
r.report(msg); }
}
This would have to be initialized in the Session initialization code:
StaticReporter.setReporter(new StandardOutReporter());
Now, in slide 17, a static field is not used. Instead, a single Reporter is associated with each Session instance. Thus, the Session class would have a reporter field that would be initialized in the Session initialization code:
reporter = new StandardOutReporter();The question now is how Compactor can access this reporter, since it is no longer being passed through. The answer is that Session provides a report method
public void report(String msg) {
reporter.report(msg);
}
Compactor gets a pointer to a Session object rather than to a Reporter object. This is better in the
sense that Session is more abstract than Reporter. Compactor is reporting a problem back to its
parent Session and letting the Session worry about how to deal with it. Of course, in order for Compactor
to call the method in Session, the pointer to the
Session object does need to be passed through Folder somehow, probably in the same manner that Reporter
was passed through previously.