Facade generic is it possible?

0

Good afternoon I am studying for object-oriented account, and in the middle of studies appeared various patterns and with them a lot of confusion

My question is, is it possible to have a generic FACADE. or anything to see what I'm talking about?

I was watching these videos lessons

Where it creates

  • BookFacade
  • InterfaceLivroDao
  • ADDRESS
  • BookController
  • Book (model)

In his example, it was a book registry, however, I am making a product registry, where there will be PRODUCTS and MARKS

I've already created the iDao interface using generic

package br.com.jcom.dao;

import java.util.List;

public interface IDao<T> {

    String getNomeTabela();

    int insert(T... elementos);

    int update(T... elementos);

    int delete(T... elementos);

    //T selectCodigo(String codigo);

    //T select(String sql, String... paramentros);

    List<T> selectAll();

    //List<T> selectAll(String sql, String... paramentros);

}

I have already created the models PRODUCTS and BRANDS

Also tagged

package br.com.jcom.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.jcom.factory.Conexao;
import br.com.jcom.modelo.Marca;

public class MarcaDao implements IDao<Marca> {

    private String nomeTabela = "Marcas";
    private Connection connection;

    public MarcaDao() {
        this.connection = new Conexao().getConnection();

    }

    @Override
    public String getNomeTabela() {
        return nomeTabela;
    }

    @Override
    public int insert(Marca... elementos) {

        String sql = "INSERT INTO " + nomeTabela + " (marca) VALUES (?)";
        PreparedStatement stmt = null;
        int result = 0;

        for (Marca marca : elementos) {
            try {
                stmt = connection.prepareStatement(sql);
                stmt.setString(1, marca.getMarca());
                result = stmt.executeUpdate();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally{
                Conexao.close(connection, stmt, null);
            }
        }

        return result;
    }

    @Override
    public int update(Marca... elementos) {

        String sql = "UPDATE " + nomeTabela + "SET marca = ? WHERE MarcaID = ?";
        PreparedStatement stmt = null;
        int result = 0;

        for (Marca marca : elementos) {
            try {
                stmt = connection.prepareStatement(sql);
                stmt.setString(1, marca.getMarca());
                stmt.setLong(2, marca.getMarcaID());
                result = stmt.executeUpdate();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                Conexao.close(connection, stmt, null);
            }
        }

        return result;
    }

    @Override
    public int delete(Marca... elementos) {

        String sql = "DELETE FROM " + nomeTabela + "WHERE MarcaID = ?";
        PreparedStatement stmt = null;
        int result = 0;

        for (Marca marca : elementos) {
            try {
                stmt = connection.prepareStatement(sql);
                stmt.setLong(1, marca.getMarcaID());
                result = stmt.executeUpdate();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                Conexao.close(connection, stmt, null);
            }
        }

        return result;
    }

    @Override
    public List<Marca> selectAll() {
        String sql = "SELECT * FROM " + nomeTabela;
        PreparedStatement stmt = null;
        List<Marca> marcas = new ArrayList<Marca>();
        ResultSet rs = null;

        try {
            stmt = connection.prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                Marca marca = new Marca();
                marca.setMarcaID(rs.getLong("marcaID"));
                marca.setMarca(rs.getString("marca"));

                marcas.add(marca);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            Conexao.close(connection, stmt, rs);
        }
        return marcas;

    }

}

And the Controller tag

package br.com.jcom.controller;

import java.util.List;

import br.com.jcom.facade.DaoFacade;
import br.com.jcom.modelo.Marca;

public class MarcaController {

    private DaoFacade marcaFacade;

    public MarcaController() {
        this.marcaFacade = new DaoFacade<Marca>();
    }

    public int addMarca(Marca marca){
        return marcaFacade.insert(marca);

    }

    public int updateMarca(Marca marca){
        return marcaFacade.update(marca);

    }

    public int deleteMarca(Marca marca){
        return marcaFacade.delete(marca);

    }

    public List<Marca> selectMarcas(){
        return marcaFacade.selectAll();

    }
}

Well now in the FACADE is that I am not able to understand the logic very well

package br.com.jcom.facade;

import java.util.List;

import br.com.jcom.dao.IDao;
import br.com.jcom.dao.MarcaDao;

public class DaoFacade<T> {
    @SuppressWarnings("rawtypes")
    private IDao dao;

    public DaoFacade() {
        this.dao = new MarcaDao();

    }

    @SuppressWarnings("unchecked")
    public int insert(T... elementos) {
        return dao.insert(elementos);

    }

    @SuppressWarnings("unchecked")
    public int update(T... elementos) {
        return dao.update(elementos);

    }

    @SuppressWarnings("unchecked")
    public int delete(T... elementos) {
        return dao.delete(elementos);

    }

    @SuppressWarnings("unchecked")
    public List<T> selectAll() {
        return dao.selectAll();

    }

}

When I create the constructor

public DaoFacade() {
        this.dao = new MarcaDao();
}

Instead of putting (= new MarcaDao ();) there is the possibility of passing a generic class to when I = new Product, I am confusing everything ??

MarcaGUI > btnSave

btnSalvar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Marca marca = new Marca();

            if (Funcoes.validaCampos(panel) == true) {
                marca.setMarca(tfMarca.getText());
            } else {
                JOptionPane.showMessageDialog(null,
                        "Preenchimento de campos (*) obrigatórios");
                return;
            }

            int result = 0;

            if (idMarca == null) {
                result = new MarcaController().addMarca(marca);
            } else {
                marca.setMarcaID(idMarca);
                result = new MarcaController().updateMarca(marca);
                idMarca = null;
            }

            if (result == 1) {
                JOptionPane.showMessageDialog(null, "Concluido");
            } else {
                JOptionPane.showMessageDialog(null, "Erro ao salvar");
            }

            refreshTable();
        }
    });
}
    
asked by anonymous 16.02.2015 / 17:07

1 answer

1

By what I see in the code the class DaoFacade is a generic class, ie it is prepared for what you intend.

When you instantiate it, you indicate which class it will use.

Your builder will be anything like this:

public class DaoFacade<T> {
    @SuppressWarnings("rawtypes")
    private T dao;

    public DaoFacade() {
        this.dao = new T();//Não é possível em JAVA - Não compila

    }
    .......
    .......
    .......
}

However, this is not sufficient because the methods will have to receive a type that is different for each Dao . Thus, the class DaoFacade , must have another generic type:

public class DaoFacade<TDao, TEntity> {
    @SuppressWarnings("rawtypes")
    private TDao dao;

    public DaoFacade() {
        this.dao = new TDao();//Não é possível em JAVA - Não compila

    }

    @SuppressWarnings("unchecked")
    public int insert(TEntity... elementos) {
        return dao.insert(elementos);

    }
    .......
    .......
    .......
}

This solution requires that an instance of the generic parameter be created, which can not be done in the usual way: dao = new ();

So, the correct way is to use the Interface IDao and pass Dao into the constructor:

public class DaoFacade<T> {
    @SuppressWarnings("rawtypes")
    private IDao dao;

    public DaoFacade(IDao dao) {
        this.dao = dao;

    }
    //O resto do código ficaria igual
    .......
    .......
    .......
}

In this case T would be the entity to process and all methods will look like you have. To create a new instance of DaoFacade:

new DaoFacad<Marca>(new MarcaDao());
    
16.02.2015 / 17:27