7
Assuming that a class A
use a class B
(directly or through an interface C
to uncouple A
of C
).
Making it clear that A
and B
are independent parties, i and.., the relationship between the two classes is not of obvious composition or aggregation, but there is a "weak" association in which A
uses B
(in that case, by means of the contract established by C
).
Consider the two examples (in Java):
Example 1 - C as Instance Variable:
public class A {
private C c;
// Não estou discutindo injeção através de construtores vs. getters and setters
// Apenas assuma que `c` foi inicializado de alguma maneira
public A(C c) {
this.c = c;
}
public metodo1() {
// faz algo com c
}
public metodo2() {
// faz algo com c
}
public metodo3() {
// não faz nada com c
}
}
Example 2 - C as parameter:
public class A {
public metodo1(C c) {
// faz algo com c
}
public metodo2(C c) {
// faz algo com c
}
public metodo3() {
// não faz nada com c
}
}
My question is in what situation should I get A
has an instance variable of type C
vs. when I’m supposed to pass an instance like C
as a parameter for the methods of A
?
In other words: In what situations the API exposed in Example 1 would be "better" than the API exposed in Example 2 and vice versa? What would be the reasons to support this decision?
After many years as a developer I still make this decision on the basis of feeling. I also noticed that over time the construction of the example 2 (which was rare, and readily refactored to 1 in my code) began to become more acceptable and even preferable in most situations. However, until today I have difficulty formalizing the reasons that lead me to choose one construction or another.
Part of the program more questions for the Beta
– Anthony Accioly
Personally, I consider this question a little "too broad" to mean "it is not clear what you are asking". Before reading his own answer, I thought it was a conceptual question (and I formulated the first part of the answer); after reading it, I saw that it was more about practical aspects (i.e. the "natural relationship between
A
andB
is clear, but implementing it in this way brings negative consequences") - and I formulated the second. I suggest you edit the question if possible to make it more specific.– mgibsonbr
Hello @mgibsonbr, suggestions for better question are welcome. But I’m unfortunately not working on a "concrete code" example. The problem is that, in my view, there is a slight difference in certain situations. A conceptual gap and space for opinion when deciding between associations vs passing parameters repeatedly in the API. For me an association is stronger and "continues", while the passage of parameters is weaker and "discrete" (I don’t know if it made sense).
– Anthony Accioly
Well, my main suggestion is to make it clearer on what the question is not. For example, when you write "Assuming a class
A
use a classB
" is ambiguous whether there is a relationship between the two [outside the context of the method]. If for the purposes of this question it is assumed that nay, thatA
andB
do not relate conceptually, it would be interesting to have this explicit in the question. Likewise, its main motivation for what I understand is the creation of a good API, not the modeling of entities in classes. Seen through this focus, more targeted responses can be provided.– mgibsonbr
Much better now! + 1, and I will edit my answer to make it more concise.
– mgibsonbr