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

Asked

Viewed 4,415 times

11

In the book Design Standards - Reusable object-oriented software solutions by Erich Gamma, in the first chapter introduction we have the following text:

"Designing object-oriented software is difficult, but designing reusable software object-oriented is even more complicated. You must identify relevant objects, factor 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 that level of granularity is and how to identify it.
For this, I ask for an example.

  • As it was not I who wrote the text , I leave here the link with a good example. https://www.componentsource.com/pt-br/help-support/about-us/components

  • The link says that granularity has to do with componentization, but I think they’re different things.

2 answers

7

The granularity level 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 in principle to create 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 think about reusing this code, we can define a class Pessoa 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 have only the field Salary more.

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

    /*restante do código*/
}

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

  • From my point of view the answer is 98% correct, because as I answered here http://answall.com/questions/82761/qual-a-forma-de-passar-values-para-attributed/82803?noredirect=1#Answer-82803, the person does not depend on a CPF she may have one, so it would be rather an attribute, but she herself does not manipulate it. So I would create a class CPF

  • The @Murilofechio explanation summarizes well a way to understand the degree of criteria adopted for the scope of a class, or granularity, however, it can be applied there are several strands and not only to this situation. Granularity can be seen as the level of detail of something, that is, it is much more than defining attributes.

1

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

nível de granularidade

Granularity low is the division into many details of objects, commonly with many compositions and few attributes per object; granularity high is the grossest level, objects are left with many attributes and few relations.

I make a parallel with the representation of fiscal notes in some corporate systems, maintained with two objects: NotaFiscalNaoEmitida and NotaFiscalEmitida. It seems to me a good example because it deals with two extremes of the same data and is consistent with this brief reference:

Building objects at the lowest granularity level provides optimal flexibility, but can 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 as Cliente, Produto, Empresa, Endereco, Imposto, etc. Has granularity low.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.