What is "granularity level" and how to identify it?

11

In Erich Gamma's Project Defaults - Reusable Software Solutions

  

"Designing object-oriented software is difficult, but designing reusable software   object-oriented is even more complicated. You must identify pertinent objects,   factorize them into classes at the correct level of granularity , define the interfaces of   classes, inheritance hierarchies, and establish the key relationships between them. "

I would like to know what this level of granularity is and how to identify it. For this, I ask for an example.

    
asked by anonymous 12.03.2015 / 00:50

2 answers

7

The level of granularity would be basically to define a criterion that we will adopt to separate our classes, thinking about the concept of object orientation. For example:
If we have to register an employee with the data of Name, CPF, Date of Birth and Salary; we would think at first of creating the following class Funcionário :

public class Funcionário
 {
     private String nome;
     private String cpf;
     private date datanasc;
     private double salario;

    /*restante do código*/
}

But if we are thinking about reusing this code, we can define a Pessoa class that will contain the attributes that are basic to all people:

public class Pessoa
 {
     private String nome;
     private String cpf;
     private date datanasc;

    /*restante do código*/
}

And so, our Class Funcionário would inherit from person and would only have the field Salary more.

public class Funcionário extends Pessoa
 {
     private double salario;

    /*restante do código*/
}

So the definition of granularity is what helps us define which attribute will be contained in each class, thus making our code reusable.

    
12.03.2015 / 12:39
0

Granularity can be understood as the level of division into which objects are built. To address in terms of high and low classification:

Lowgranularityisthedivisionintomanydetailsofobjects,commonlywithmanycompositionsandfewattributesperobject;highgranularityisthecoarserlevel,objectsgetmanyattributesandfewrelationships.

Imakeaparallelwiththerepresentationofinvoicesinsomecorporatesystems,maintainedwithtwoobjects:NotaFiscalNaoEmitidaandNotaFiscalEmitida.Itseemstomeagoodexamplebecauseitdealswithtwoextremesofthesamedataandisconsistentwith this brief reference :

  

Building objects at the lowest level of granularity provides optimal flexibility, but may be unacceptable in terms of performance and memory usage. (free translation)

The first has only a few fields of the invoice and refers to dozens of other entities such as Cliente , Produto , Empresa , Endereco , Imposto . It has low granularity .

Once issued and no longer modified, and for performance purposes, historical consistency, and storage simplicity, NotaFiscalEmitida has a copy of all relationship information stored in a single row in the database. The object has an absurd configuration with more than a hundred fields and no relation, but it is totally independent of any other entity and self-contained in its data. It has high granularity .

    
29.12.2017 / 13:57