In object orientation, which I am assuming to be the case, the implementation of a method may depend on data (fields) of a given object. It may also depend on other objects, which there involves collaborations, but then it gets a little more complicated. Let’s see the simplest cases.
For example, a method getNomeCompleto()
may depend on the fields primeiroNome
and sobrenome
of an object Pessoa
(its implementation may be, for example return primeiroNome + " " + sobrenome;
). It is just an example, not necessarily good but illustrative, of method implementation that depends on fields of an object. And the fields primeiroNome
and sobrenome
belong to whom? To a Pessoa
.
Thus, it is advisable that the method be placed in the class that has the data it needs to function.
If this is not possible, then you can put it in a utility class, but instead of creating a utility class that "aggregates" miscellaneous and disconnected utility methods, you can create utility class centered on the data types that class represents.
Let’s see each of the examples cited.
function get_qtd_dia_mes(pDT_BASE date) return number;
It depends on a date. Ideally it should belong to a type Mês
. By example it should belong to the type DT_BASE
(that is to say, meuDtBase.getQtdDiasNoMes()
), if it is not possible then it should belong to a class DateUtil
/DateHelper
(for example).
function get_numero_aleatorio return number;
It depends on an RNG (random number generator). It should belong to a type Random
(that is to say, Random.getNextNumber()
), if it is not possible then it should belong to a class RandomUtil
/RandomHelper
.
function get_nr_matricula_fake(pNR_MATRICULA_ORIGINAL number) return number;
Depends on NR_MATRICULA_ORIGINAL
which may be a specific type or belong to a "matriculable type" (Estudante
for example). If it is not possible to belong to one of these types then it should belong to the Util
/Helper
correspondent.
The principle of One Responsibility has to do with cohesion, and it is in this spirit that I leave the above suggestions.
controversial subject: in my view the class of utilities is to have utilities and is its main reason, now can also fall if you start this class to do everything in your project ... You can put the class or a minimal example, edit your question?
– novic
I put a minimal example. Thank you!
– Valdinei