Let's say I need write a Wrapper for a 3rd party class which I'm not able to change.
The interface of the class looks like this
class Rewriter {
public List<Mapping> getMappings();
}
The wrapper looks like this
class RewriterSpec {
private final Rewriter rewriter;
public RewriterSpec(Rewriter rewriter) {
this.rewriter = rewriter;
}
public addMapping(Mapping m) {
rewriter.getMappings().add(m);
}
}
So from my understanding the RewriterSpec
violates the Law of Demeter because it requires structural knowledge about the Rewriter.
Now, the question is, would it be better from a design / testing point of view to just pass in the list of mappings?
class Rewriter {
public List<Mapping> getMappings();
}
The wrapper looks like this
class RewriterSpec {
private final Rewriter rewriter;
public RewriterSpec(List<Mapping> mappings) {
this.mappings = mappings;
}
public addMapping(Mapping m) {
mappings.add(m);
}
}
Is it okay to just pass the List by reference?