What is the pattern represented by this class called?

Asked

Viewed 227 times

1

I wonder what kind of class this is DaoDeAutenticacao, whose skeleton I wrote down. Despite the name I don’t think it’s a DAO (Data Access Object), however it is defined, and yes something else (a Data Mapper?). I’d like to know what name has this pattern, if it has one.

Its intention is not to map a single database table but a set of related tables, although the example is mapping a single one.

The Java object CentralDeAlarme populated with the database data and returned by the method autenticar() is an object of interest in the field of application. Moreover, what is called this object? It is a Data Transfer Object (DTO)?

public class DaoDeAutenticacao {

    ...

    public CentralDeAlarme autenticar(String senha) {
        CentralDeAlarme resultado = null;

        try {
            // Abre conexão com o banco

            // Monta e chama statement
            // SELECT c.id, c.fabricante, c.modelo FROM centrais c WHERE c.senha = senha;

            resultado = new CentralDeAlarme(id, fabricante, modelo);

            return resultado;

        finally {
            // fecha conexão com o banco
        }
    }
}

Using the Datodeauthentication class:

CentralDeAlarme central = daoDeAutenticacao.autenticar(senha);
if (central == null) { // autenticação falhou
    ...
    return;
}

// autenticação bem-sucedida
Logger.log("Central autenticada: " + central);
...

The Centraldealarme class:

public class CentralDeAlarme {
    private final int id;
    private final Fabricante fabricante;
    private final Modelo modelo;

    // getters
}
  • It’s certainly not DTO. Actually I’m not sure I understand the question.

  • I improved the question, please see if it became clearer.

  • The new code appears to be a DTO.

  • And the DaoDeAutenticacao, would be what?

  • I think it’s weird, but it might just be me who doesn’t know how to do those things that people do around here :)

  • So it’s not a DAO, it’s just a weird class? P

  • I don’t consider it a DAO.

  • Understood. I will have to research further then to know what is considered a DAO.

Show 3 more comments

3 answers

4


Will be a DAO?
As already mentioned, the DAO standard is used to separate Apis or data access operations (low level) from business logic (high level). Simple as that! An approach to implement this standard would be: An interface, a concrete class to implement it and a VO (Value Object), the latter being a POJO that will contain only attributes, getters, setters and nothing else.

Will be a Data Mapper?
Imagine on one side a model class (the good old POJO) and on the other side a database. In the middle, "mappers" objects move the data between the model objects and the base, keeping one independent of the other. Well, in a simple way this would be the essence of a Data Mapper. This pattern can become costly according to the complexity of the relational model.

What is called this object?
In the case of Centraldealarme, would be a DTO, or a BO or maybe a VO...

DTO (Data Transfer Object) - Simple object to transfer data back and forth in the application.

BO (Business Object) - Encapsulates business logic for an object. Contains only the properties of the business object, contains only the business methods or both.

VO (Value Object) - Basically used to display data in the presentation layer (there are controversies). In fact, the Centraldealarme object seems to me a DTO.

What is called the pattern represented by this class?
Something similar to a DAO or with particularities of one. As we know, Patterns are not written in rock. It goes that this approach, within a specific context, represents an evolution towards something better. It goes like...

  • Hello Allan. As you said, in Java a DAO needs interface and "interchangeable" implementations with each other, because its intention is to exchange the implementation transparently for the domain objects, so it is not the case for the question code. In addition the DAO operations go more to the CRUD side, which is what the domain objects ask for. The question if it is a Data Mapper I asked by exclusion, looking at the Fowler’s Nterprise application Patterns, but I didn’t understand enough to enter a definition. Maybe it is one, anyway. And the other class proves//te a same DTO.

  • Well, as there is no point in considering possibilities without having the definition well (that is, whether it is a data mapper or not), I will give the question as answered. Thank you and show up!

  • That’s it Piovesan! In this case so specific we go by approximation and common sense. I thank you.

2

DAO ( Data Access Object ) :It is a standard for data persistence that allows you to separate business rules from database access rules. In other words, this should contain all database manipulation such as queries, insertions updates...

Business: Here you concentrate the business rule, you can call the methods of the Daos class and implement its business rule.

Entity or Value Object: These are the persistence objects, which represent the tables in an object-oriented manner. Example : User, Profile...

Data transfer Object: are data transfer objects, which can be used to make conjunctions, generate reports and etc.

0

So if the goal is to group several information from different tables one can use the concept of DTO since it is a set of data that will be transferred in an object and that do not represent a single entity. Another class that you could use that might fit into the model are POJO’s (Plain Old Java Object) are simple classes that aim only at moving information without internal methods besides getter, setters and constructor, no external interfaces, inheritances or frameworks

  • Hello William, welcome to the site. Missed answer the first question (the title) regarding the Daodeauthentication class. Another thing, the name of the pattern you described as POJO is actually the Java Bean pattern. They are also Pojos, but have wider sense.

  • Really, it’s a java bean, thanks for clarifying!

Browser other questions tagged

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